From c0f0178ca3b39c729902e026ddea56685428b64f Mon Sep 17 00:00:00 2001 From: Saad Ahmed Siddiqui Date: Thu, 12 Sep 2024 11:48:55 +0200 Subject: [PATCH] fix: added ethereum transaction overrides to `getTransferTransaction()` and `getApprovalTransactions()` --- packages/evm/src/__test__/fungible.test.ts | 17 ++++++++++++++--- packages/evm/src/evmAssetTransfer.ts | 6 +++--- packages/evm/src/fungibleAssetTransfer.ts | 10 ++++++---- packages/evm/src/nonFungibleAssetTransfer.ts | 10 +++++++--- packages/evm/src/types.ts | 1 + packages/evm/src/utils/approveAndCheckFns.ts | 4 +++- packages/evm/src/utils/transaction.ts | 1 + 7 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/evm/src/__test__/fungible.test.ts b/packages/evm/src/__test__/fungible.test.ts index a41413336..f944e1d6d 100644 --- a/packages/evm/src/__test__/fungible.test.ts +++ b/packages/evm/src/__test__/fungible.test.ts @@ -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'; @@ -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() @@ -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'), diff --git a/packages/evm/src/evmAssetTransfer.ts b/packages/evm/src/evmAssetTransfer.ts index 6dbbab6d2..615164f6f 100644 --- a/packages/evm/src/evmAssetTransfer.ts +++ b/packages/evm/src/evmAssetTransfer.ts @@ -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'; @@ -19,8 +19,8 @@ import { createTransactionRequest } from './utils/transaction.js'; * TODO: Add Support for all */ interface IAssetTransfer { - getTransferTransaction(): Promise; - getApprovalTransactions(): Promise>; + getTransferTransaction(overrides?: ethers.Overrides): Promise; + getApprovalTransactions(overrides?: ethers.Overrides): Promise>; } /** diff --git a/packages/evm/src/fungibleAssetTransfer.ts b/packages/evm/src/fungibleAssetTransfer.ts index e08ad5855..aa67b065a 100644 --- a/packages/evm/src/fungibleAssetTransfer.ts +++ b/packages/evm/src/fungibleAssetTransfer.ts @@ -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'; import { AssetTransfer } from './evmAssetTransfer.js'; import type { @@ -126,7 +126,9 @@ class FungibleAssetTransfer extends AssetTransfer { * associated with fungible transfer * @returns {Promise>} */ - public async getApprovalTransactions(): Promise> { + public async getApprovalTransactions( + overrides?: ethers.Overrides, + ): Promise> { const provider = new Web3Provider(this.sourceNetworkProvider); const sourceDomainConfig = this.config.getDomainConfig(this.source); const bridge = Bridge__factory.connect(sourceDomainConfig.bridge, provider); @@ -149,13 +151,13 @@ class FungibleAssetTransfer extends AssetTransfer { const approvals: Array = []; 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)); diff --git a/packages/evm/src/nonFungibleAssetTransfer.ts b/packages/evm/src/nonFungibleAssetTransfer.ts index 5f74155f2..ab1285c76 100644 --- a/packages/evm/src/nonFungibleAssetTransfer.ts +++ b/packages/evm/src/nonFungibleAssetTransfer.ts @@ -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'; @@ -65,7 +65,9 @@ class NonFungibleAssetTransfer extends AssetTransfer { this.transferResource = resource; } - public async getApprovalTransactions(): Promise> { + public async getApprovalTransactions( + overrides?: ethers.Overrides, + ): Promise> { const approvalTransactions: Array = []; const provider = new providers.Web3Provider(this.sourceNetworkProvider); const sourceDomainConfig = this.config.getDomainConfig(this.source.caipId); @@ -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)); diff --git a/packages/evm/src/types.ts b/packages/evm/src/types.ts index e9b93af1b..6f7522d51 100644 --- a/packages/evm/src/types.ts +++ b/packages/evm/src/types.ts @@ -17,6 +17,7 @@ export interface TransactionRequest { value: bigint; data: string; gasLimit: bigint; + gasPrice?: bigint; } export type EvmFee = { diff --git a/packages/evm/src/utils/approveAndCheckFns.ts b/packages/evm/src/utils/approveAndCheckFns.ts index d459dacf8..9326e6653 100644 --- a/packages/evm/src/utils/approveAndCheckFns.ts +++ b/packages/evm/src/utils/approveAndCheckFns.ts @@ -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. @@ -59,10 +59,12 @@ export const approve = async ( tokenInstance: ERC20 | ERC721MinterBurnerPauser, spender: string, amountOrIdForApproval: string, + overrides?: ethers.Overrides, ): Promise => { const unsignedTx = await tokenInstance.populateTransaction.approve( spender, amountOrIdForApproval, + overrides, ); return unsignedTx; }; diff --git a/packages/evm/src/utils/transaction.ts b/packages/evm/src/utils/transaction.ts index 03d0deb8a..bab42151c 100644 --- a/packages/evm/src/utils/transaction.ts +++ b/packages/evm/src/utils/transaction.ts @@ -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; }