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

web3.utils.sha3 – sha256哈希函数web3.utils.keccak256 – keccak256哈希函数web3.utils.randomHex – ...