mars-v2-frontend/hooks/useTokenBalance.tsx
Linkie Link 27cdd1c954
Linter and prettier adjustments (#50)
* tidy: added eslintrc and prettierrc rules

* tidy: formated the files via ‚yarn format‘

* import sort improvements

* format script regex fix

* replace eslint import severity to warning

* remove staged file

Co-authored-by: Gustavo Mauricio <gustavo.mauricio58@gmail.com>
2022-11-09 10:04:06 +01:00

42 lines
997 B
TypeScript

import { useQuery } from '@tanstack/react-query'
import BigNumber from 'bignumber.js'
import useWalletStore from 'stores/useWalletStore'
import { queryKeys } from 'types/query-keys-factory'
import { chain } from 'utils/chains'
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