lev-stake-sol/hooks/useBankRates.ts

125 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-10-01 18:36:50 -07:00
import { useMemo } from 'react'
2023-09-20 18:33:18 -07:00
import useStakeRates from './useStakeRates'
import useMangoGroup from './useMangoGroup'
2023-10-01 18:36:50 -07:00
// import mangoStore from '@store/mangoStore'
import useLeverageMax from './useLeverageMax'
2023-09-20 18:33:18 -07:00
2023-10-01 18:36:50 -07:00
// const set = mangoStore.getState().set
2023-09-20 18:33:18 -07:00
export default function useBankRates(selectedToken: string, leverage: number) {
const { data: stakeRates } = useStakeRates()
const { group } = useMangoGroup()
2024-02-20 04:59:49 -08:00
2023-10-01 18:36:50 -07:00
// const estimatedMaxAPY = mangoStore((s) => s.estimatedMaxAPY.current)
const leverageMax = useLeverageMax(selectedToken)
2023-09-20 18:33:18 -07:00
const stakeBank = useMemo(() => {
return group?.banksMapByName.get(selectedToken)?.[0]
}, [selectedToken, group])
const borrowBank = useMemo(() => {
2024-02-20 04:59:49 -08:00
return group?.banksMapByName.get('USDC')?.[0]
2023-09-20 18:33:18 -07:00
}, [group])
const stakeBankDepositRate = useMemo(() => {
2024-02-27 07:30:05 -08:00
return stakeBank ? stakeBank.getDepositRate() : 0
2023-09-20 18:33:18 -07:00
}, [stakeBank])
const borrowBankBorrowRate = useMemo(() => {
2024-02-27 07:30:05 -08:00
return borrowBank ? Number(borrowBank.getBorrowRate()) : 0
2023-09-20 18:33:18 -07:00
}, [borrowBank])
2024-02-27 07:30:05 -08:00
const jlpStakeRateAPY = useMemo(() => {
return stakeRates ? stakeRates[selectedToken.toLowerCase()] : 0
2023-09-20 18:33:18 -07:00
}, [stakeRates, selectedToken])
2024-02-27 07:30:05 -08:00
const financialMetrics = useMemo(() => {
2024-03-03 02:35:38 -08:00
// Collateral fee is charged on the assets needed to back borrows and
// 1 deposited JLP can back maintAssetWeight * (1 JLP-value) USDC borrows.
const collateralFeePerBorrowPerDay =
Number(stakeBank?.collateralFeePerDay) /
Number(stakeBank?.maintAssetWeight)
// Convert the borrow APR to a daily rate
const borrowRatePerDay = Number(borrowBankBorrowRate) / 365
// Convert the JLP APY to a daily rate
const jlpRatePerDay = (1 + jlpStakeRateAPY) ** (1 / 365) - 1
// Assume the user deposits 1 JLP, then these are the starting deposits and
// borrows for the desired leverage (in terms of starting-value JLP)
const initialBorrows = leverage - 1
const initialDeposits = leverage
// In the following, we'll simulate time passing and how the deposits and
// borrows evolve.
// Note that these will be in terms of starting-value JLP, meaning that JLP
// price increases will be modelled as deposits increasing in amount.
let borrows = initialBorrows
let deposits = initialDeposits
2023-09-20 18:33:18 -07:00
2024-02-27 07:30:05 -08:00
let collectedCollateralFees = 0
let collectedReturns = 0
2024-02-22 06:12:37 -08:00
2024-02-27 07:30:05 -08:00
for (let day = 1; day <= 365; day++) {
2024-03-03 02:35:38 -08:00
borrows *= 1 + borrowRatePerDay
2024-02-27 10:15:26 -08:00
2024-03-03 02:35:38 -08:00
const collateralFees = collateralFeePerBorrowPerDay * borrows
deposits -= collateralFees
collectedCollateralFees += collateralFees
const jlpReturns = jlpRatePerDay * deposits
deposits += jlpReturns
collectedReturns += jlpReturns
2024-02-27 07:30:05 -08:00
}
// APY's for the calculation
2024-03-03 02:35:38 -08:00
const depositAPY = (deposits - initialDeposits) * 100
const collateralFeeAPY = collectedCollateralFees * 100
const collectedReturnsAPY = collectedReturns * 100
2024-02-27 07:30:05 -08:00
// Interest Fee APY: Reflecting borrowing cost as an annual percentage yield
2024-03-03 02:35:38 -08:00
const borrowsAPY = (borrows - initialBorrows) * 100
2024-02-27 07:30:05 -08:00
2024-03-03 02:35:38 -08:00
// Total APY, comparing the end value (deposits - borrows) to the starting value (1)
const APY = (deposits - borrows - 1) * 100
2023-09-20 18:33:18 -07:00
2024-02-27 07:30:05 -08:00
// Comparisons to outside
2024-03-03 02:35:38 -08:00
const nonMangoAPY = jlpStakeRateAPY * leverage * 100
const diffToNonMango = APY - nonMangoAPY
const diffToNonLeveraged = APY - jlpStakeRateAPY * 100
return {
APY,
depositAPY,
collectedReturnsAPY,
collateralFeeAPY,
borrowsAPY,
nonMangoAPY,
diffToNonMango,
diffToNonLeveraged,
}
}, [
leverage,
borrowBankBorrowRate,
jlpStakeRateAPY,
stakeBank?.collateralFeePerDay,
stakeBank?.maintAssetWeight,
])
2023-10-01 18:36:50 -07:00
const estimatedMaxAPY = useMemo(() => {
return (
2024-03-03 02:35:38 -08:00
jlpStakeRateAPY * leverageMax -
Number(borrowBankBorrowRate) * (leverageMax - 1)
2023-10-01 18:36:50 -07:00
)
2024-02-27 07:30:05 -08:00
}, [jlpStakeRateAPY, borrowBankBorrowRate, leverageMax])
2023-09-20 18:33:18 -07:00
return {
2024-02-27 07:30:05 -08:00
financialMetrics,
2023-09-20 18:33:18 -07:00
stakeBankDepositRate,
borrowBankBorrowRate,
2024-02-27 07:30:05 -08:00
jlpStakeRateAPY,
2023-09-20 18:33:18 -07:00
estimatedMaxAPY,
}
}