前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >web3.js配置指南

web3.js配置指南

作者头像
孟斯特
发布2024-05-17 18:48:33
670
发布2024-05-17 18:48:33
举报
文章被收录于专栏:code人生code人生

原文在这里[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时,有一个选项可以在全局级别修改上述任何配置参数,它将对所有包都可用。

代码语言:javascript
复制
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对象设置全局配置。

代码语言:javascript
复制
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(...)函数进行修改。

代码语言:javascript
复制
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包:

代码语言:javascript
复制
$ npm i web3-eth

可以通过在构造函数中传递来设置配置选项:

代码语言:javascript
复制
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(...)函数。

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
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',
 ...
*/

对于单独导入的包,可以使用相同的方法来获取当前的配置参数。

代码语言:javascript
复制
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允许用户指定某些类型的数据应默认以何种格式返回。这是一个可以在全局级别设置的配置参数,影响整个库中数据的返回方式。

代码语言:javascript
复制
import { Web3, FMT_NUMBER, FMT_BYTES } from 'web3';

web3.defaultReturnFormat = {
    number: FMT_NUMBER.BIGINT,
    bytes: FMT_BYTES.HEX,
};

INFO defaultReturnFormat既可以在全局范围内配置,也可以在包级别进行配置:

代码语言:javascript
复制
import { Web3Eth, FMT_NUMBER, FMT_BYTES } from 'web3-eth';

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

所有可用的数值数据选项:

代码语言:javascript
复制
export enum FMT_NUMBER {
    NUMBER = 'NUMBER_NUMBER',
    HEX = 'NUMBER_HEX',
    STR = 'NUMBER_STR',
    BIGINT = 'NUMBER_BIGINT',
}

所有可用的字节数据选项:

代码语言:javascript
复制
export enum FMT_BYTES {
    HEX = 'BYTES_HEX',
    UINT8ARRAY = 'BYTES_UINT8ARRAY',
}

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[21]进行许可,使用时请注明出处。 Author: mengbin[22] blog: mengbin[23] Github: mengbin92[24] cnblogs: 恋水无意[25] 腾讯云开发者社区:孟斯特[26]

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 孟斯特 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置项参数
  • 全局配置
  • 包级别配置
    • 在Web3实例下的单个包中设置配置
      • 在单独导入的包中设置配置
      • 获取当前配置
        • defaultReturnFormat
          • References
          相关产品与服务
          云开发 CloudBase
          云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档