From c353ba0af1a7eba9a57cc54f64993f6da6d99433 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Wed, 15 May 2024 15:48:18 -0400 Subject: [PATCH 1/7] add gas conditional if simple transaction --- packages/web3-eth/src/utils/send_tx_helper.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/src/utils/send_tx_helper.ts b/packages/web3-eth/src/utils/send_tx_helper.ts index 282e778e519..754a66aeabb 100644 --- a/packages/web3-eth/src/utils/send_tx_helper.ts +++ b/packages/web3-eth/src/utils/send_tx_helper.ts @@ -122,7 +122,14 @@ export class SendTxHelper< public async checkRevertBeforeSending(tx: TransactionCall) { if (this.options.checkRevertBeforeSending !== false) { - const reason = await getRevertReason(this.web3Context, tx, this.options.contractAbi); + let formatTx = tx; + if (tx.data === undefined && tx.gas === undefined) { // eth.call will run into an error if data isnt filled and gas is not defined, simple transaction so we fill it with 21000 + formatTx = { + ...tx, + gas: 21000 + } + } + const reason = await getRevertReason(this.web3Context, formatTx, this.options.contractAbi); if (reason !== undefined) { throw await getTransactionError( this.web3Context, From 3ad61bc80a59bbbc85215acc0c33ec84c1cabf2f Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 09:22:20 -0400 Subject: [PATCH 2/7] update --- packages/web3-eth/src/rpc_method_wrappers.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 20e832be892..9843b44d7b9 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -575,13 +575,12 @@ export function sendTransaction< }, ETH_DATA_FORMAT, ); - + try { transactionFormatted = await sendTxHelper.populateGasPrice({ transaction, transactionFormatted, }); - await sendTxHelper.checkRevertBeforeSending( transactionFormatted as TransactionCall, ); From 3048798648a1b90dc44d447fc5601cc143bda4f5 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 10:13:43 -0400 Subject: [PATCH 3/7] format and add changelog --- packages/web3-eth/CHANGELOG.md | 4 ++++ packages/web3-eth/src/rpc_method_wrappers.ts | 2 +- packages/web3-eth/src/utils/send_tx_helper.ts | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 5fb80eef663..9d9b67b29ca 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -242,3 +242,7 @@ Documentation: ### Changed - Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000) + +### Fixed + +- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043) \ No newline at end of file diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 9843b44d7b9..7cbf6801519 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -575,7 +575,7 @@ export function sendTransaction< }, ETH_DATA_FORMAT, ); - + try { transactionFormatted = await sendTxHelper.populateGasPrice({ transaction, diff --git a/packages/web3-eth/src/utils/send_tx_helper.ts b/packages/web3-eth/src/utils/send_tx_helper.ts index 754a66aeabb..84ed76b2b3b 100644 --- a/packages/web3-eth/src/utils/send_tx_helper.ts +++ b/packages/web3-eth/src/utils/send_tx_helper.ts @@ -123,7 +123,7 @@ export class SendTxHelper< public async checkRevertBeforeSending(tx: TransactionCall) { if (this.options.checkRevertBeforeSending !== false) { let formatTx = tx; - if (tx.data === undefined && tx.gas === undefined) { // eth.call will run into an error if data isnt filled and gas is not defined, simple transaction so we fill it with 21000 + if (tx.data === undefined && tx.input && tx.gas === undefined) { // eth.call runs into error if data isnt filled and gas is not defined, its a simple transaction so we fill it with 21000 formatTx = { ...tx, gas: 21000 From fe4ff9b51480380e6fbff3dab0114c7ee2272b85 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 10:14:20 -0400 Subject: [PATCH 4/7] format --- packages/web3-eth/src/rpc_method_wrappers.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 7cbf6801519..238fe162429 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -581,6 +581,7 @@ export function sendTransaction< transaction, transactionFormatted, }); + await sendTxHelper.checkRevertBeforeSending( transactionFormatted as TransactionCall, ); From 1eead18d6e6f6ea5daacb89959db3f05e5343f08 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 10:15:06 -0400 Subject: [PATCH 5/7] format --- packages/web3-eth/src/rpc_method_wrappers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth/src/rpc_method_wrappers.ts b/packages/web3-eth/src/rpc_method_wrappers.ts index 238fe162429..20e832be892 100644 --- a/packages/web3-eth/src/rpc_method_wrappers.ts +++ b/packages/web3-eth/src/rpc_method_wrappers.ts @@ -581,7 +581,7 @@ export function sendTransaction< transaction, transactionFormatted, }); - + await sendTxHelper.checkRevertBeforeSending( transactionFormatted as TransactionCall, ); From 6d0c60ca32c22642b351ac2f04a211d0203d8377 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 12:10:07 -0400 Subject: [PATCH 6/7] add test --- .../web3-eth/test/unit/send_tx_helper.test.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/web3-eth/test/unit/send_tx_helper.test.ts b/packages/web3-eth/test/unit/send_tx_helper.test.ts index 55a482395b8..e8a4977f3ae 100644 --- a/packages/web3-eth/test/unit/send_tx_helper.test.ts +++ b/packages/web3-eth/test/unit/send_tx_helper.test.ts @@ -21,6 +21,7 @@ import { JsonRpcResponse, TransactionReceipt, Web3BaseWalletAccount, + TransactionCall } from 'web3-types'; import { Web3Context, Web3EventMap, Web3PromiEvent } from 'web3-core'; import { @@ -151,6 +152,29 @@ describe('sendTxHelper class', () => { expect(f).toHaveBeenCalledWith(error); promiEvent.off('error', f); }); + + it('add gas to simple transaction in checkRevertBeforeSending', async () => { + const _sendTxHelper = new SendTxHelper({ + web3Context, + promiEvent: promiEvent as PromiEvent, + options: { + checkRevertBeforeSending: true, + }, + returnFormat: DEFAULT_RETURN_FORMAT, + }); + jest.spyOn(utils, 'getRevertReason').mockResolvedValue(undefined); + + const tx = {from:"0x"} as TransactionCall + + await _sendTxHelper.checkRevertBeforeSending(tx); + + const expectedTx = { + ...tx, + gas: 21000, + }; + expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx); + + }); it('emit handleError with handleRevert', async () => { const error = new ContractExecutionError({ code: 1, message: 'error' }); web3Context.handleRevert = true; From 37455ebbe11d347fcca843071eb8dc7ae09e01f6 Mon Sep 17 00:00:00 2001 From: Alex Luu Date: Thu, 16 May 2024 12:44:15 -0400 Subject: [PATCH 7/7] fix test --- packages/web3-eth/src/utils/send_tx_helper.ts | 2 +- packages/web3-eth/test/unit/send_tx_helper.test.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/web3-eth/src/utils/send_tx_helper.ts b/packages/web3-eth/src/utils/send_tx_helper.ts index 84ed76b2b3b..0037e1051fa 100644 --- a/packages/web3-eth/src/utils/send_tx_helper.ts +++ b/packages/web3-eth/src/utils/send_tx_helper.ts @@ -123,7 +123,7 @@ export class SendTxHelper< public async checkRevertBeforeSending(tx: TransactionCall) { if (this.options.checkRevertBeforeSending !== false) { let formatTx = tx; - if (tx.data === undefined && tx.input && tx.gas === undefined) { // eth.call runs into error if data isnt filled and gas is not defined, its a simple transaction so we fill it with 21000 + if (isNullish(tx.data) && isNullish(tx.input) && isNullish(tx.gas)) { // eth.call runs into error if data isnt filled and gas is not defined, its a simple transaction so we fill it with 21000 formatTx = { ...tx, gas: 21000 diff --git a/packages/web3-eth/test/unit/send_tx_helper.test.ts b/packages/web3-eth/test/unit/send_tx_helper.test.ts index e8a4977f3ae..6b40496f5a6 100644 --- a/packages/web3-eth/test/unit/send_tx_helper.test.ts +++ b/packages/web3-eth/test/unit/send_tx_helper.test.ts @@ -162,7 +162,6 @@ describe('sendTxHelper class', () => { }, returnFormat: DEFAULT_RETURN_FORMAT, }); - jest.spyOn(utils, 'getRevertReason').mockResolvedValue(undefined); const tx = {from:"0x"} as TransactionCall @@ -172,7 +171,7 @@ describe('sendTxHelper class', () => { ...tx, gas: 21000, }; - expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx); + expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx, undefined); }); it('emit handleError with handleRevert', async () => {