原文在这里[1]
配置项参数
以下是一个配置参数列表,可以设置用于修改web3.js包中不同函数行为的参数。以下是配置选项的详细列表:
•handleRevert[2]•defaultAccount[3]•defaultBlock[4]•transactionBlockTimeout[5]•transactionConfirmationBlocks[6]•transactionPollingInterval[7]•transactionPollingTimeout[8]•transactionReceiptPollingInterval[9]•transactionSendTimeout[10]•transactionConfirmationPollingInterval[11]•blockHeaderTimeout[12]•maxListenersWarningThreshold[13]•contractDataInputFill[14]•defaultNetworkId[15]•defaultChain[16]•defaultHardfork[17]•defaultCommon[18]•defaultTransactionType[19]•defaultReturnFormat[20]
全局配置
在实例化Web3时,有一个选项可以在全局级别修改上述任何配置参数,它将对所有包都可用。
import { Web3 } from 'web3';
const web3 = new Web3({
provider: 'https://mainnet.infura.io/v3/YOURID',
config: {
defaultTransactionType: '0x0',
},
});
//now default transaction type will be 0x0 so using following function in eth will send type 0x0 transaction
web3.eth
.sendTransaction({
from: '0x18532dF2Ab835d4E9D07a8b9B759bf5F8f890f49',
to: '0xB2f70d8965e754cc07D343a9b5332876D3070155',
value: 100,
gasLimit: 21000,
})
.then((res) => console.log(res));
对于高级用户:也可以使用Web3Context
对象设置全局配置。
import { Web3, Web3Context } from 'web3';
const context = new Web3Context('http://127.0.0.1:7545');
context.setConfig({ defaultTransactionType: '0x0' });
const web3 = new Web3(context);
//it will not default to 0x0 type transactions
web3.eth.sendTransaction({
from: '0x18532dF2Ab835d4E9D07a8b9B759bf5F8f890f49',
to: '0x018e221145dE7cefAD09BD53F41c11A918Bf1Cb7',
value: 100,
gasLimit: 21000
}).then(res => console.log(res));
包级别配置
在Web3实例下的单个包中设置配置
一些影响选定包的配置选项可以使用setConfig(...)
函数进行修改。
import { Web3 } from 'web3';
const web3 = new Web3('https://mainnet.infura.io/v3/YOURID');
web3.eth.setConfig({ defaultTransactionType: '0x0'});
web3.eth
.sendTransaction({
from: '0x18532dF2Ab835d4E9D07a8b9B759bf5F8f890f49',
to: '0xB2f70d8965e754cc07D343a9b5332876D3070155',
value: 100,
gasLimit: 21000,
})
.then((res) => console.log(res));
在单独导入的包中设置配置
如果是导入单个包而不是整个web3.js,那么可以通过在构造函数中传递配置或使用setConfig(...)
函数来设置配置参数:
例如,如果只使用以下方式安装了web3Eth包:
$ npm i web3-eth
可以通过在构造函数中传递来设置配置选项:
import { Web3Eth } from 'web3-eth';
const web3EthObj = new Web3Eth({
provider: 'http://127.0.0.1:7545',
config: {
defaultTransactionType: 0x0,
},
});
web3EthObj
.sendTransaction({
from: '0x18532dF2Ab835d4E9D07a8b9B759bf5F8f890f49',
to: '0x018e221145dE7cefAD09BD53F41c11A918Bf1Cb7',
value: 100,
gasLimit: 21000,
})
.then((res) => console.log(res));
为单独导入的包设置配置的另一种方式是使用setConfig(...)
函数。
import { Web3Eth } from 'web3-eth';
const web3EthObj = new Web3Eth('http://127.0.0.1:7545');
web3EthObj.setConfig({ defaultTransactionType: 0x0 });
web3EthObj
.sendTransaction({
from: '0x18532dF2Ab835d4E9D07a8b9B759bf5F8f890f49',
to: '0x018e221145dE7cefAD09BD53F41c11A918Bf1Cb7',
value: 100,
gasLimit: 21000,
})
.then((res) => console.log(res));
获取当前配置
要获取当前配置参数的列表,可以使用getContextObject().config
,如下所示:
import { Web3 } from 'web3';
const web3 = new Web3('http://127.0.0.1:7545');
console.log(web3.getContextObject().config)
/* ↳
handleRevert: false,
defaultAccount: undefined,
defaultBlock: 'latest',
transactionBlockTimeout: 50,
transactionConfirmationBlocks: 24,
transactionPollingInterval: 1000,
transactionPollingTimeout: 750000,
transactionReceiptPollingInterval: undefined,
transactionSendTimeout: 750000,
transactionConfirmationPollingInterval: undefined,
blockHeaderTimeout: 10,
maxListenersWarningThreshold: 100,
contractDataInputFill: 'input',
defaultNetworkId: undefined,
defaultChain: 'mainnet',
defaultHardfork: 'london',
defaultCommon: undefined,
defaultTransactionType: '0x2',
defaultMaxPriorityFeePerGas: '0x9502f900',
...
*/
对于单独导入的包,可以使用相同的方法来获取当前的配置参数。
import { Web3Eth } from 'web3';
const web3 = new Web3Eth('http://127.0.0.1:7545');
console.log(web3.getContextObject().config)
/* ↳
handleRevert: false,
defaultAccount: undefined,
defaultBlock: 'latest',
transactionBlockTimeout: 50,
transactionConfirmationBlocks: 24,
transactionPollingInterval: 1000,
transactionPollingTimeout: 750000,
...
*/
defaultReturnFormat
defaultReturnFormat
允许用户指定某些类型的数据应默认以何种格式返回。这是一个可以在全局级别设置的配置参数,影响整个库中数据的返回方式。
import { Web3, FMT_NUMBER, FMT_BYTES } from 'web3';
web3.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
};
INFO defaultReturnFormat既可以在全局范围内配置,也可以在包级别进行配置:
import { Web3Eth, FMT_NUMBER, FMT_BYTES } from 'web3-eth';
const eth = new Web3Eth()
eth.defaultReturnFormat = {
number: FMT_NUMBER.BIGINT,
bytes: FMT_BYTES.HEX,
};
所有可用的数值数据选项:
export enum FMT_NUMBER {
NUMBER = 'NUMBER_NUMBER',
HEX = 'NUMBER_HEX',
STR = 'NUMBER_STR',
BIGINT = 'NUMBER_BIGINT',
}
所有可用的字节数据选项:
export enum FMT_BYTES {
HEX = 'BYTES_HEX',
UINT8ARRAY = 'BYTES_UINT8ARRAY',
}
References
[1]
这里: https://docs.web3js.org/guides/web3_config/
[2]
handleRevert: https://docs.web3js.org/api/web3-core/class/Web3Config#handleRevert
[3]
defaultAccount: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultAccount
[4]
defaultBlock: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultBlock
[5]
transactionBlockTimeout: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionBlockTimeout
[6]
transactionConfirmationBlocks: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionConfirmationBlocks
[7]
transactionPollingInterval: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionPollingInterval
[8]
transactionPollingTimeout: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionPollingTimeout
[9]
transactionReceiptPollingInterval: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionReceiptPollingInterval
[10]
transactionSendTimeout: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionSendTimeout
[11]
transactionConfirmationPollingInterval: https://docs.web3js.org/api/web3-core/class/Web3Config#transactionConfirmationPollingInterval
[12]
blockHeaderTimeout: https://docs.web3js.org/api/web3-core/class/Web3Config#blockHeaderTimeout
[13]
maxListenersWarningThreshold: https://docs.web3js.org/api/web3-core/class/Web3Config#maxListenersWarningThreshold
[14]
contractDataInputFill: https://docs.web3js.org/api/web3-core/class/Web3Config#contractDataInputFill
[15]
defaultNetworkId: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultNetworkId
[16]
defaultChain: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultChain
[17]
defaultHardfork: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultHardfork
[18]
defaultCommon: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultCommon
[19]
defaultTransactionType: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultTransactionType
[20]
defaultReturnFormat: https://docs.web3js.org/api/web3-core/class/Web3Config#defaultReturnFormat
[21]
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
[22]
mengbin: mengbin1992@outlook.com
[23]
mengbin: https://mengbin.top
[24]
mengbin92: https://mengbin92.github.io/
[25]
恋水无意: https://www.cnblogs.com/lianshuiwuyi/
[26]
孟斯特: https://cloud.tencent.com/developer/user/6649301