consider scaled weights

This commit is contained in:
tjs 2023-09-25 16:48:23 -04:00
parent 124471d26d
commit 0452e53fb9
4 changed files with 24 additions and 17 deletions

View File

@ -166,9 +166,6 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
setSubmitting(true) setSubmitting(true)
try { try {
console.log('starting deposit')
console.log('amountToBorrow', amountToBorrow)
const newAccountNum = getNextAccountNumber(mangoAccounts) const newAccountNum = getNextAccountNumber(mangoAccounts)
const { signature: tx, slot } = await stakeAndCreate( const { signature: tx, slot } = await stakeAndCreate(
client, client,
@ -214,6 +211,22 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
setLeverage(v * 1) setLeverage(v * 1)
}, []) }, [])
const leverageMax = useMemo(() => {
if (!stakeBank || !borrowBank) return 1
const borrowInitLiabWeight = borrowBank.scaledInitLiabWeight(
borrowBank.price,
)
const stakeInitAssetWeight = stakeBank.scaledInitAssetWeight(
stakeBank.price,
)
if (!borrowInitLiabWeight || !stakeInitAssetWeight) return 1
const x = stakeInitAssetWeight.div(borrowInitLiabWeight).toNumber()
const conversionRate = borrowBank.uiPrice / stakeBank.uiPrice
const y = 1 - conversionRate * stakeInitAssetWeight.toNumber()
return 1 + x / y
}, [stakeBank, borrowBank])
useEffect(() => { useEffect(() => {
const group = mangoStore.getState().group const group = mangoStore.getState().group
set((state) => { set((state) => {
@ -311,7 +324,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
<p className="mb-2 font-bold text-th-fgd-1">{leverage}x</p> <p className="mb-2 font-bold text-th-fgd-1">{leverage}x</p>
</div> </div>
<LeverageSlider <LeverageSlider
leverageMax={3} leverageMax={leverageMax}
onChange={changeLeverage} onChange={changeLeverage}
step={0.1} step={0.1}
/> />

View File

@ -158,7 +158,6 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) {
setSubmitting(true) setSubmitting(true)
try { try {
console.log('starting deposit')
const { signature: tx } = await unstakeAndClose( const { signature: tx } = await unstakeAndClose(
client, client,
group, group,

View File

@ -14,19 +14,16 @@ const fetchRates = async () => {
fetchAndParsePricesCsv(DATA_SOURCE.SOLBLAZE_CSV), fetchAndParsePricesCsv(DATA_SOURCE.SOLBLAZE_CSV),
fetchAndParsePricesCsv(DATA_SOURCE.LIDO_CSV), fetchAndParsePricesCsv(DATA_SOURCE.LIDO_CSV),
]) ])
console.log('jitosol', jitoPrices)
// may be null if the price range cannot be calculated // may be null if the price range cannot be calculated
const msolRange = getPriceRangeFromPeriod(msolPrices, PERIOD.DAYS_30) const msolRange = getPriceRangeFromPeriod(msolPrices, PERIOD.DAYS_30)
const jitoRange = getPriceRangeFromPeriod(jitoPrices, PERIOD.DAYS_30) const jitoRange = getPriceRangeFromPeriod(jitoPrices, PERIOD.DAYS_30)
const bsolRange = getPriceRangeFromPeriod(bsolPrices, PERIOD.DAYS_30) const bsolRange = getPriceRangeFromPeriod(bsolPrices, PERIOD.DAYS_30)
const lidoRange = getPriceRangeFromPeriod(lidoPrices, PERIOD.DAYS_30) const lidoRange = getPriceRangeFromPeriod(lidoPrices, PERIOD.DAYS_30)
console.log('msol prices', msolPrices)
const rateData: Record<string, number> = {} const rateData: Record<string, number> = {}
if (msolRange) { if (msolRange) {
console.log('APY: ', calcYield(msolRange)?.apy) // 0.06707557862842384 => 6.71 %
rateData.msol = calcYield(msolRange)?.apy rateData.msol = calcYield(msolRange)?.apy
} }
if (jitoRange) { if (jitoRange) {

View File

@ -50,8 +50,6 @@ export const unstakeAndClose = async (
} }
const stakeBalance = mangoAccount.getTokenBalanceUi(stakeBank) const stakeBalance = mangoAccount.getTokenBalanceUi(stakeBank)
const borrowedSol = mangoAccount.getTokenBalance(solBank) const borrowedSol = mangoAccount.getTokenBalance(solBank)
console.log('borrowedSol', borrowedSol)
console.log('unstake amount', amount)
let swapAlts: AddressLookupTableAccount[] = [] let swapAlts: AddressLookupTableAccount[] = []
if (borrowedSol.toNumber()) { if (borrowedSol.toNumber()) {
@ -101,12 +99,6 @@ export const unstakeAndClose = async (
amount, amount,
group.getMintDecimals(stakeBank.mint), group.getMintDecimals(stakeBank.mint),
) )
console.log(
'amount, stakeBalance, nativeAmount',
amount,
floorToDecimal(stakeBalance, stakeBank.mintDecimals).toNumber(),
nativeWithdrawAmount.toNumber(),
)
const withdrawMax = const withdrawMax =
amount == floorToDecimal(stakeBalance, stakeBank.mintDecimals).toNumber() amount == floorToDecimal(stakeBalance, stakeBank.mintDecimals).toNumber()
const healthRemainingAccounts: PublicKey[] = withdrawMax const healthRemainingAccounts: PublicKey[] = withdrawMax
@ -201,7 +193,13 @@ export const stakeAndCreate = async (
} }
const depositHealthRemainingAccounts: PublicKey[] = mangoAccount const depositHealthRemainingAccounts: PublicKey[] = mangoAccount
? client.buildHealthRemainingAccounts(group, [mangoAccount], [], [], []) ? client.buildHealthRemainingAccounts(
group,
[mangoAccount],
[stakeBank],
[],
[],
)
: [stakeBank.publicKey, stakeBank.oracle] : [stakeBank.publicKey, stakeBank.oracle]
const depositTokenIxs = await createDepositIx( const depositTokenIxs = await createDepositIx(
client, client,