Skip to content

Commit

Permalink
fix: revert fix erc20Approvals (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcar121293 authored Nov 13, 2023
1 parent 68ac851 commit bbe0116
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
19 changes: 7 additions & 12 deletions packages/sdk/src/chains/EVM/assetTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,15 @@ export class EVMAssetTransfer extends BaseAssetTransfer {
handlerAddress: string,
): Promise<Array<PopulatedTransaction>> {
const approvals: Array<PopulatedTransaction> = [];
const transferAmount = BigNumber.from(transfer.details.amount);
if (
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)
fee.type == FeeHandlerType.DYNAMIC &&
(await getERC20Allowance(transfer.sender, erc20, fee.handlerAddress)).lt(fee.fee)
) {
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));
}

Expand Down
3 changes: 1 addition & 2 deletions packages/sdk/src/chains/EVM/utils/depositFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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';

Expand Down Expand Up @@ -134,7 +133,7 @@ export const executeDeposit = async (
overrides?: ethers.PayableOverrides,
): Promise<PopulatedTransaction> => {
const transactionSettings = {
value: feeData.type == FeeHandlerType.PERCENTAGE ? 0 : feeData.fee,
value: feeData.fee,
gasLimit: ASSET_TRANSFER_GAS_LIMIT,
};

Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/test/chains/EVM/assetTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,27 @@ describe('EVM asset transfer', () => {

const fee = {
fee: BigNumber.from('100'),
type: FeeHandlerType.PERCENTAGE,
type: FeeHandlerType.DYNAMIC,
handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e',
};
const approvals = await assetTransfer.buildApprovals(transfer, fee);

expect(approvals.length).toBe(1);
});

it('Should return 1 approval txs if ERC20 token not approved and percentage fee used', async function () {
it('Should return 2 approval txs if ERC20 token not approved and dynamic fee used', async function () {
resourceHandlerFunction.mockResolvedValue('0x03896BaeA3A8CD98E46C576AF6Ceaffb69a51AFB');
getAllowanceMock.mockResolvedValue(BigNumber.from('0'));
approveMock.mockResolvedValue({});

const fee = {
fee: BigNumber.from('100'),
type: FeeHandlerType.PERCENTAGE,
type: FeeHandlerType.DYNAMIC,
handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e',
};
const approvals = await assetTransfer.buildApprovals(transfer, fee);

expect(approvals.length).toBe(1);
expect(approvals.length).toBe(2);
});

it('Should return empty array if ERC721 token already approved', async function () {
Expand Down

0 comments on commit bbe0116

Please sign in to comment.