Skip to content

Commit

Permalink
add gas conditional if simple transaction (#7043)
Browse files Browse the repository at this point in the history
* add gas conditional if simple transaction

* update

* format and add changelog

* format

* format

* add test

* fix test
  • Loading branch information
Alex committed May 21, 2024
1 parent 2f73aa5 commit 32b6b29
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 8 additions & 1 deletion packages/web3-eth/src/utils/send_tx_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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
}
}
const reason = await getRevertReason(this.web3Context, formatTx, this.options.contractAbi);
if (reason !== undefined) {
throw await getTransactionError<ReturnFormat>(
this.web3Context,
Expand Down
23 changes: 23 additions & 0 deletions packages/web3-eth/test/unit/send_tx_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
JsonRpcResponse,
TransactionReceipt,
Web3BaseWalletAccount,
TransactionCall
} from 'web3-types';
import { Web3Context, Web3EventMap, Web3PromiEvent } from 'web3-core';
import {
Expand Down Expand Up @@ -151,6 +152,28 @@ 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,
});

const tx = {from:"0x"} as TransactionCall

await _sendTxHelper.checkRevertBeforeSending(tx);

const expectedTx = {
...tx,
gas: 21000,
};
expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx, undefined);

});
it('emit handleError with handleRevert', async () => {
const error = new ContractExecutionError({ code: 1, message: 'error' });
web3Context.handleRevert = true;
Expand Down

2 comments on commit 32b6b29

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 32b6b29 Previous: 2f73aa5 Ratio
processingTx 9175 ops/sec (±3.74%) 9204 ops/sec (±3.98%) 1.00
processingContractDeploy 39773 ops/sec (±6.82%) 38039 ops/sec (±7.10%) 0.96
processingContractMethodSend 19448 ops/sec (±6.66%) 19205 ops/sec (±6.35%) 0.99
processingContractMethodCall 38565 ops/sec (±6.00%) 40094 ops/sec (±5.17%) 1.04
abiEncode 44084 ops/sec (±7.26%) 43919 ops/sec (±6.46%) 1.00
abiDecode 30319 ops/sec (±8.09%) 29754 ops/sec (±7.79%) 0.98
sign 1589 ops/sec (±0.87%) 1568 ops/sec (±0.52%) 0.99
verify 373 ops/sec (±0.58%) 372 ops/sec (±0.68%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 32b6b29 Previous: 2f73aa5 Ratio
processingTx 9127 ops/sec (±3.93%) 9204 ops/sec (±3.98%) 1.01
processingContractDeploy 38174 ops/sec (±6.85%) 38039 ops/sec (±7.10%) 1.00
processingContractMethodSend 19643 ops/sec (±6.67%) 19205 ops/sec (±6.35%) 0.98
processingContractMethodCall 39588 ops/sec (±5.50%) 40094 ops/sec (±5.17%) 1.01
abiEncode 44820 ops/sec (±6.55%) 43919 ops/sec (±6.46%) 0.98
abiDecode 30431 ops/sec (±7.47%) 29754 ops/sec (±7.79%) 0.98
sign 1586 ops/sec (±0.75%) 1568 ops/sec (±0.52%) 0.99
verify 369 ops/sec (±0.54%) 372 ops/sec (±0.68%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.