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

feat(docs): Expand web3 config guide #7131

Merged
merged 58 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
a993700
add doc for `handleRevert`
mmyyrroonn Jun 24, 2024
46dc323
add doc for defaultAccount
mmyyrroonn Jun 24, 2024
6d9fcf8
add doc for `defaultBlock`
mmyyrroonn Jun 25, 2024
6c0f38c
add doc for transactionBlockTimeout
mmyyrroonn Jun 25, 2024
38ac6ed
add doc for transactionConfirmationBlocks
mmyyrroonn Jun 25, 2024
0fb431a
add doc for transactionPollingInterval
mmyyrroonn Jun 25, 2024
5744e79
add doc for transactionPollingTimeout
mmyyrroonn Jun 25, 2024
382ee1d
add doc for transactionReceiptPollingInterval and improve doc for tra…
mmyyrroonn Jun 25, 2024
58445a9
add doc for transactionSendTimeout
mmyyrroonn Jun 25, 2024
b23bb96
add doc for transactionConfirmationPollingInterval
mmyyrroonn Jun 25, 2024
a0adf0b
add doc for blockHeaderTimeout
mmyyrroonn Jun 26, 2024
973d9bb
add doc for maxListenersWarningThreshold
mmyyrroonn Jun 26, 2024
c50492b
add doc for contractDataInputFill
mmyyrroonn Jun 26, 2024
56c6152
add doc for defaultNetworkId
mmyyrroonn Jun 26, 2024
fbab925
add doc for defaultHardfork, defaultChain and defaultCommon
mmyyrroonn Jun 26, 2024
6787e8b
add doc for defaultTransactionType
blackmoshui Jun 26, 2024
8297909
add doc for defaultMaxPriorityFeePerGas
blackmoshui Jun 26, 2024
098a245
refine
mmyyrroonn Jun 27, 2024
c8d5ea0
add doc for `handleRevert`
mmyyrroonn Jun 24, 2024
1e3794c
add doc for defaultAccount
mmyyrroonn Jun 24, 2024
77931c9
add doc for `defaultBlock`
mmyyrroonn Jun 25, 2024
c618a81
add doc for transactionBlockTimeout
mmyyrroonn Jun 25, 2024
040cbbb
add doc for transactionConfirmationBlocks
mmyyrroonn Jun 25, 2024
86dbc63
add doc for transactionPollingInterval
mmyyrroonn Jun 25, 2024
2f512dc
add doc for transactionPollingTimeout
mmyyrroonn Jun 25, 2024
05a845b
add doc for transactionReceiptPollingInterval and improve doc for tra…
mmyyrroonn Jun 25, 2024
7d0f2f0
add doc for transactionSendTimeout
mmyyrroonn Jun 25, 2024
aeff438
add doc for transactionConfirmationPollingInterval
mmyyrroonn Jun 25, 2024
634751e
add doc for blockHeaderTimeout
mmyyrroonn Jun 26, 2024
4f13cfd
add doc for maxListenersWarningThreshold
mmyyrroonn Jun 26, 2024
1413f42
add doc for contractDataInputFill
mmyyrroonn Jun 26, 2024
9e901a6
add doc for defaultNetworkId
mmyyrroonn Jun 26, 2024
8d79edf
add doc for defaultHardfork, defaultChain and defaultCommon
mmyyrroonn Jun 26, 2024
b5c2a1d
add doc for defaultTransactionType
blackmoshui Jun 26, 2024
ca704f5
add doc for defaultMaxPriorityFeePerGas
blackmoshui Jun 26, 2024
168111c
refine
mmyyrroonn Jun 27, 2024
66772d5
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Jul 10, 2024
788ef32
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Jul 17, 2024
2324306
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
luu-alex Jul 17, 2024
4f19cc5
Merge branch 'fix-6805-expand-web3config-guide-doc' of github.com:mmy…
mmyyrroonn Jul 18, 2024
dee663a
fix semi colons
mmyyrroonn Jul 18, 2024
34dffab
emphasize the default value and use the original default value in the…
mmyyrroonn Jul 19, 2024
5ef8737
run deploy
luu-alex Jul 19, 2024
ed4d040
revert
luu-alex Jul 19, 2024
ac08d70
add link, fix pr comments
mmyyrroonn Jul 22, 2024
1b98de7
remove redundant global config example
mmyyrroonn Jul 22, 2024
75594a1
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
luu-alex Jul 22, 2024
7a56f4b
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Jul 23, 2024
5da96a8
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Jul 25, 2024
632a750
Apply suggestions from code review
mmyyrroonn Jul 29, 2024
d16b843
resolve review comments
mmyyrroonn Jul 29, 2024
91d52cf
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Jul 31, 2024
cc97e2a
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Aug 2, 2024
4a5a47d
Merge branch '4.x' into fix-6805-expand-web3config-guide-doc
mmyyrroonn Aug 5, 2024
b2d8639
Apply suggestions from code review
mmyyrroonn Aug 5, 2024
b5655d2
remove all example except for transactionPollingInterval, defaultRetu…
mmyyrroonn Aug 5, 2024
80e10db
Apply suggestions from code review
mmyyrroonn Aug 6, 2024
e2a2547
Update docs/docs/guides/web3_config/index.md
danforbes Aug 6, 2024
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
263 changes: 230 additions & 33 deletions docs/docs/guides/web3_config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ sidebar_label: 'Web3 Config Guide'

