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: fix erc20Approvals #311

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

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

Expand Down Expand Up @@ -133,7 +134,7 @@ export const executeDeposit = async (
overrides?: ethers.PayableOverrides,
): Promise<PopulatedTransaction> => {
const transactionSettings = {
value: feeData.fee,
value: feeData.type == FeeHandlerType.PERCENTAGE ? 0 : 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.DYNAMIC,
type: FeeHandlerType.PERCENTAGE,
handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e',
};
const approvals = await assetTransfer.buildApprovals(transfer, fee);

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({});

const fee = {
fee: BigNumber.from('100'),
type: FeeHandlerType.DYNAMIC,
type: FeeHandlerType.PERCENTAGE,
handlerAddress: '0xe495c86962DcA7208ECcF2020A273395AcE8da3e',
};
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 () {
Expand Down
Loading