Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up gas summary code #1853

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading