Skip to content

Commit

Permalink
refactor: clean up gas summary code
Browse files Browse the repository at this point in the history
  • Loading branch information
fionnachan committed Aug 20, 2024
1 parent cebdbee commit 6940013
Showing 1 changed file with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -50,9 +44,6 @@ export function useGasSummary(): UseGasSummaryResult {

const [{ amount }] = useArbQueryParams()
const debouncedAmount = useDebounce(amount, 300)
const [gasSummary, setGasSummary] = useState<UseGasSummaryResult>(
INITIAL_GAS_SUMMARY_RESULT
)
const decimals = useSelectedTokenDecimals()

const amountBigNumber = useMemo(() => {
Expand All @@ -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 } =
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6940013

Please sign in to comment.