mango-ui-v3/hooks/useSrmAccount.tsx

102 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-08-17 10:14:39 -07:00
import { useState } from 'react'
2021-09-13 14:14:59 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-10-18 13:38:03 -07:00
import {
getMultipleAccounts,
nativeToUi,
} from '@blockworks-foundation/mango-client'
import {
MSRM_DECIMALS,
SRM_DECIMALS,
} from '@project-serum/serum/lib/token-instructions'
2021-04-09 17:01:00 -07:00
import { parseTokenAccountData } from '../utils/tokens'
2021-08-17 10:14:39 -07:00
import { useEffect } from 'react'
2021-12-08 09:27:48 -08:00
export function getFeeRates(feeTier: number): { taker: number; maker: number } {
if (feeTier === 1) {
return { taker: 0.00039, maker: 0 }
} else if (feeTier === 2) {
return { taker: 0.00038, maker: 0 }
} else if (feeTier === 3) {
return { taker: 0.00036, maker: 0 }
} else if (feeTier === 4) {
return { taker: 0.00034, maker: 0 }
} else if (feeTier === 5) {
return { taker: 0.00032, maker: 0 }
} else if (feeTier === 6) {
// MSRM
return { taker: 0.0003, maker: 0 }
}
// Base
return { taker: 0.0004, maker: 0 }
}
export function getFeeTier(msrmBalance: number, srmBalance: number): number {
if (msrmBalance >= 1) {
return 6
} else if (srmBalance >= 1_000_000) {
return 5
} else if (srmBalance >= 100_000) {
return 4
} else if (srmBalance >= 10_000) {
return 3
} else if (srmBalance >= 1_000) {
return 2
} else if (srmBalance >= 100) {
return 1
} else {
return 0
}
}
const useSrmAccount = () => {
2021-08-17 10:14:39 -07:00
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
2021-09-13 14:14:59 -07:00
const connection = useMangoStore((s) => s.connection.current)
2021-08-17 10:14:39 -07:00
const [srmAccount, setSrmAccount] = useState(null)
2021-10-18 13:38:03 -07:00
const [msrmAccount, setMsrmAccount] = useState(null)
2021-08-17 10:14:39 -07:00
useEffect(() => {
2021-08-17 12:37:07 -07:00
if (mangoGroup) {
const srmPk = mangoGroup.srmVault
2021-10-18 13:38:03 -07:00
const msrmPk = mangoGroup.msrmVault
2021-08-17 12:37:07 -07:00
const fetchAccounts = async () => {
2021-10-18 13:38:03 -07:00
const [srmAccountInfo, msrmAccountInfo] = await getMultipleAccounts(
connection,
[srmPk, msrmPk]
)
2021-08-17 10:14:39 -07:00
2021-10-18 13:38:03 -07:00
setSrmAccount(srmAccountInfo.accountInfo)
setMsrmAccount(msrmAccountInfo.accountInfo)
2021-08-17 12:37:07 -07:00
}
2021-08-17 10:14:39 -07:00
2021-08-17 12:37:07 -07:00
fetchAccounts()
}
2021-08-17 10:14:39 -07:00
}, [mangoGroup])
2021-10-18 13:38:03 -07:00
const srmAccountData = srmAccount
2021-04-12 20:39:08 -07:00
? parseTokenAccountData(srmAccount?.data)
: null
2021-10-18 13:38:03 -07:00
const totalSrm = srmAccountData
? nativeToUi(srmAccountData.amount, SRM_DECIMALS)
: 0
const msrmAccountData = msrmAccount
? parseTokenAccountData(msrmAccount?.data)
: null
const totalMsrm = msrmAccountData
? nativeToUi(msrmAccountData.amount, MSRM_DECIMALS)
2021-04-12 20:39:08 -07:00
: 0
2021-10-18 13:38:03 -07:00
const feeTier = getFeeTier(totalMsrm, totalSrm)
2021-08-18 07:33:03 -07:00
const { maker, taker } = getFeeRates(feeTier)
2021-10-18 13:38:03 -07:00
// account for GUI rebate in taker fees
return {
totalSrm,
totalMsrm,
feeTier,
rates: { maker, taker, takerWithRebate: taker - (taker + maker) * 0.2 },
}
}
export default useSrmAccount