mango-ui-v3/hooks/useSrmAccount.tsx

66 lines
1.8 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'
import { getFeeTier, getFeeRates } from '@project-serum/serum'
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'
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