mars-v2-frontend/hooks/useTokenBalance.tsx
Gustavo Mauricio f709c12da2
Tooling improvements and minor refactor (#19)
* refactor: store selector callbacks less verbose

* chore: prettier tailwind plugin added and respective formatting

* disable metamask connection button
2022-09-30 13:50:16 +01:00

42 lines
995 B
TypeScript

import { useQuery } from '@tanstack/react-query'
import BigNumber from 'bignumber.js'
import useWalletStore from 'stores/useWalletStore'
import { chain } from 'utils/chains'
import { queryKeys } from 'types/query-keys-factory'
type Result = {
balance: {
amount: number
denom: string
}
}
const useTokenBalance = (denom?: string) => {
const address = useWalletStore((s) => s.address)
const result = useQuery<Result>(
queryKeys.tokenBalance(address, denom || chain.stakeCurrency.coinMinimalDenom),
async () =>
fetch(
`${chain.rest}/cosmos/bank/v1beta1/balances/${address}/by_denom?denom=${
denom || chain.stakeCurrency.coinMinimalDenom
}`
).then((res) => res.json()),
{
enabled: !!address,
}
)
return {
...result,
data: result?.data
? BigNumber(result.data.balance.amount)
.div(10 ** chain.stakeCurrency.coinDecimals)
.toNumber()
: 0,
}
}
export default useTokenBalance