web3.js 查询最新区块号

要查询最新区块号,可以使用web3.eth.getBlockNumber()函数。

代码如下:

app.js

const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY') // YOUR_INFURA_API_KEY替换为你自己的key

// 查询最新区块
web3.eth.getBlockNumber().then(console.log)

运行app.js:

$ node app

输出

8718170

要查询最新区块,可以使用web3.eth.getBlock('latest')函数。代码如下:app.jsconst Web3 = require('web3')const web3 = new Web3('h ...