mars-v2-frontend/hooks/useTokenBalance.tsx
Linkie Link c4f8f4eab0
Mp 1757 wallet connect (#66)
* MP-1757: implemented the WalletProvider and connect buttons

* tidy: tidy up the search

* MP-1691: moved modals outside of the DOM

* MP-1691: changed CreditManager into AccountDetails

* fix: fixed the naming

* MP-1691: UX approvements

* MP-1691: global confirm and delete modal added

* fix: merged the credit-account and wallet branch

* MP-1757: added the status store

* fix: updated the store interaction

* MP-1757: major cleanup of stores

* tidy: format
2022-12-08 21:14:38 +01:00

42 lines
992 B
TypeScript

import { useQuery } from '@tanstack/react-query'
import BigNumber from 'bignumber.js'
import { useWalletStore } from 'stores'
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