Skip to content

Commit

Permalink
fix: added ethereum transaction overrides to `getTransferTransaction(…
Browse files Browse the repository at this point in the history
…)` and `getApprovalTransactions()`
  • Loading branch information
saadahmsiddiqui committed Sep 12, 2024
1 parent 48e84d5 commit c0f0178
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
17 changes: 14 additions & 3 deletions packages/evm/src/__test__/fungible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { BigNumber } from 'ethers';
import { parseEther } from 'ethers/lib/utils.js';

import { createFungibleAssetTransfer } from '../fungibleAssetTransfer.js';
import type { TransactionRequest } from '../types.js';

import { ASSET_TRANSFER_PARAMS } from './constants.js';

Expand Down Expand Up @@ -313,8 +312,9 @@ describe('Fungible - Deposit', () => {
to: '',
value: BigInt(0),
data: '',
gasLimit: BigInt(0),
} as TransactionRequest),
gasLimit: BigNumber.from('1'),
gasPrice: BigNumber.from('1'),
}),
},
_resourceIDToHandlerAddress: jest
.fn()
Expand All @@ -334,6 +334,17 @@ describe('Fungible - Deposit', () => {
expect(depositTransaction).toBeTruthy();
});

it('should return deposit transaction with overrides', async () => {
const transfer = await createFungibleAssetTransfer(TRANSFER_PARAMS);
const depositTransaction = await transfer.getTransferTransaction({
gasLimit: 1n,
gasPrice: 1n,
});

expect(depositTransaction.gasLimit).toEqual(1n);
expect(depositTransaction.gasPrice).toEqual(1n);
});

