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)
try {
console.log('starting deposit')
console.log('amountToBorrow', amountToBorrow)
const newAccountNum = getNextAccountNumber(mangoAccounts)
const { signature: tx, slot } = await stakeAndCreate(
client,
@ -214,6 +211,22 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
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(() => {
const group = mangoStore.getState().group
set((state) => {
@ -311,7 +324,7 @@ function StakeForm({ token: selectedToken }: StakeFormProps) {
<p className="mb-2 font-bold text-th-fgd-1">{leverage}x</p>
</div>
<LeverageSlider
leverageMax={3}
leverageMax={leverageMax}
onChange={changeLeverage}
step={0.1}
/>

View File

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

View File

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

View File

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