From 6940013d82e32ee452b7513a774d3208ecc82c87 Mon Sep 17 00:00:00 2001 From: Fionna <13184582+fionnachan@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:10:25 +0100 Subject: [PATCH] refactor: clean up gas summary code --- .../src/hooks/TransferPanel/useGasSummary.ts | 70 +++++++++---------- 1 file changed, 32 insertions(+), 38 deletions(-) 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 0baf591ff6..e888413c4d 100644 --- a/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts +++ b/packages/arb-token-bridge-ui/src/hooks/TransferPanel/useGasSummary.ts @@ -20,12 +20,6 @@ import { useSelectedTokenDecimals } from './useSelectedTokenDecimals' import { percentIncrease } from '@/token-bridge-sdk/utils' import { DEFAULT_GAS_PRICE_PERCENT_INCREASE } from '@/token-bridge-sdk/Erc20DepositStarter' -const INITIAL_GAS_SUMMARY_RESULT: UseGasSummaryResult = { - status: 'loading', - estimatedParentChainGasFees: undefined, - estimatedChildChainGasFees: undefined -} - export type GasEstimationStatus = | 'loading' | 'success' @@ -50,9 +44,6 @@ export function useGasSummary(): UseGasSummaryResult { const [{ amount }] = useArbQueryParams() const debouncedAmount = useDebounce(amount, 300) - const [gasSummary, setGasSummary] = useState( - INITIAL_GAS_SUMMARY_RESULT - ) const decimals = useSelectedTokenDecimals() const amountBigNumber = useMemo(() => { @@ -69,15 +60,6 @@ export function useGasSummary(): UseGasSummaryResult { const parentChainGasPrice = useGasPrice({ provider: parentChainProvider }) const childChainGasPrice = useGasPrice({ provider: childChainProvider }) - const setGasSummaryStatus = useCallback( - (status: GasEstimationStatus) => - setGasSummary(previousGasSummary => ({ - ...previousGasSummary, - status - })), - [] - ) - const balance = useBalanceOnSourceChain(token) const { gasEstimates: estimateGasResult, error: gasEstimatesError } = @@ -137,56 +119,68 @@ export function useGasSummary(): UseGasSummaryResult { ) }, [childChainGasPrice, estimateGasResult, isDepositMode]) - useEffect(() => { + const gasSummary: UseGasSummaryResult = useMemo(() => { if ( !isDepositMode && (isTokenArbitrumOneNativeUSDC(token?.address) || isTokenArbitrumSepoliaNativeUSDC(token?.address)) ) { - setGasSummaryStatus('unavailable') - return + return { + status: 'unavailable', + estimatedParentChainGasFees: undefined, + estimatedChildChainGasFees: undefined + } } if (!balance) { - setGasSummaryStatus('loading') - return + return { + status: 'loading', + estimatedParentChainGasFees: undefined, + estimatedChildChainGasFees: undefined + } } // If user has input an amount over their balance, don't estimate gas if (amountBigNumber.gt(balance)) { - setGasSummaryStatus('insufficientBalance') - return + return { + status: 'insufficientBalance', + estimatedParentChainGasFees: undefined, + estimatedChildChainGasFees: undefined + } } if ( typeof estimatedParentChainGasFees === 'undefined' || typeof estimatedChildChainGasFees === 'undefined' ) { - setGasSummaryStatus('loading') - return + return { + status: 'loading', + estimatedParentChainGasFees: undefined, + estimatedChildChainGasFees: undefined + } } if (gasEstimatesError) { - setGasSummaryStatus('error') - return + return { + status: 'error', + estimatedParentChainGasFees: undefined, + estimatedChildChainGasFees: undefined + } } - setGasSummary({ + return { status: 'success', estimatedParentChainGasFees, estimatedChildChainGasFees - }) + } }, [ - walletAddress, - balance, - token, - childChainProvider, - setGasSummaryStatus, isDepositMode, + token?.address, + balance, + amountBigNumber, estimatedParentChainGasFees, estimatedChildChainGasFees, - gasEstimatesError, - amountBigNumber + gasEstimatesError ]) return gasSummary