diff --git a/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasEstimates.ts b/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasEstimates.ts index 965d0be202..a1be6322c8 100644 --- a/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasEstimates.ts +++ b/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasEstimates.ts @@ -1,10 +1,13 @@ import { BigNumber, Signer } from 'ethers' import useSWR from 'swr' -import { useSigner } from 'wagmi' +import { useAccount, useSigner } from 'wagmi' import { DepositGasEstimates, GasEstimates } from '../arbTokenBridge.types' import { BridgeTransferStarterFactory } from '@/token-bridge-sdk/BridgeTransferStarterFactory' import { getProviderForChainId } from '@/token-bridge-sdk/utils' +import { useAppState } from '../../state' +import { useBalanceOnSourceChain } from '../useBalanceOnSourceChain' +import { useNetworks } from '../useNetworks' async function fetcher([ signer, @@ -36,35 +39,36 @@ async function fetcher([ } export function useGasEstimates({ - walletAddress, - sourceChainId, - destinationChainId, sourceChainErc20Address, destinationChainErc20Address, - amount, - sourceChainBalance + amount }: { - walletAddress?: string - sourceChainId: number - destinationChainId: number sourceChainErc20Address?: string destinationChainErc20Address?: string amount: BigNumber - sourceChainBalance: BigNumber | null }): { gasEstimates: GasEstimates | DepositGasEstimates | undefined error: any } { + const [{ sourceChain, destinationChain }] = useNetworks() + const { + app: { selectedToken: token } + } = useAppState() + const { address: walletAddress } = useAccount() + const balance = useBalanceOnSourceChain(token) const { data: signer } = useSigner() + const amountToTransfer = + balance !== null && amount.gte(balance) ? balance : amount + const { data: gasEstimates, error } = useSWR( - signer && sourceChainBalance && sourceChainBalance.gte(amount) + signer ? ([ - sourceChainId, - destinationChainId, + sourceChain.id, + destinationChain.id, sourceChainErc20Address, destinationChainErc20Address, - amount.toString(), // BigNumber is not serializable + amountToTransfer.toString(), // BigNumber is not serializable walletAddress, 'gasEstimates' ] as const) diff --git a/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts b/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts index e888413c4d..27d8928040 100644 --- a/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts +++ b/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts @@ -1,6 +1,5 @@ import { constants, utils } from 'ethers' -import { useAccount } from 'wagmi' -import { useCallback, useEffect, useMemo, useState } from 'react' +import { useMemo } from 'react' import { useDebounce } from '@uidotdev/usehooks' import { useAppState } from '../../state' @@ -40,7 +39,6 @@ export function useGasSummary(): UseGasSummaryResult { const [networks] = useNetworks() const { childChainProvider, parentChainProvider, isDepositMode } = useNetworksRelationship(networks) - const { address: walletAddress } = useAccount() const [{ amount }] = useArbQueryParams() const debouncedAmount = useDebounce(amount, 300) @@ -64,17 +62,13 @@ export function useGasSummary(): UseGasSummaryResult { const { gasEstimates: estimateGasResult, error: gasEstimatesError } = useGasEstimates({ - walletAddress, - sourceChainId: networks.sourceChain.id, - destinationChainId: networks.destinationChain.id, amount: amountBigNumber, sourceChainErc20Address: isDepositMode ? token?.address : token?.l2Address, destinationChainErc20Address: isDepositMode ? token?.l2Address - : token?.address, - sourceChainBalance: balance + : token?.address }) const estimatedParentChainGasFees = useMemo(() => { @@ -132,39 +126,27 @@ export function useGasSummary(): UseGasSummaryResult { } } - if (!balance) { + if (balance === null) { return { status: 'loading', - estimatedParentChainGasFees: undefined, - estimatedChildChainGasFees: undefined + estimatedParentChainGasFees, + estimatedChildChainGasFees } } - // If user has input an amount over their balance, don't estimate gas if (amountBigNumber.gt(balance)) { return { status: 'insufficientBalance', - estimatedParentChainGasFees: undefined, - estimatedChildChainGasFees: undefined - } - } - - if ( - typeof estimatedParentChainGasFees === 'undefined' || - typeof estimatedChildChainGasFees === 'undefined' - ) { - return { - status: 'loading', - estimatedParentChainGasFees: undefined, - estimatedChildChainGasFees: undefined + estimatedParentChainGasFees, + estimatedChildChainGasFees } } if (gasEstimatesError) { return { status: 'error', - estimatedParentChainGasFees: undefined, - estimatedChildChainGasFees: undefined + estimatedParentChainGasFees, + estimatedChildChainGasFees } }