Skip to content

Commit

Permalink
6801 Fill gas price when baseFeePerGas==='0x0' (#7050)
Browse files Browse the repository at this point in the history
* Fix gas price filling

* fix

* fix build

* fix lint

* add unit test

* add unit test

* changelog
  • Loading branch information
avkos authored May 22, 2024
1 parent e0fc158 commit f4e55bd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,10 @@ If there are any bugs, improvements, optimizations or any new feature proposal f

### Fixed

#### web3-eth

- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043)

#### web3-utils


Expand All @@ -2498,6 +2502,7 @@ If there are any bugs, improvements, optimizations or any new feature proposal f
#### web3-eth

- Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000)
- Changed functionality: For networks that returns `baseFeePerGas===0x0` fill `maxPriorityFeePerGas` and `maxFeePerGas` by `getGasPrice` method (#7050)

#### web3-rpc-methods

Expand Down
3 changes: 2 additions & 1 deletion packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ Documentation:
### Changed

- Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000)
- Changed functionality: For networks that returns `baseFeePerGas===0x0` fill `maxPriorityFeePerGas` and `maxFeePerGas` by `getGasPrice` method (#7050)

### Fixed

- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043)
- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043)
11 changes: 7 additions & 4 deletions packages/web3-eth/src/utils/get_transaction_gas_pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ async function getEip1559GasPricing<ReturnFormat extends DataFormat>(
web3Context: Web3Context<EthExecutionAPI>,
returnFormat: ReturnFormat,
): Promise<FormatType<{ maxPriorityFeePerGas?: Numbers; maxFeePerGas?: Numbers }, ReturnFormat>> {
const block = await getBlock(web3Context, web3Context.defaultBlock, false, returnFormat);

const block = await getBlock(web3Context, web3Context.defaultBlock, false, ETH_DATA_FORMAT);
if (isNullish(block.baseFeePerGas)) throw new Eip1559NotSupportedError();

if (!isNullish(transaction.gasPrice)) {
let gasPrice: Numbers | undefined;
if (isNullish(transaction.gasPrice) && BigInt(block.baseFeePerGas) === BigInt(0)) {
gasPrice = await getGasPrice(web3Context, returnFormat);
}
if (!isNullish(transaction.gasPrice) || !isNullish(gasPrice)) {
const convertedTransactionGasPrice = format(
{ format: 'uint' },
transaction.gasPrice as Numbers,
transaction.gasPrice ?? gasPrice,
returnFormat,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Web3Context } from 'web3-core';

import { FMT_BYTES, FMT_NUMBER } from 'web3-types';
import * as RpcMethodWrappers from '../../../src/rpc_method_wrappers';
import { getTransactionGasPricing } from '../../../src/utils/get_transaction_gas_pricing';

describe('getTransactionGasPricing', () => {
const web3Context = new Web3Context();

it('should use the call rpc wrapper', async () => {
const gasPrice = '0x123456';
const gasPriceSpy = jest
.spyOn(RpcMethodWrappers, 'getGasPrice')
.mockImplementation(async () => gasPrice);
const getBlockSpy = jest
.spyOn(RpcMethodWrappers, 'getBlock')
// @ts-expect-error only for testing purposes
.mockImplementation(async () => ({
baseFeePerGas: '0x0',
}));

const transaction = {
from: '0x4fec0a51024b13030d26e70904b066c6d41157a5',
to: '0x36361143b7e2c676f8ccd67743a89d26437f0529',
data: '0x819f48fe',
type: '0x2',
};

const res = await getTransactionGasPricing(transaction, web3Context, {
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
});

expect(getBlockSpy).toHaveBeenCalled();
expect(gasPriceSpy).toHaveBeenCalled();
expect(res).toEqual({
gasPrice: undefined,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
});
});
});

2 comments on commit f4e55bd

@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: f4e55bd Previous: e0fc158 Ratio
processingTx 9157 ops/sec (±3.53%) 9220 ops/sec (±3.39%) 1.01
processingContractDeploy 39854 ops/sec (±6.49%) 37127 ops/sec (±6.27%) 0.93
processingContractMethodSend 19031 ops/sec (±8.97%) 19169 ops/sec (±7.11%) 1.01
processingContractMethodCall 37579 ops/sec (±6.24%) 37740 ops/sec (±5.81%) 1.00
abiEncode 42781 ops/sec (±6.77%) 42093 ops/sec (±6.98%) 0.98
abiDecode 29806 ops/sec (±7.94%) 28578 ops/sec (±7.99%) 0.96
sign 1522 ops/sec (±3.29%) 1542 ops/sec (±0.87%) 1.01
verify 368 ops/sec (±0.73%) 360 ops/sec (±0.57%) 0.98

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: f4e55bd Previous: e0fc158 Ratio
processingTx 9252 ops/sec (±3.66%) 9220 ops/sec (±3.39%) 1.00
processingContractDeploy 40465 ops/sec (±5.98%) 37127 ops/sec (±6.27%) 0.92
processingContractMethodSend 20094 ops/sec (±7.18%) 19169 ops/sec (±7.11%) 0.95
processingContractMethodCall 40512 ops/sec (±6.70%) 37740 ops/sec (±5.81%) 0.93
abiEncode 45046 ops/sec (±7.60%) 42093 ops/sec (±6.98%) 0.93
abiDecode 30062 ops/sec (±7.63%) 28578 ops/sec (±7.99%) 0.95
sign 1545 ops/sec (±3.45%) 1542 ops/sec (±0.87%) 1.00
verify 381 ops/sec (±0.59%) 360 ops/sec (±0.57%) 0.94

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

Please sign in to comment.