Skip to content

Commit

Permalink
try gas estimation before checking allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
fionnachan committed Dec 18, 2024
1 parent 3b63f91 commit 6a7a5df
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface AddressToDecimals {
export type GasEstimates = {
estimatedParentChainGas: BigNumber
estimatedChildChainGas: BigNumber
isError?: boolean
}

export type DepositGasEstimates = GasEstimates & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getChainIdFromProvider,
percentIncrease
} from './utils'
import { tokenRequiresApprovalOnL2 } from '../util/L2ApprovalUtils'
import { withdrawInitTxEstimateGas } from '../util/WithdrawalUtils'
import { addressIsSmartContract } from '../util/AddressUtils'

Expand Down Expand Up @@ -104,17 +103,10 @@ export class Erc20WithdrawalStarter extends BridgeTransferStarter {
throw Error('Erc20 token address not found')
}

const destinationChainErc20Address =
await this.getDestinationChainErc20Address()

const address = await getAddressFromSigner(signer)

const sourceChainId = await getChainIdFromProvider(this.sourceChainProvider)

const destinationChainId = await getChainIdFromProvider(
this.destinationChainProvider
)

const gatewayAddress = await this.getSourceChainGatewayAddress()

const sourceChainTokenBridge = getArbitrumNetwork(sourceChainId).tokenBridge
Expand All @@ -130,28 +122,25 @@ export class Erc20WithdrawalStarter extends BridgeTransferStarter {
}

// the below checks are only run for tokens using custom gateway / custom custom gateway
//
// check if token withdrawal gas estimation fails
// if it fails, the token gateway is not allowed to burn the token without additional approval
const transferEstimateGasResult = await this.transferEstimateGas({
amount,
signer
})

// check if token is known to require withdrawal approval on child chain
// return true without checking if there is an existing allowance
// users might have already approved enough allowance, but it's rare
// so we can skip the check to save some calls
if (
await tokenRequiresApprovalOnL2({
tokenAddressOnParentChain: destinationChainErc20Address,
parentChainId: destinationChainId,
childChainId: sourceChainId
})
) {
return true
if (!transferEstimateGasResult.isError) {
return false
}

// a catch-all check for tokens not on the hardcoded list
const allowanceForSourceChainGateway = await fetchErc20Allowance({
address: this.sourceChainErc20Address,
provider: this.sourceChainProvider,
owner: address,
spender: gatewayAddress
})

return allowanceForSourceChainGateway.lt(amount)
}

Expand Down
88 changes: 0 additions & 88 deletions packages/arb-token-bridge-ui/src/util/L2ApprovalUtils.ts

This file was deleted.

28 changes: 0 additions & 28 deletions packages/arb-token-bridge-ui/src/util/TokenApprovalUtils.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/arb-token-bridge-ui/src/util/WithdrawalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ export async function withdrawInitTxEstimateGas({
// https://arbiscan.io/tx/0xb9c866257b6f8861c2323ae902f681f7ffa313c3a3b93347f1ecaa0aa5c9b59e
estimatedChildChainGas: isToken
? BigNumber.from(1_400_000)
: BigNumber.from(800_000)
: BigNumber.from(800_000),
isError: true
}
}
}
Expand Down
42 changes: 0 additions & 42 deletions packages/arb-token-bridge-ui/src/util/xErc20Utils.ts

This file was deleted.

0 comments on commit 6a7a5df

Please sign in to comment.