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

add ignoregaspricing config #7320

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 14 additions & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Web3ConfigOptions {
defaultNetworkId?: Numbers;
defaultChain: string;
defaultHardfork: string;
ignoreGasPricing: boolean;

defaultCommon?: Common;
defaultTransactionType: Numbers;
Expand Down Expand Up @@ -104,6 +105,7 @@ export abstract class Web3Config
transactionTypeParser: undefined,
customTransactionSchema: undefined,
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
ignoreGasPricing: false,
};

public constructor(options?: Partial<Web3ConfigOptions>) {
Expand Down Expand Up @@ -208,7 +210,7 @@ export abstract class Web3Config
* - `"latest"` - String: The latest block (current head of the blockchain)
* - `"pending"` - String: The currently mined block (including pending transactions)
* - `"finalized"` - String: (For POS networks) The finalized block is one which has been accepted as canonical by greater than 2/3 of validators
* - `"safe"` - String: (For POS networks) The safe head block is one which under normal network conditions, is expected to be included in the canonical chain. Under normal network conditions the safe head and the actual tip of the chain will be equivalent (with safe head trailing only by a few seconds). Safe heads will be less likely to be reorged than the proof of work network`s latest blocks.
* - `"safe"` - String: (For POS networks) The safe head block is one which under normal network conditions, is expected to be included in the canonical chain. Under normal network conditions the safe head and the actual tip of the chain will be equivalent (with safe head trailing only by a few seconds). Safe heads will be less likely to be reorged than the proof of work network's latest blocks.
*/
public set defaultBlock(val) {
this._triggerConfigChange('defaultBlock', val);
Expand Down Expand Up @@ -485,6 +487,17 @@ export abstract class Web3Config
this.config.defaultCommon = val;
}

/**
* Will get the ignoreGasPricing property. When true, the gasPrice, maxPriorityFeePerGas, and maxFeePerGas will not be autofilled in the transaction object.
* Useful when you want wallets to handle gas pricing.
*/
public get ignoreGasPricing() {
return this.config.ignoreGasPricing;
}
public set ignoreGasPricing(val) {
this._triggerConfigChange('ignoreGasPricing', val);
this.config.ignoreGasPricing = val;
}
public get defaultTransactionType() {
return this.config.defaultTransactionType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] =
"useSubscriptionWhenCheckingBlockTimeout": false,
},
"handleRevert": false,
"ignoreGasPricing": false,
"maxListenersWarningThreshold": 100,
"transactionBlockTimeout": 50,
"transactionBuilder": undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/web3-core/test/unit/web3_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const defaultConfig = {
transactionBuilder: undefined,
transactionTypeParser: undefined,
customTransactionSchema: undefined,
ignoreGasPricing: false,
};
const setValue = {
string: 'newValue',
Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth/src/utils/send_tx_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export class SendTxHelper<
}): Promise<TxType> {
let result = transactionFormatted;
if (
!this.web3Context.config.ignoreGasPricing &&
!this.options?.ignoreGasPricing &&
isNullish((transactionFormatted as Transaction).gasPrice) &&
(isNullish((transaction as Transaction).maxPriorityFeePerGas) ||
Expand Down
8 changes: 1 addition & 7 deletions packages/web3-eth/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ import {
TransactionWithSenderAPI,
ETH_DATA_FORMAT,
} from 'web3-types';
import {
isAddress,
isHexStrict,
isHexString32Bytes,
isNullish,
isUInt,
} from 'web3-validator';
import { isAddress, isHexStrict, isHexString32Bytes, isNullish, isUInt } from 'web3-validator';
import {
ChainMismatchError,
HardforkMismatchError,
Expand Down
49 changes: 48 additions & 1 deletion packages/web3-eth/test/unit/send_tx_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ describe('sendTxHelper class', () => {
let sendTxHelper: SendTxHelper<DataFormat>;
let promiEvent: Web3PromiEvent<TransactionReceipt, Web3EventMap>;
let web3Context: Web3Context<EthExecutionAPI>;
beforeAll(() => {
beforeEach(() => {
jest.clearAllMocks();
web3Context = new Web3Context<EthExecutionAPI>();
promiEvent = new Web3PromiEvent<TransactionReceipt, Web3EventMap>(resolve => {
resolve({} as unknown as TransactionReceipt);
Expand Down Expand Up @@ -267,4 +268,50 @@ describe('sendTxHelper class', () => {
expect(utils.trySendTransaction).toHaveBeenCalled();
expect(wallet.signTransaction).toHaveBeenCalledWith(receipt);
});
it('should not call getTransactionGasPricing when ignoreGasPricing is true', async () => {
web3Context.config.ignoreGasPricing = true;
const transaction = {
from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
input: '0x68656c6c6f21',
nonce: '0x15',
to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
value: '0xf3dbb76162000',
type: '0x0',
chainId: '0x1',
};
const _sendTxHelper = new SendTxHelper({
web3Context,
promiEvent: promiEvent as PromiEvent,
options: {},
returnFormat: DEFAULT_RETURN_FORMAT,
});
await _sendTxHelper.populateGasPrice({
transactionFormatted: transaction,
transaction,
});
expect(getTransactionGasPricing).not.toHaveBeenCalled();
});
it('should call getTransactionGasPricing when ignoreGasPricing is false', async () => {
web3Context.config.ignoreGasPricing = false;
const transaction = {
from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
input: '0x68656c6c6f21',
nonce: '0x15',
to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
value: '0xf3dbb76162000',
type: '0x0',
chainId: '0x1',
};
const _sendTxHelper = new SendTxHelper({
web3Context,
promiEvent: promiEvent as PromiEvent,
options: {},
returnFormat: DEFAULT_RETURN_FORMAT,
});
await _sendTxHelper.populateGasPrice({
transactionFormatted: transaction,
transaction,
});
expect(getTransactionGasPricing).toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions packages/web3-types/src/eth_contract_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,8 @@ export interface ContractOptions {
* The max fee per gas to use for transactions.
*/
maxFeePerGas?: Uint;
/**
* Ignore gas price, turn on for metamask suggestion fee
*/
ignoreGasPricing?: boolean;
}
Loading