You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
#### 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 =newWeb3(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 =newWeb3(wsUrl);
131
+
// When you are ready to terminate your program
132
+
web3.currentProvider?.disconnect();
133
+
// The program will now terminate
134
+
```
135
+
114
136
#### Configuring WebSocket Providers
115
137
116
138
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).
Copy file name to clipboardexpand all lines: docs/docs/guides/05_smart_contracts/tips_and_tricks.md
+13
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,19 @@ sidebar_label: 'Tips and Tricks'
9
9
📝 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.
10
10
:::
11
11
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.
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 =newWeb3(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
@@ -208,7 +210,7 @@ export abstract class Web3Config
208
210
* - `"latest"` - String: The latest block (current head of the blockchain)
209
211
* - `"pending"` - String: The currently mined block (including pending transactions)
210
212
* - `"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.
212
214
*/
213
215
publicsetdefaultBlock(val){
214
216
this._triggerConfigChange('defaultBlock',val);
@@ -485,6 +487,17 @@ export abstract class Web3Config
485
487
this.config.defaultCommon=val;
486
488
}
487
489
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.
0 commit comments