mango-ui-v3/hooks/useSrmAccount.tsx

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-17 10:14:39 -07:00
import { useState } from 'react'
import useMangoStore, { DEFAULT_CONNECTION } from '../stores/useMangoStore'
2021-04-09 17:01:00 -07:00
import { nativeToUi } from '@blockworks-foundation/mango-client'
import { 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)
const [srmAccount, setSrmAccount] = useState(null)
// const [msrmAccount, setMsrmAccount] = useState(null)
useEffect(() => {
2021-08-17 12:37:07 -07:00
if (mangoGroup) {
const srmPk = mangoGroup.srmVault
const fetchAccounts = async () => {
const srmAccountInfo = await DEFAULT_CONNECTION.getAccountInfo(srmPk)
2021-08-17 10:14:39 -07:00
2021-08-17 12:37:07 -07:00
setSrmAccount(srmAccountInfo)
}
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-04-12 20:39:08 -07:00
const accountData = srmAccount
? parseTokenAccountData(srmAccount?.data)
: null
const totalSrm = accountData
? nativeToUi(accountData.amount, SRM_DECIMALS)
: 0
const feeTier = getFeeTier(0, totalSrm)
2021-08-18 07:33:03 -07:00
const { maker, taker } = getFeeRates(feeTier)
2021-08-18 07:33:03 -07:00
// mul taker by 0.8 to account for GUI rebate
return { totalSrm, feeTier, rates: { maker, taker: taker * 0.8 } }
}
export default useSrmAccount