Web3js教程

web3.js 查询平均gas价格

查询平均gas价格,可以使用 web3.eth.getGasPrice()函数。
示例
app.js
const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY') // YOUR_INFURA_API_KEY替换为你自己的key
// 根据最近几个区块,计算平均Gas价格
web3.eth.getGasPrice().then((result) => {
    console.log("wei: " + result)
    console.log("ether: " + web3.utils.fromWei(result, 'ether'))
})
运行app.js
$ node app
输出
wei: 1000000000
ether: 0.000000001
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4