From 7bee64b28553ee67e0fb5635b5cad421ec2a329d Mon Sep 17 00:00:00 2001 From: tcar Date: Mon, 13 Nov 2023 12:41:07 +0100 Subject: [PATCH 1/4] fix erc20Approvals; Signed-off-by: tcar --- packages/sdk/src/chains/EVM/assetTransfer.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/chains/EVM/assetTransfer.ts b/packages/sdk/src/chains/EVM/assetTransfer.ts index 8f05f1799..cc73c77f6 100644 --- a/packages/sdk/src/chains/EVM/assetTransfer.ts +++ b/packages/sdk/src/chains/EVM/assetTransfer.ts @@ -262,15 +262,20 @@ export class EVMAssetTransfer extends BaseAssetTransfer { handlerAddress: string, ): Promise> { const approvals: Array = []; + const transferAmount = BigNumber.from(transfer.details.amount); if ( - fee.type == FeeHandlerType.DYNAMIC && - (await getERC20Allowance(transfer.sender, erc20, fee.handlerAddress)).lt(fee.fee) + fee.type == FeeHandlerType.PERCENTAGE && + (await getERC20Allowance(transfer.sender, erc20, fee.handlerAddress)).lt( + fee.fee.add(transferAmount), + ) + ) { + approvals.push( + await approve(fee.fee.add(transferAmount).toString(), erc20, fee.handlerAddress), + ); + } else if ( + fee.type == FeeHandlerType.BASIC && + (await getERC20Allowance(transfer.sender, erc20, handlerAddress)).lt(transferAmount) ) { - approvals.push(await approve(fee.fee.toString(), erc20, fee.handlerAddress)); - } - - const transferAmount = BigNumber.from(transfer.details.amount); - if ((await getERC20Allowance(transfer.sender, erc20, handlerAddress)).lt(transferAmount)) { approvals.push(await approve(transferAmount.toString(), erc20, handlerAddress)); } From c7623cfae1bf2248eec05f9e13ca3fdf229b36a7 Mon Sep 17 00:00:00 2001 From: tcar Date: Mon, 13 Nov 2023 12:48:12 +0100 Subject: [PATCH 2/4] update tests; Signed-off-by: tcar --- packages/sdk/test/chains/EVM/assetTransfer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sdk/test/chains/EVM/assetTransfer.test.ts b/packages/sdk/test/chains/EVM/assetTransfer.test.ts index b7e74cdae..2e978d9f9 100644 --- a/packages/sdk/test/chains/EVM/assetTransfer.test.ts +++ b/packages/sdk/test/chains/EVM/assetTransfer.test.ts @@ -214,7 +214,7 @@ describe('EVM asset transfer', () => { const fee = { fee: BigNumber.from('100'), - type: FeeHandlerType.DYNAMIC, + type: FeeHandlerType.PERCENTAGE, handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e', }; const approvals = await assetTransfer.buildApprovals(transfer, fee); @@ -229,7 +229,7 @@ describe('EVM asset transfer', () => { const fee = { fee: BigNumber.from('100'), - type: FeeHandlerType.DYNAMIC, + type: FeeHandlerType.PERCENTAGE, handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e', }; const approvals = await assetTransfer.buildApprovals(transfer, fee); From c9cd9419aa17e893ea461d59c6c486fbeaf1595b Mon Sep 17 00:00:00 2001 From: tcar Date: Mon, 13 Nov 2023 12:59:26 +0100 Subject: [PATCH 3/4] update test; Signed-off-by: tcar --- packages/sdk/test/chains/EVM/assetTransfer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sdk/test/chains/EVM/assetTransfer.test.ts b/packages/sdk/test/chains/EVM/assetTransfer.test.ts index 2e978d9f9..992a135c2 100644 --- a/packages/sdk/test/chains/EVM/assetTransfer.test.ts +++ b/packages/sdk/test/chains/EVM/assetTransfer.test.ts @@ -222,7 +222,7 @@ describe('EVM asset transfer', () => { expect(approvals.length).toBe(1); }); - it('Should return 2 approval txs if ERC20 token not approved and dynamic fee used', async function () { + it('Should return 1 approval txs if ERC20 token not approved and percentage fee used', async function () { resourceHandlerFunction.mockResolvedValue('0x03896BaeA3A8CD98E46C576AF6Ceaffb69a51AFB'); getAllowanceMock.mockResolvedValue(BigNumber.from('0')); approveMock.mockResolvedValue({}); @@ -234,7 +234,7 @@ describe('EVM asset transfer', () => { }; const approvals = await assetTransfer.buildApprovals(transfer, fee); - expect(approvals.length).toBe(2); + expect(approvals.length).toBe(1); }); it('Should return empty array if ERC721 token already approved', async function () { From 064c70e4b1ec9fad0b6d6b6c43d6f10f07edf318 Mon Sep 17 00:00:00 2001 From: tcar Date: Mon, 13 Nov 2023 13:22:02 +0100 Subject: [PATCH 4/4] don't send msg.value when using percentage fee handler; Signed-off-by: tcar --- packages/sdk/src/chains/EVM/utils/depositFns.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/chains/EVM/utils/depositFns.ts b/packages/sdk/src/chains/EVM/utils/depositFns.ts index a97d60858..0053c84db 100644 --- a/packages/sdk/src/chains/EVM/utils/depositFns.ts +++ b/packages/sdk/src/chains/EVM/utils/depositFns.ts @@ -2,6 +2,7 @@ import { BigNumber, ContractReceipt, PopulatedTransaction, ethers } from 'ethers import { Bridge } from '@buildwithsygma/sygma-contracts'; import { DepositEvent } from '@buildwithsygma/sygma-contracts/dist/ethers/Bridge.js'; +import { FeeHandlerType } from '../../../types'; import { createERCDepositData, createPermissionlessGenericDepositData } from '../helpers.js'; import { Erc20TransferParamsType, Erc721TransferParamsType, EvmFee } from '../types/index.js'; @@ -133,7 +134,7 @@ export const executeDeposit = async ( overrides?: ethers.PayableOverrides, ): Promise => { const transactionSettings = { - value: feeData.fee, + value: feeData.type == FeeHandlerType.PERCENTAGE ? 0 : feeData.fee, gasLimit: ASSET_TRANSFER_GAS_LIMIT, };