lev-stake-sol-v1/hooks/useLeverageMax.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import { useMemo } from 'react'
import useMangoGroup from './useMangoGroup'
2023-09-25 18:47:58 -07:00
import { floorToDecimal } from 'utils/numbers'
export default function useLeverageMax(selectedToken: string) {
const { group } = useMangoGroup()
const stakeBank = useMemo(() => {
return group?.banksMapByName.get(selectedToken)?.[0]
}, [selectedToken, group])
const borrowBank = useMemo(() => {
return group?.banksMapByName.get('SOL')?.[0]
}, [group])
const leverageMax = useMemo(() => {
2023-10-11 12:46:26 -07:00
if (!stakeBank || !borrowBank) return 0
const borrowInitLiabWeight = borrowBank.scaledInitLiabWeight(
borrowBank.price,
)
const stakeInitAssetWeight = stakeBank.scaledInitAssetWeight(
stakeBank.price,
)
if (!borrowInitLiabWeight || !stakeInitAssetWeight) return 1
2023-11-12 07:52:28 -08:00
const x = stakeInitAssetWeight.toNumber() / borrowInitLiabWeight.toNumber()
const conversionRate = borrowBank.uiPrice / stakeBank.uiPrice
2023-11-10 08:09:15 -08:00
2023-11-12 07:52:28 -08:00
const y = 1 - conversionRate * stakeInitAssetWeight.toNumber()
2023-11-12 12:55:49 -08:00
const max = floorToDecimal(1 + (x / y) * 0.9, 1).toNumber()
2023-11-10 07:17:21 -08:00
2023-11-08 16:52:12 -08:00
return max
}, [stakeBank, borrowBank])
return leverageMax
}