Skip to content

Commit

Permalink
Merge branch 'master' into fs-729-add-cctp-e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx authored Sep 3, 2024
2 parents 21c6af5 + ca78d06 commit 734b147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 734b147

Please sign in to comment.