Skip to content

Commit

Permalink
fix: infinite approve amount (DimensionDev#4435)
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui authored Sep 23, 2021
1 parent 629c554 commit 620839c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/web3-shared/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 14 additions & 0 deletions packages/web3-shared/src/hooks/useERC20TokenTotalSupply.ts
Original file line number Diff line number Diff line change
@@ -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])
}

0 comments on commit 620839c

Please sign in to comment.