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

Commit c8291f2

Browse files
Merge branch '4.x' into 6288-issues-in-contract-subscriptions
2 parents ee31c50 + 822f8c1 commit c8291f2

File tree

12 files changed

+205
-3
lines changed

12 files changed

+205
-3
lines changed

docs/docs/guides/02_web3_providers_guide/index.md

+22
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ await web3.eth.getBlockNumber();
111111
// ↳ 18849658n
112112
```
113113

114+
#### Websocket Providers will only terminate when closed
115+
116+
When connected to a WebSocket provider, the program will not automatically terminate after the code finishes running. This is to ensure the WebSocket connection remains open, allowing the program to continue listening for events.
117+
118+
The program will remain active until you explicitly disconnect from the WebSocket provider:
119+
120+
```ts
121+
const web3 = new Web3(wsUrl);
122+
// The program will keep running to listen for events.
123+
```
124+
125+
#### Terminating the Program
126+
127+
When you're ready to stop the program, you can manually disconnect from the WebSocket provider by calling the disconnect method. This will properly close the connection and terminate the program:
128+
129+
```ts
130+
const web3 = new Web3(wsUrl);
131+
// When you are ready to terminate your program
132+
web3.currentProvider?.disconnect();
133+
// The program will now terminate
134+
```
135+
114136
#### Configuring WebSocket Providers
115137

116138
The [`WebSocketProvider` constructor](/api/web3-providers-ws/class/WebSocketProvider#constructor) accepts two optional parameters that can be used to configure the behavior of the `WebSocketProvider`: the first parameter must be of type [`ClientRequestArgs`](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules__types_node_http_d_._http_.clientrequestargs.html) or of [`ClientOptions`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e5ee5eae6a592198e469ad9f412bab8d223fcbb6/types/ws/index.d.ts#L243) and the second parameter must be of type [`ReconnectOptions`](/api/web3/namespace/utils#ReconnectOptions).

docs/docs/guides/05_smart_contracts/tips_and_tricks.md

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ sidebar_label: 'Tips and Tricks'
99
📝 This article offers insights into **Smart Contracts** with helpful tips and tricks. If you have suggestions or questions, feel free to open an issue. We also welcome contributions through PRs.
1010
:::
1111

12+
## Ignoring Web3.js autofill gas prices
13+
14+
When interacting with methods in contracts, Web3.js will automatically fill the gas. If you are using metamask or a similar provider and would rather have a suggestion elsewhere, the `ignoreGasPricing` option enables you to send transactions or interact with contracts without having web3.js automatically fill in the gas estimate.
15+
16+
#### Contract example
17+
18+
```ts
19+
let contractDeployed: Contract<typeof BasicAbi>;
20+
// instantiate contract...
21+
contractDeployed.config.ignoreGasPricing = true;
22+
const receipt = await contractDeployed.methods.setValues(1, 'string value', true).send(sendOptions);
23+
```
24+
1225
## Calling Smart Contracts Methods with Parameter Overloading
1326

1427
### Overview of Function Overloading

docs/docs/guides/09_web3_config/index.md

+28
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ There is list of configuration params that can be set for modifying behavior of
2929
- [defaultMaxPriorityFeePerGas](/guides/web3_config/#defaultmaxpriorityfeepergas)
3030
- [customTransactionSchema](/guides/web3_config/#customTransactionSchema)
3131
- [defaultReturnFormat](/guides/web3_config/#defaultreturnformat)
32+
- [ignoreGasPricing](/guides/web3_config/#ignoreGasPricing)
3233

3334
## Global level Config
3435

@@ -477,3 +478,30 @@ export enum FMT_BYTES {
477478
UINT8ARRAY = 'BYTES_UINT8ARRAY',
478479
}
479480
```
481+
482+
### [ignoreGasPricing]
483+
484+
The `ignoreGasPricing` option enables you to send transactions or interact with contracts without having web3.js automatically fill in the gas estimate. This feature is particularly useful when you prefer to let wallets or providers handle gas estimation instead.
485+
486+
#### Send transaction example
487+
488+
```ts
489+
const web3 = new Web3(PROVIDER);
490+
web3.config.ignoreGasPricing = true; // when setting configurations for the web3 object, this will also apply to newly created contracts from the web3 object
491+
const transaction: TransactionWithToLocalWalletIndex = {
492+
from: tempAcc.address,
493+
to: '0x0000000000000000000000000000000000000000',
494+
value: BigInt(1),
495+
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
496+
};
497+
const receipt = await web3.eth.sendTransaction(transaction); // web3.js will not estimate gas now.
498+
```
499+
500+
#### Contract example
501+
502+
```ts
503+
let contractDeployed: Contract<typeof BasicAbi>;
504+
// instantiate contract...
505+
contractDeployed.config.ignoreGasPricing = true;
506+
const receipt = await contractDeployed.methods.setValues(1, 'string value', true).send(sendOptions);
507+
```

