diff --git a/packages/maskbook/src/plugins/Wallet/SNSAdaptor/WalletStatusDialog/TransactionDescription.tsx b/packages/maskbook/src/plugins/Wallet/SNSAdaptor/WalletStatusDialog/TransactionDescription.tsx index 8f5941c23a15..af21a2f18685 100644 --- a/packages/maskbook/src/plugins/Wallet/SNSAdaptor/WalletStatusDialog/TransactionDescription.tsx +++ b/packages/maskbook/src/plugins/Wallet/SNSAdaptor/WalletStatusDialog/TransactionDescription.tsx @@ -7,13 +7,16 @@ import { NativeTokenDetailed, ERC20TokenDetailed, FungibleTokenDetailed, + pow10, } from '@masknet/web3-shared' import type Services from '../../../../extension/service' -function getTokenAmountDescription(amount?: string, tokenDetailed?: FungibleTokenDetailed, negative?: boolean) { - return `${negative ? '-' : ''}${formatBalance(amount ?? '0', tokenDetailed?.decimals ?? 0, 4)} ${ - tokenDetailed?.symbol - }`.trim() +function getTokenAmountDescription(amount = '0', tokenDetailed?: FungibleTokenDetailed, negative?: boolean) { + return `${negative ? '-' : ''}${ + pow10(9 + (tokenDetailed?.decimals ?? 18)).isGreaterThanOrEqualTo(amount) + ? formatBalance(amount, tokenDetailed?.decimals ?? 0, 4) + : 'infinite' + } ${tokenDetailed?.symbol}`.trim() } function getTransactionDescription( diff --git a/packages/web3-shared/src/hooks/index.ts b/packages/web3-shared/src/hooks/index.ts index cc09aa3f0ccd..576222a4210b 100644 --- a/packages/web3-shared/src/hooks/index.ts +++ b/packages/web3-shared/src/hooks/index.ts @@ -14,6 +14,7 @@ export * from './useAddressNames' export * from './useERC1155TokenAssetDetailed' export * from './useERC165' export * from './useERC20TokenAllowance' +export * from './useERC20TokenTotalSupply' export * from './useERC20TokenApproveCallback' export * from './useERC20TokenBalance' export * from './useERC20TokenDetailed' diff --git a/packages/web3-shared/src/hooks/useERC20TokenTotalSupply.ts b/packages/web3-shared/src/hooks/useERC20TokenTotalSupply.ts new file mode 100644 index 000000000000..828dddff188d --- /dev/null +++ b/packages/web3-shared/src/hooks/useERC20TokenTotalSupply.ts @@ -0,0 +1,14 @@ +import { useAsyncRetry } from 'react-use' +import { EthereumAddress } from 'wallet.ts' +import { useChainId } from '.' +import { useERC20TokenContract } from '..' + +export function useERC20TokenTotalSupply(address?: string) { + const chainId = useChainId() + const erc20TokenContract = useERC20TokenContract(address) + return useAsyncRetry(async () => { + if (!address) return + if (!EthereumAddress.isValid(address)) return + return erc20TokenContract?.methods.totalSupply().call() + }, [chainId, address, erc20TokenContract]) +}