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

feat: check custom gateway tokens withdrawal approval #2144

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -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
@@ -1,4 +1,4 @@
import { Erc20Bridger } from '@arbitrum/sdk'
import { Erc20Bridger, getArbitrumNetwork } from '@arbitrum/sdk'
import { BigNumber, constants } from 'ethers'
import { ERC20__factory } from '@arbitrum/sdk/dist/lib/abi/factories/ERC20__factory'
import {
Expand All @@ -11,6 +11,7 @@ import {
TransferType
} from './BridgeTransferStarter'
import {
fetchErc20Allowance,
fetchErc20L2GatewayAddress,
getL1ERC20Address
} from '../util/TokenUtils'
Expand All @@ -19,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 @@ -86,6 +86,15 @@ export class Erc20WithdrawalStarter extends BridgeTransferStarter {
// no-op
}

/**
* Most tokens inherently allows the token gateway to burn whichever amount
* on the child chain for withdrawal because they inherited the
* IArbToken interface that allows the gateway to burn without allowance approval
*
* if the token does not have the bridgeBurn method, approval is required
* https://github.com/OffchainLabs/token-bridge-contracts/blob/d54877598e80a00d264d2b4353968faafd6f534d/contracts/tokenbridge/arbitrum/IArbToken.sol
*
*/
public requiresTokenApproval = async ({
amount,
signer
Expand All @@ -94,38 +103,45 @@ 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

// check first if token is even eligible for allowance check on l2
if (
(await tokenRequiresApprovalOnL2({
tokenAddressOnParentChain: destinationChainErc20Address,
parentChainId: destinationChainId,
childChainId: sourceChainId
})) &&
this.sourceChainErc20Address
) {
const token = ERC20__factory.connect(
this.sourceChainErc20Address,
this.sourceChainProvider
)

const gatewayAddress = await this.getSourceChainGatewayAddress()
const allowance = await token.allowance(address, gatewayAddress)

return allowance.lt(amount)
// tokens that use the standard gateways do not require approval on child chain
const standardGateways = [
sourceChainTokenBridge?.childErc20Gateway.toLowerCase(),
sourceChainTokenBridge?.childWethGateway.toLowerCase()
]

if (standardGateways.includes(gatewayAddress.toLowerCase())) {
return false
}

return false
// 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
})

if (!transferEstimateGasResult.isError) {
return false
}

const allowanceForSourceChainGateway = await fetchErc20Allowance({
address: this.sourceChainErc20Address,
provider: this.sourceChainProvider,
owner: address,
spender: gatewayAddress
})

return allowanceForSourceChainGateway.lt(amount)
}

public async approveTokenEstimateGas({ signer, amount }: ApproveTokenProps) {
Expand Down
96 changes: 0 additions & 96 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.

Loading