2023-09-25 17:50:05 -07:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import useMangoGroup from './useMangoGroup'
|
2023-09-25 18:47:58 -07:00
|
|
|
import { floorToDecimal } from 'utils/numbers'
|
2023-09-25 17:50:05 -07:00
|
|
|
|
|
|
|
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(() => {
|
|
|
|
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()
|
2023-09-25 18:47:58 -07:00
|
|
|
return floorToDecimal(1 + x / y, 1).toNumber()
|
2023-09-25 17:50:05 -07:00
|
|
|
}, [stakeBank, borrowBank])
|
|
|
|
|
|
|
|
return leverageMax
|
|
|
|
}
|