There is list of configuration params that can be set for modifying behavior of different functions in web3.js packages. Following is list of configuration options with details:

- [handleRevert]( /api/web3-core/class/Web3Config#handleRevert)
- [defaultAccount](/api/web3-core/class/Web3Config#defaultAccount)
- [defaultBlock](/api/web3-core/class/Web3Config#defaultBlock)
- [transactionBlockTimeout](/api/web3-core/class/Web3Config#transactionBlockTimeout)
- [transactionConfirmationBlocks](/api/web3-core/class/Web3Config#transactionConfirmationBlocks)
- [transactionPollingInterval](/api/web3-core/class/Web3Config#transactionPollingInterval)
- [transactionPollingTimeout](/api/web3-core/class/Web3Config#transactionPollingTimeout)
- [transactionReceiptPollingInterval](/api/web3-core/class/Web3Config#transactionReceiptPollingInterval)
- [transactionSendTimeout](/api/web3-core/class/Web3Config#transactionSendTimeout)
- [transactionConfirmationPollingInterval](/api/web3-core/class/Web3Config#transactionConfirmationPollingInterval)
- [blockHeaderTimeout](/api/web3-core/class/Web3Config#blockHeaderTimeout)
- [maxListenersWarningThreshold](/api/web3-core/class/Web3Config#maxListenersWarningThreshold)
- [contractDataInputFill](/api/web3-core/class/Web3Config#contractDataInputFill)
- [defaultNetworkId](/api/web3-core/class/Web3Config#defaultNetworkId)
- [defaultChain](/api/web3-core/class/Web3Config#defaultChain)
- [defaultHardfork](/api/web3-core/class/Web3Config#defaultHardfork)
- [defaultCommon](/api/web3-core/class/Web3Config#defaultCommon)
- [defaultTransactionType](/api/web3-core/class/Web3Config#defaultTransactionType)
- [defaultReturnFormat](/api/web3-core/class/Web3Config#defaultReturnFormat)
- [handleRevert](/guides/web3_config/#handlerevert)
- [defaultAccount](/guides/web3_config/#defaultaccount)
- [defaultBlock](/guides/web3_config/#defaultblock)
- [transactionBlockTimeout](/guides/web3_config/#transactionblocktimeout)
- [transactionConfirmationBlocks](/guides/web3_config/#transactionconfirmationblocks)
- [transactionPollingInterval](/guides/web3_config/#transactionpollinginterval)
- [transactionPollingTimeout](/guides/web3_config/#transactionpollingtimeout)
- [transactionReceiptPollingInterval](/guides/web3_config/#transactionreceiptpollinginterval)
- [transactionSendTimeout](/guides/web3_config/#transactionsendtimeout)
- [[Deprecated] transactionConfirmationPollingInterval](/guides/web3_config/#transactionconfirmationpollinginterval)
- [blockHeaderTimeout](/guides/web3_config/#blockheadertimeout)
- [maxListenersWarningThreshold](/guides/web3_config/#maxlistenerswarningthreshold)
- [contractDataInputFill](/guides/web3_config/#contractdatainputfill)
- [defaultNetworkId](/guides/web3_config/#defaultnetworkid)
- [defaultChain](/guides/web3_config/#defaultchain)
- [defaultHardfork](/guides/web3_config/#defaulthardfork)
- [defaultCommon](/guides/web3_config/#defaultcommon)
- [defaultTransactionType](/guides/web3_config/#defaulttransactiontype)
- [defaultMaxPriorityFeePerGas](/guides/web3_config/#defaultmaxpriorityfeepergas)
- [defaultReturnFormat](/guides/web3_config/#defaultreturnformat)

## Global level Config

Expand Down Expand Up @@ -201,44 +202,240 @@ console.log(web3.getContextObject().config)
*/
```

## Explanation of Configuration Parameters

### defaultReturnFormat
The `defaultReturnFormat` allows users to specify the format in which certain types of data should be returned by default. It is a configuration parameter that can be set at the global level and affects how data is returned across the entire library.
### [handleRevert](/api/web3-core/class/Web3Config#handleRevert)
The following methods will retrieve specific error types and error messages when `handleRevert` is set to `true`:
- [`web3.eth.sendTransaction()`](/api/web3-eth/function/sendTransaction);
- [`myContract.methods.myMethod().send()`](/libdocs/Contract#send);

The error type will be one of the following:
- [InvalidResponseError](/api/web3-errors/class/InvalidResponseError)
- [ContractExecutionError](/api/web3-errors/class/ContractExecutionError)
- [TransactionRevertWithCustomError](/api/web3-errors/class/TransactionRevertWithCustomError)
- [TransactionRevertedWithoutReasonError](/api/web3-errors/class/TransactionRevertedWithoutReasonError)
- [TransactionRevertInstructionError](/api/web3-errors/class/TransactionRevertInstructionError)
- [TransactionPollingTimeoutError](/api/web3-errors/class/TransactionPollingTimeoutError)

For example, the error message could be `TransactionRevertInstructionError('Returned error: invalid argument 0: json: cannot unmarshal invalid hex string into Go struct field TransactionArgs.data of type hexutil.Bytes')`. The `handleRevert` option is only supported for [`sendTransaction`](/api/web3-eth/function/sendTransaction) and not for [`sendSignedTransaction`](/api/web3-eth/function/sendSignedTransaction) for now.

The default value of `handleRevert` is `false`.

### [defaultAccount](/api/web3-core/class/Web3Config#defaultAccount)
The `defaultAccount` option is used as the default `from` property, if no `from` property is specified for the following methods:
- [`web3.eth.sendTransaction()`](/api/web3-eth/function/sendTransaction);
- [`web3.eth.call()`](/api/web3-eth/function/call);
- [`myContract.methods.myMethod().call()`](/libdocs/Contract#call);
- [`myContract.methods.myMethod().send()`](/libdocs/Contract#send);

The default value of `defaultAccount` is `undefined`.

### [defaultBlock](/api/web3-core/class/Web3Config#defaultBlock)
The following methods accept an optional `blockNumber` parameter, the `defaultBlock` option is used for these methods if no `blockNumber` parameter is provided.
- [`web3.eth.getBalance()`](/api/web3-eth/function/getBalance);
- [`web3.eth.getCode()`](/api/web3-eth/function/getCode);
- [`web3.eth.getTransactionCount()`](/api/web3-eth/function/getTransactionCount);
- [`web3.eth.getStorageAt()`](/api/web3-eth/function/getStorageAt);
- [`web3.eth.call()`](/api/web3-eth/function/call);
- [`myContract.methods.myMethod().call()`](/libdocs/Contract#call);

If a `blockNumber` parameter is provided to one of the above function calls, it will override this option.

The default value of `defaultBlock` is "latest".

#### All available choices for defaultBlock:
```ts
import { Web3, FMT_NUMBER, FMT_BYTES } from 'web3';
web3.defaultBlock = 20167235; // A block number
web3.defaultBlock = "earliest"; // The genesis block
web3.defaultBlock = "latest"; // The latest block (current head of the blockchain)
web3.defaultBlock = "pending"; // The block pending to be mined (including pending transactions)
web3.defaultBlock = "finalized"; // (For POS networks) The finalized block is one which has been accepted as canonical by greater than 2/3 of validators
web3.defaultBlock = "safe"; // (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.
```

web3.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
### [transactionBlockTimeout](/api/web3-core/class/Web3Config#transactionBlockTimeout)
This option defines the number of new blocks to wait for the **first confirmation**, otherwise the [`PromiEvent`](/api/web3/class/Web3PromiEvent) rejects with a timeout error.

The default value of `transactionBlockTimeout` is 50.

### [transactionConfirmationBlocks](/api/web3-core/class/Web3Config#transactionConfirmationBlocks)
This defines the number of blocks required for a transaction to be considered confirmed. Different chains have varying security considerations and requirements for confirmation block numbers.

The default value of `transactionConfirmationBlocks` is 24.

### [transactionPollingInterval](/api/web3-core/class/Web3Config#transactionPollingInterval)
This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network. Modifying this value can reduce the wait time for confirmations or decrease the number of network requests. Setting the `transactionPollingInterval` would also set [`transactionReceiptPollingInterval`](/guides/web3_config/#transactionreceiptpollinginterval) and [`transactionConfirmationPollingInterval`](/guides/web3_config/#transactionconfirmationpollinginterval) to the same value.

The default value of `transactionPollingInterval` is 1000 ms.

```ts
import { Web3 } from 'web3';

const web3 = new Web3('http://127.0.0.1:7545');

web3.transactionPollingInterval = 1000; // 1000 ms = 1 s

console.log(web3.getContextObject().config);
/* ↳
...
transactionPollingInterval: 1000,
transactionPollingTimeout: 750000,
transactionReceiptPollingInterval: 1000,
transactionSendTimeout: 750000,
transactionConfirmationPollingInterval: 1000,
...
*/
```
mmyyrroonn marked this conversation as resolved.
Show resolved Hide resolved

### [transactionPollingTimeout](/api/web3-core/class/Web3Config#transactionPollingTimeout)
This option defines the number of seconds Web3 will wait for a receipt which confirms that a transaction was mined by the network. It can be set based on the average transaction confirmation time on the network. Note: If the `transactionPollingTimeout` is exceeded, the transaction may still be pending.

The default value of `transactionPollingTimeout` is 750 seconds (12.5 minutes).

### [transactionReceiptPollingInterval](/api/web3-core/class/Web3Config#transactionReceiptPollingInterval)
This option defines the number of seconds between Web3 calls for a receipt which confirms that a transaction was mined by the network. Compared to [`transactionPollingInterval`](/guides/web3_config/#transactionpollinginterval), it takes higher precedence. When this value is set, it will be read first.

The default value of `transactionReceiptPollingInterval` is `undefined`.

### [transactionSendTimeout](/api/web3-core/class/Web3Config#transactionSendTimeout)
The `transactionSendTimeout` option is used to specify how long to wait for the network to return the sent transaction result. Note: If the RPC call times out, the transaction may still be pending or even mined by the network. It is recommended that the pending transactions be checked in such a case.

The default value of `transactionSendTimeout` is 750 seconds (12.5 minutes).

### [transactionConfirmationPollingInterval](/api/web3-core/class/Web3Config#transactionConfirmationPollingInterval)
The `transactionConfirmationPollingInterval` option is deprecated. Please use [`transactionReceiptPollingInterval`](/guides/web3_config/#transactionreceiptpollinginterval) or [`transactionPollingInterval`](/guides/web3_config/#transactionpollinginterval) instead.

### [blockHeaderTimeout](/api/web3-core/class/Web3Config#blockHeaderTimeout)
After sending a transaction, Web3 will listen for the appearance of new blocks and proceed with subsequent operations based on the transaction results within them. This option defines the number of seconds Web3 should wait for the appearance of new blocks before reverting to polling to fetch the transaction receipt.

The default value of `blockHeaderTimeout` is 10 seconds.

### [maxListenersWarningThreshold](/api/web3-core/class/Web3Config#maxListenersWarningThreshold)
The `maxListenersWarningThreshold` is used to set the `maxListeners` property in [`EventEmitter`](/api/web3-utils/class/EventEmitter).

The default value of `maxListenersWarningThreshold` is 100.

### [contractDataInputFill](/api/web3-core/class/Web3Config#contractDataInputFill)
The `contractDataInputFill` option allows users to specify whether the [`data`](/api/web3/namespace/types#data) or [`input`](/api/web3/namespace/types#input) property (or both properties) should be set to the hash of the method signature and encoded parameters. This will affect the contracts [`send`](/libdocs/Contract#send), [`call`](/libdocs/Contract#call) and [`estimateGas`](/libdocs/Contract#estimategas) methods.

The default value of `contractDataInputFill` is `data`.

#### All available choices for contractDataInputFill:
```ts
'data'
'input'
'both'
```

### [defaultNetworkId](/api/web3-core/class/Web3Config#defaultNetworkId)
Each network has its own [network ID](https://docs.goquorum.consensys.io/concepts/network-and-chain-id). The `defaultNetworkId` option allows users to set the default network ID. If this option is not set, Web3 will fetch the network ID with an RPC request.

The default value of `defaultNetworkId` is `undefined`.

### [defaultChain](/api/web3-core/class/Web3Config#defaultChain)
The `defaultChain` option is used to set the [`Common`](/api/web3-eth-contract/class/Contract#defaultCommon) `baseChain` property. The value of this option should be consistent with [`defaultCommon.baseChain`](/guides/web3_config/#defaultcommon) if both options are set.

The default value of `defaultChain` is `mainnet`.

#### All available choices for defaultChain:
```ts
'goerli'
'kovan'
'mainnet'
'rinkeby'
'ropsten'
'sepolia'
```

### [defaultHardfork](/api/web3-core/class/Web3Config#defaultHardfork)
The `defaultHardfork` option is used to set the [`Common`](/api/web3-eth-contract/class/Contract#defaultCommon) `hardfork` property. The value of this option should be consistent with [`defaultCommon.hardfork`](/guides/web3_config/#defaultcommon) if both options are set.

The default value of `defaultHardfork` is `london`.

#### All available choices for defaultHardFork:
```ts
'chainstart'
'frontier'
'homestead'
'dao'
'tangerineWhistle'
'spuriousDragon'
'byzantium'
'constantinople'
'petersburg'
'istanbul'
'muirGlacier'
'berlin'
'london'
'altair'
'arrowGlacier'
'grayGlacier'
'bellatrix'
'merge'
'capella'
'sharding'
```

### [defaultCommon](/api/web3-core/class/Web3Config#defaultCommon)
The `defaultCommon` option is used to set the [`defaultCommon`](/libdocs/Contract#defaultcommon) value for smart contract transactions. It should be consistent with the [`defaultHardfork`](/guides/web3_config/#defaulthardfork) and [`defaultChain`](/guides/web3_config/#defaultchain) options if they are set.

The default value of `defaultCommon` is `undefined`.

```ts
import { Web3, Hardfork } from 'web3';

const web3 = new Web3('http://127.0.0.1:7545');

web3.defaultHardfork = 'berlin'
web3.defaultChain = 'goerli'

web3.defaultCommon = {
baseChain: 'goerli',
hardfork: 'berlin' as Hardfork,
customChain: {
networkId: 1,
chainId: 1,
},
mmyyrroonn marked this conversation as resolved.
Show resolved Hide resolved
};

console.log(web3.getContextObject().config);
```
:::info
The `defaultReturnFormat` can be configured both globally and at the package level:

### [defaultTransactionType](/api/web3-core/class/Web3Config#defaultTransactionType)
The `defaultTransactionType` option is used to set the transaction type. Transactions with type 0x0 are legacy transactions that use the transaction format that existed before [typed transactions](https://ethereum.org/en/developers/docs/transactions/#typed-transaction-envelope) were introduced in [EIP-2718](https://eips.ethereum.org/EIPS/eip-2718). Transactions with type 0x1 are transactions introduced in [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930). Transactions with type 0x2 are transactions introduced in [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559), included in Ethereum's London fork.

The default value of `defaultTransactionType` is `0x02`.

### [defaultMaxPriorityFeePerGas](/api/web3-core/class/Web3Config#defaultMaxPriorityFeePerGas)
The `defaultMaxPriorityFeePerGas` option is used to set the [`defaultMaxPriorityFeePerGas`](/api/web3-eth-contract/class/Contract#defaultMaxPriorityFeePerGas) value for [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) smart contract transactions ([transaction type](#defaulttransactiontype) 0x2).

The default value of `defaultMaxPriorityFeePerGas` is 2500000000 (2.5gwei) in hexstring format.

### [defaultReturnFormat](/api/web3-core/class/Web3Config#defaultReturnFormat)
The `defaultReturnFormat` option allows users to specify the format in which certain types of data should be returned by default. It is a configuration parameter that can be set at the global level and affects how data is returned across the entire library.
```ts
import { Web3Eth, FMT_NUMBER, FMT_BYTES } from 'web3-eth';
import { Web3, FMT_NUMBER, FMT_BYTES } from 'web3';

const eth = new Web3Eth()
eth.defaultReturnFormat = {
web3.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
};

```
:::

#### All available choices for numeric data:
```ts
export enum FMT_NUMBER {
NUMBER = 'NUMBER_NUMBER',
HEX = 'NUMBER_HEX',
STR = 'NUMBER_STR',
BIGINT = 'NUMBER_BIGINT',
}
};
```
#### All available choices for bytes data:
```ts
export enum FMT_BYTES {
HEX = 'BYTES_HEX',
UINT8ARRAY = 'BYTES_UINT8ARRAY',
}
};
```
2 changes: 1 addition & 1 deletion packages/web3-core/src/web3_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export abstract class Web3Config
* The `contractDataInputFill` options property will allow you to set the hash of the method signature and encoded parameters to the property
* either `data`, `input` or both within your contract.
* This will affect the contracts send, call and estimateGas methods
* Default is `input`.
* Default is `data`.
*/
public get contractDataInputFill() {
return this.config.contractDataInputFill;
Expand Down
Loading