Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 32b6b29

Browse files
author
Alex
authored
add gas conditional if simple transaction (#7043)
* add gas conditional if simple transaction * update * format and add changelog * format * format * add test * fix test
1 parent 2f73aa5 commit 32b6b29

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

packages/web3-eth/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,7 @@ Documentation:
242242
### Changed
243243

244244
- Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000)
245+
246+
### Fixed
247+
248+
- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043)

packages/web3-eth/src/utils/send_tx_helper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ export class SendTxHelper<
122122

123123
public async checkRevertBeforeSending(tx: TransactionCall) {
124124
if (this.options.checkRevertBeforeSending !== false) {
125-
const reason = await getRevertReason(this.web3Context, tx, this.options.contractAbi);
125+
let formatTx = tx;
126+
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
127+
formatTx = {
128+
...tx,
129+
gas: 21000
130+
}
131+
}
132+
const reason = await getRevertReason(this.web3Context, formatTx, this.options.contractAbi);
126133
if (reason !== undefined) {
127134
throw await getTransactionError<ReturnFormat>(
128135
this.web3Context,

packages/web3-eth/test/unit/send_tx_helper.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
JsonRpcResponse,
2222
TransactionReceipt,
2323
Web3BaseWalletAccount,
24+
TransactionCall
2425
} from 'web3-types';
2526
import { Web3Context, Web3EventMap, Web3PromiEvent } from 'web3-core';
2627
import {
@@ -151,6 +152,28 @@ describe('sendTxHelper class', () => {
151152
expect(f).toHaveBeenCalledWith(error);
152153
promiEvent.off('error', f);
153154
});
155+
156+
it('add gas to simple transaction in checkRevertBeforeSending', async () => {
157+
const _sendTxHelper = new SendTxHelper({
158+
web3Context,
159+
promiEvent: promiEvent as PromiEvent,
160+
options: {
161+
checkRevertBeforeSending: true,
162+
},
163+
returnFormat: DEFAULT_RETURN_FORMAT,
164+
});
165+
166+
const tx = {from:"0x"} as TransactionCall
167+
168+
await _sendTxHelper.checkRevertBeforeSending(tx);
169+
170+
const expectedTx = {
171+
...tx,
172+
gas: 21000,
173+
};
174+
expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx, undefined);
175+
176+
});
154177
it('emit handleError with handleRevert', async () => {
155178
const error = new ContractExecutionError({ code: 1, message: 'error' });
156179
web3Context.handleRevert = true;

0 commit comments

Comments
 (0)