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

fix: revert fix erc20Approvals #313

Merged
merged 1 commit into from
Nov 13, 2023
Merged
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
19 changes: 7 additions & 12 deletions packages/sdk/src/chains/EVM/assetTransfer.ts
Original file line number Diff line number Diff line change
@@ -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));
}

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

@@ -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,
};

8 changes: 4 additions & 4 deletions packages/sdk/test/chains/EVM/assetTransfer.test.ts
Original file line number Diff line number Diff line change
@@ -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 () {