packages/web3-core/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,7 @@ Documentation:
240240
- Adds a new property (`customTransactionSchema`) to `Web3ConfigOptions`(#7227)
241241

242242
## [Unreleased]
243+
244+
### Added
245+
246+
- Added new property `ignoreGasPricing` to `Web3ConfigOptions`. If `ignoreGasPricing` is true, gasPrice will not be estimated (#7320)

packages/web3-core/src/web3_config.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface Web3ConfigOptions {
4848
defaultNetworkId?: Numbers;
4949
defaultChain: string;
5050
defaultHardfork: string;
51+
ignoreGasPricing: boolean;
5152

5253
defaultCommon?: Common;
5354
defaultTransactionType: Numbers;
@@ -104,6 +105,7 @@ export abstract class Web3Config
104105
transactionTypeParser: undefined,
105106
customTransactionSchema: undefined,
106107
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
108+
ignoreGasPricing: false,
107109
};
108110

109111
public constructor(options?: Partial<Web3ConfigOptions>) {
@@ -208,7 +210,7 @@ export abstract class Web3Config
208210
* - `"latest"` - String: The latest block (current head of the blockchain)
209211
* - `"pending"` - String: The currently mined block (including pending transactions)
210212
* - `"finalized"` - String: (For POS networks) The finalized block is one which has been accepted as canonical by greater than 2/3 of validators
211-
* - `"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.
213+
* - `"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.
212214
*/
213215
public set defaultBlock(val) {
214216
this._triggerConfigChange('defaultBlock', val);
@@ -485,6 +487,17 @@ export abstract class Web3Config
485487
this.config.defaultCommon = val;
486488
}
487489

490+
/**
491+
* Will get the ignoreGasPricing property. When true, the gasPrice, maxPriorityFeePerGas, and maxFeePerGas will not be autofilled in the transaction object.
492+
* Useful when you want wallets to handle gas pricing.
493+
*/
494+
public get ignoreGasPricing() {
495+
return this.config.ignoreGasPricing;
496+
}
497+
public set ignoreGasPricing(val) {
498+
this._triggerConfigChange('ignoreGasPricing', val);
499+
this.config.ignoreGasPricing = val;
500+
}
488501
public get defaultTransactionType() {
489502
return this.config.defaultTransactionType;
490503
}

packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports[`Web3Context getContextObject should return correct context object 1`] =
2424
"useSubscriptionWhenCheckingBlockTimeout": false,
2525
},
2626
"handleRevert": false,
27+
"ignoreGasPricing": false,
2728
"maxListenersWarningThreshold": 100,
2829
"transactionBlockTimeout": 50,
2930
"transactionBuilder": undefined,

packages/web3-core/test/unit/web3_config.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const defaultConfig = {
5050
transactionBuilder: undefined,
5151
transactionTypeParser: undefined,
5252
customTransactionSchema: undefined,
53+
ignoreGasPricing: false,
5354
};
5455
const setValue = {
5556
string: 'newValue',

packages/web3-eth-contract/test/integration/contract_methods.test.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,48 @@ describe('contract', () => {
9292
});
9393

9494
describe('send', () => {
95-
it('should returns a receipt', async () => {
95+
beforeEach(() => {
96+
contractDeployed.config.ignoreGasPricing = false;
97+
});
98+
it('should return a receipt', async () => {
99+
const receipt = await contractDeployed.methods
100+
.setValues(1, 'string value', true)
101+
.send(sendOptions);
102+
103+
expect(receipt.events).toBeUndefined();
104+
expect(receipt).toEqual(
105+
expect.objectContaining({
106+
// status: BigInt(1),
107+
transactionHash: expect.any(String),
108+
}),
109+
);
110+
111+
// To avoid issue with the `objectContaining` and `cypress` had to add
112+
// these expectations explicitly on each attribute
113+
expect(receipt.status).toEqual(BigInt(1));
114+
});
115+
116+
it('should return a receipt with ignoring gas config true', async () => {
117+
contractDeployed.config.ignoreGasPricing = true;
118+
const receipt = await contractDeployed.methods
119+
.setValues(1, 'string value', true)
120+
.send(sendOptions);
121+
122+
expect(receipt.events).toBeUndefined();
123+
expect(receipt).toEqual(
124+
expect.objectContaining({
125+
// status: BigInt(1),
126+
transactionHash: expect.any(String),
127+
}),
128+
);
129+
130+
// To avoid issue with the `objectContaining` and `cypress` had to add
131+
// these expectations explicitly on each attribute
132+
expect(receipt.status).toEqual(BigInt(1));
133+
});
134+
135+
it('should return a receipt with ignoring gas config false', async () => {
136+
contractDeployed.config.ignoreGasPricing = false;
96137
const receipt = await contractDeployed.methods
97138
.setValues(1, 'string value', true)
98139
.send(sendOptions);

packages/web3-eth/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,7 @@ Documentation:
284284
### Changed
285285

286286
- Allow `getEthereumjsTxDataFrom` to return additional fields that may be passed if using a `customTransactionSchema`.
287+
288+
### Added
289+
290+
- `populateGasPrice` function now checks `Web3Context.config.ignoreGasPricing`. If `ignoreGasPricing` is true, gasPrice will not be estimated (#7320)

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

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export class SendTxHelper<
163163
}): Promise<TxType> {
164164
let result = transactionFormatted;
165165
if (
166+
!this.web3Context.config.ignoreGasPricing &&
166167
!this.options?.ignoreGasPricing &&
167168
isNullish((transactionFormatted as Transaction).gasPrice) &&
168169
(isNullish((transaction as Transaction).maxPriorityFeePerGas) ||

packages/web3-eth/test/integration/web3_eth/send_transaction.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,34 @@ describe('Web3Eth.sendTransaction', () => {
199199
const minedTransactionData = await web3Eth.getTransaction(response.transactionHash);
200200
expect(minedTransactionData).toMatchObject(transaction);
201201
});
202+
it('should send a transaction while ignoring gas price successfully', async () => {
203+
const transaction: TransactionWithToLocalWalletIndex = {
204+
from: tempAcc.address,
205+
to: '0x0000000000000000000000000000000000000000',
206+
value: BigInt(1),
207+
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
208+
};
202209

210+
const localWeb3Eth = new Web3Eth(getSystemTestProvider());
211+
localWeb3Eth.config.ignoreGasPricing = true;
212+
const response = await localWeb3Eth.sendTransaction(transaction);
213+
expect(response.status).toBe(BigInt(1));
214+
await closeOpenConnection(localWeb3Eth);
215+
});
216+
it('should send a transaction with automated gas price successfully', async () => {
217+
const transaction: TransactionWithToLocalWalletIndex = {
218+
from: tempAcc.address,
219+
to: '0x0000000000000000000000000000000000000000',
220+
value: BigInt(1),
221+
data: '0x64edfbf0e2c706ba4a09595315c45355a341a576cc17f3a19f43ac1c02f814ee',
222+
};
223+
224+
const localWeb3Eth = new Web3Eth(getSystemTestProvider());
225+
localWeb3Eth.config.ignoreGasPricing = false;
226+
const response = await localWeb3Eth.sendTransaction(transaction);
227+
expect(response.status).toBe(BigInt(1));
228+
await closeOpenConnection(localWeb3Eth);
229+
});
203230
describe('Deploy and interact with contract', () => {
204231
let greeterContractAddress: string;
205232

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

+48-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ describe('sendTxHelper class', () => {
8989
let sendTxHelper: SendTxHelper<DataFormat>;
9090
let promiEvent: Web3PromiEvent<TransactionReceipt, Web3EventMap>;
9191
let web3Context: Web3Context<EthExecutionAPI>;
92-
beforeAll(() => {
92+
beforeEach(() => {
93+
jest.clearAllMocks();
9394
web3Context = new Web3Context<EthExecutionAPI>();
9495
promiEvent = new Web3PromiEvent<TransactionReceipt, Web3EventMap>(resolve => {
9596
resolve({} as unknown as TransactionReceipt);
@@ -267,4 +268,50 @@ describe('sendTxHelper class', () => {
267268
expect(utils.trySendTransaction).toHaveBeenCalled();
268269
expect(wallet.signTransaction).toHaveBeenCalledWith(receipt);
269270
});
271+
it('should not call getTransactionGasPricing when ignoreGasPricing is true', async () => {
272+
web3Context.config.ignoreGasPricing = true;
273+
const transaction = {
274+
from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
275+
input: '0x68656c6c6f21',
276+
nonce: '0x15',
277+
to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
278+
value: '0xf3dbb76162000',
279+
type: '0x0',
280+
chainId: '0x1',
281+
};
282+
const _sendTxHelper = new SendTxHelper({
283+
web3Context,
284+
promiEvent: promiEvent as PromiEvent,
285+
options: {},
286+
returnFormat: DEFAULT_RETURN_FORMAT,
287+
});
288+
await _sendTxHelper.populateGasPrice({
289+
transactionFormatted: transaction,
290+
transaction,
291+
});
292+
expect(getTransactionGasPricing).not.toHaveBeenCalled();
293+
});
294+
it('should call getTransactionGasPricing when ignoreGasPricing is false', async () => {
295+
web3Context.config.ignoreGasPricing = false;
296+
const transaction = {
297+
from: '0xa7d9ddbe1f17865597fbd27ec712455208b6b76d',
298+
input: '0x68656c6c6f21',
299+
nonce: '0x15',
300+
to: '0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb',
301+
value: '0xf3dbb76162000',
302+
type: '0x0',
303+
chainId: '0x1',
304+
};
305+
const _sendTxHelper = new SendTxHelper({
306+
web3Context,
307+
promiEvent: promiEvent as PromiEvent,
308+
options: {},
309+
returnFormat: DEFAULT_RETURN_FORMAT,
310+
});
311+
await _sendTxHelper.populateGasPrice({
312+
transactionFormatted: transaction,
313+
transaction,
314+
});
315+
expect(getTransactionGasPricing).toHaveBeenCalled();
316+
});
270317
});

0 commit comments

Comments
 (0)