it('should throw ERROR - Insufficient account balance - Percentage', async () => {
(BasicFeeHandler__factory.connect as jest.Mock).mockReturnValue({
feeHandlerType: jest.fn().mockResolvedValue('percentage'),
Expand Down
6 changes: 3 additions & 3 deletions packages/evm/src/evmAssetTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Config } from '@buildwithsygma/core';
import { isValidAddressForNetwork } from '@buildwithsygma/core';
import { Bridge__factory } from '@buildwithsygma/sygma-contracts';
import { Web3Provider } from '@ethersproject/providers';
import type { PayableOverrides } from 'ethers';
import type { ethers, PayableOverrides } from 'ethers';
import { constants, utils } from 'ethers';

import { EvmTransfer } from './evmTransfer.js';
Expand All @@ -19,8 +19,8 @@ import { createTransactionRequest } from './utils/transaction.js';
* TODO: Add Support for all
*/
interface IAssetTransfer {
getTransferTransaction(): Promise<TransactionRequest>;
getApprovalTransactions(): Promise<Array<TransactionRequest>>;
getTransferTransaction(overrides?: ethers.Overrides): Promise<TransactionRequest>;
getApprovalTransactions(overrides?: ethers.Overrides): Promise<Array<TransactionRequest>>;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions packages/evm/src/fungibleAssetTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EvmResource } from '@buildwithsygma/core';
import { Config, FeeHandlerType, ResourceType, SecurityModel } from '@buildwithsygma/core';
import { Bridge__factory, ERC20__factory } from '@buildwithsygma/sygma-contracts';
import { Web3Provider } from '@ethersproject/providers';
import { BigNumber, constants, type PopulatedTransaction, utils } from 'ethers';
import { BigNumber, constants, ethers, type PopulatedTransaction, utils } from 'ethers';

Check failure on line 5 in packages/evm/src/fungibleAssetTransfer.ts

View workflow job for this annotation

GitHub Actions / check (ubuntu-latest, 18)

Import "ethers" is only used as types

Check failure on line 5 in packages/evm/src/fungibleAssetTransfer.ts

View workflow job for this annotation

GitHub Actions / check (ubuntu-latest, 20)

Import "ethers" is only used as types

import { AssetTransfer } from './evmAssetTransfer.js';
import type {
Expand Down Expand Up @@ -126,7 +126,9 @@ class FungibleAssetTransfer extends AssetTransfer {
* associated with fungible transfer
* @returns {Promise<Array<TransactionRequest>>}
*/
public async getApprovalTransactions(): Promise<Array<TransactionRequest>> {
public async getApprovalTransactions(
overrides?: ethers.Overrides,
): Promise<Array<TransactionRequest>> {
const provider = new Web3Provider(this.sourceNetworkProvider);
const sourceDomainConfig = this.config.getDomainConfig(this.source);
const bridge = Bridge__factory.connect(sourceDomainConfig.bridge, provider);
Expand All @@ -149,13 +151,13 @@ class FungibleAssetTransfer extends AssetTransfer {
const approvals: Array<PopulatedTransaction> = [];
if (fee.type == FeeHandlerType.PERCENTAGE && feeHandlerAllowance.lt(fee.fee)) {
const approvalAmount = BigNumber.from(fee.fee).toString();
approvals.push(await approve(erc20, fee.handlerAddress, approvalAmount));
approvals.push(await approve(erc20, fee.handlerAddress, approvalAmount, overrides));
}

const transferAmount = BigNumber.from(this.adjustedAmount);
if (handlerAllowance.lt(transferAmount)) {
const approvalAmount = BigNumber.from(transferAmount).toString();
approvals.push(await approve(erc20, handlerAddress, approvalAmount));
approvals.push(await approve(erc20, handlerAddress, approvalAmount, overrides));
}

return approvals.map(approval => createTransactionRequest(approval));
Expand Down
10 changes: 7 additions & 3 deletions packages/evm/src/nonFungibleAssetTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Bridge__factory,
ERC721MinterBurnerPauser__factory,
} from '@buildwithsygma/sygma-contracts';
import type { PopulatedTransaction } from 'ethers';
import type { ethers, PopulatedTransaction } from 'ethers';
import { providers } from 'ethers';

import { AssetTransfer } from './evmAssetTransfer.js';
Expand Down Expand Up @@ -65,7 +65,9 @@ class NonFungibleAssetTransfer extends AssetTransfer {
this.transferResource = resource;
}

public async getApprovalTransactions(): Promise<Array<TransactionRequest>> {
public async getApprovalTransactions(
overrides?: ethers.Overrides,
): Promise<Array<TransactionRequest>> {
const approvalTransactions: Array<PopulatedTransaction> = [];
const provider = new providers.Web3Provider(this.sourceNetworkProvider);
const sourceDomainConfig = this.config.getDomainConfig(this.source.caipId);
Expand All @@ -78,7 +80,9 @@ class NonFungibleAssetTransfer extends AssetTransfer {
const isAlreadyApproved = await isApproved(tokenInstance, handlerAddress, Number(this.tokenId));

if (!isAlreadyApproved) {
approvalTransactions.push(await approve(tokenInstance, handlerAddress, this.tokenId));
approvalTransactions.push(
await approve(tokenInstance, handlerAddress, this.tokenId, overrides),
);
}

return approvalTransactions.map(transaction => createTransactionRequest(transaction));
Expand Down
1 change: 1 addition & 0 deletions packages/evm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TransactionRequest {
value: bigint;
data: string;
gasLimit: bigint;
gasPrice?: bigint;
}

export type EvmFee = {
Expand Down
4 changes: 3 additions & 1 deletion packages/evm/src/utils/approveAndCheckFns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ERC20, ERC721MinterBurnerPauser } from '@buildwithsygma/sygma-contracts';
import type { BigNumber, PopulatedTransaction } from 'ethers';
import type { BigNumber, ethers, PopulatedTransaction } from 'ethers';

/**
* Determines whether the specified token is approved for the provided handler address.
Expand Down Expand Up @@ -59,10 +59,12 @@ export const approve = async (
tokenInstance: ERC20 | ERC721MinterBurnerPauser,
spender: string,
amountOrIdForApproval: string,
overrides?: ethers.Overrides,
): Promise<PopulatedTransaction> => {
const unsignedTx = await tokenInstance.populateTransaction.approve(
spender,
amountOrIdForApproval,
overrides,
);
return unsignedTx;
};
1 change: 1 addition & 0 deletions packages/evm/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export function createTransactionRequest(transaction: PopulatedTransaction): Tra
value: transaction.value ? transaction.value.toBigInt() : undefined,
data: transaction.data,
gasLimit: transaction.gasLimit ? transaction.gasLimit.toBigInt() : undefined,
gasPrice: transaction.gasPrice ? transaction.gasPrice.toBigInt() : undefined,
} as TransactionRequest;
}

0 comments on commit c0f0178

Please sign in to comment.