Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6801 Fill gas price when baseFeePerGas==='0x0' #7050

Merged
merged 9 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
avkos marked this conversation as resolved.
Show resolved Hide resolved
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,
});
});
});
Loading