vega-frontend-monorepo/libs/deposits/src/lib/use-get-balance-of-erc20-token.ts
Matthew Russell 11be7aaa8a
refacotr: deposit manager (#867)
* refactor: deposit manager with a zustand store and refetching balances after contracts complete

* refactor: remove assetId query string functionality

* chore: remove unused import

* chore: add a comment with a link to code explanation

* refactor: capture errors from deposit value get functions

* refactor: add error handling for async perform funcs

* feat: add assets to react helpers for types and erc20 check
2022-07-28 13:23:59 +01:00

30 lines
864 B
TypeScript

import type { Token } from '@vegaprotocol/smart-contracts';
import * as Sentry from '@sentry/react';
import { useWeb3React } from '@web3-react/core';
import { useCallback } from 'react';
import BigNumber from 'bignumber.js';
import type { Asset } from '@vegaprotocol/react-helpers';
import { addDecimal } from '@vegaprotocol/react-helpers';
export const useGetBalanceOfERC20Token = (
contract: Token | null,
asset: Asset | undefined
) => {
const { account } = useWeb3React();
const getBalance = useCallback(async () => {
if (!contract || !asset || !account) {
return;
}
try {
const res = await contract.balanceOf(account);
return new BigNumber(addDecimal(res.toString(), asset.decimals));
} catch (err) {
Sentry.captureException(err);
return;
}
}, [contract, asset, account]);
return getBalance;
};