From c511d18e0469abbcfa32bf38ea991778ac4a7b29 Mon Sep 17 00:00:00 2001 From: Bartek Date: Wed, 11 Sep 2024 13:43:14 +0200 Subject: [PATCH] fix approval --- .../TransferPanel/TransferPanel.tsx | 19 +++++++++++++++++-- .../token-bridge-sdk/BridgeTransferStarter.ts | 6 ++++++ .../token-bridge-sdk/Erc20DepositStarter.ts | 14 ++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx b/packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx index d3f2ac46a6..653ea5a175 100644 --- a/packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx +++ b/packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx @@ -757,10 +757,20 @@ export function TransferPanel() { return } + const isCustomNativeTokenAmount2 = + nativeCurrency.isCustom && + isBatchTransferSupported && + Number(amount2) > 0 + const isNativeCurrencyApprovalRequired = await bridgeTransferStarter.requiresNativeCurrencyApproval({ signer, - amount: amountBigNumber + amount: amountBigNumber, + options: { + approvalAmountIncrease: isCustomNativeTokenAmount2 + ? utils.parseUnits(amount2, nativeCurrency.decimals) + : undefined + } }) if (isNativeCurrencyApprovalRequired) { @@ -770,7 +780,12 @@ export function TransferPanel() { const approvalTx = await bridgeTransferStarter.approveNativeCurrency({ signer, - amount: amountBigNumber + amount: amountBigNumber, + options: { + approvalAmountIncrease: isCustomNativeTokenAmount2 + ? utils.parseUnits(amount2, nativeCurrency.decimals) + : undefined + } }) if (approvalTx) { diff --git a/packages/arb-token-bridge-ui/src/token-bridge-sdk/BridgeTransferStarter.ts b/packages/arb-token-bridge-ui/src/token-bridge-sdk/BridgeTransferStarter.ts index 7dcbafa9ef..0d6a64f28e 100644 --- a/packages/arb-token-bridge-ui/src/token-bridge-sdk/BridgeTransferStarter.ts +++ b/packages/arb-token-bridge-ui/src/token-bridge-sdk/BridgeTransferStarter.ts @@ -63,6 +63,9 @@ export type TransferProps = { export type RequiresNativeCurrencyApprovalProps = { amount: BigNumber signer: Signer + options?: { + approvalAmountIncrease?: BigNumber + } } export type ApproveNativeCurrencyEstimateGasProps = { @@ -73,6 +76,9 @@ export type ApproveNativeCurrencyEstimateGasProps = { export type ApproveNativeCurrencyProps = { signer: Signer amount: BigNumber + options?: { + approvalAmountIncrease?: BigNumber + } } export type RequiresTokenApprovalProps = { diff --git a/packages/arb-token-bridge-ui/src/token-bridge-sdk/Erc20DepositStarter.ts b/packages/arb-token-bridge-ui/src/token-bridge-sdk/Erc20DepositStarter.ts index d11dcbdcf3..3934103f21 100644 --- a/packages/arb-token-bridge-ui/src/token-bridge-sdk/Erc20DepositStarter.ts +++ b/packages/arb-token-bridge-ui/src/token-bridge-sdk/Erc20DepositStarter.ts @@ -75,7 +75,8 @@ export class Erc20DepositStarter extends BridgeTransferStarter { public async requiresNativeCurrencyApproval({ amount, - signer + signer, + options }: RequiresNativeCurrencyApprovalProps) { if (!this.sourceChainErc20Address) { throw Error('Erc20 token address not found') @@ -128,7 +129,9 @@ export class Erc20DepositStarter extends BridgeTransferStarter { // We want to bridge a certain amount of an ERC-20 token, but the Retryable fees on the destination chain will be paid in the custom fee token // We have to check if the native-token spending allowance is enough to cover the fees return customFeeTokenAllowanceForSourceChainGateway.lt( - estimatedDestinationChainGasFee + estimatedDestinationChainGasFee.add( + options?.approvalAmountIncrease ?? BigNumber.from(0) + ) ) } @@ -153,7 +156,8 @@ export class Erc20DepositStarter extends BridgeTransferStarter { public async approveNativeCurrency({ signer, - amount + amount, + options }: ApproveNativeCurrencyProps) { if (!this.sourceChainErc20Address) { throw Error('Erc20 token address not found') @@ -198,7 +202,9 @@ export class Erc20DepositStarter extends BridgeTransferStarter { return erc20Bridger.approveGasToken({ erc20ParentAddress: this.sourceChainErc20Address, parentSigner: signer, - amount: estimatedDestinationChainGasFee + amount: estimatedDestinationChainGasFee.add( + options?.approvalAmountIncrease ?? BigNumber.from(0) + ) }) }