web3.js 查询最新区块

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

代码如下:

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.getBlock('latest').then(console.log)

运行app.js:

$ node app

输出

{ difficulty: '2456340185966952',
  extraData: '0x5050594520737061726b706f6f6c2d6574682d636e2d687a347033',
  gasLimit: 9924577,
  gasUsed: 9922914,
  hash:
   '0x25d9f9dd736ebaac3c99cb0edb5df22745c354d208c6e2b5b43f426f754adfbe',
  logsBloom:
   '0x0ce1945692a7b01c22c20e81306988264040c59cd0e0169c6b680452540943200c1a7123950308c8803ac839190d53404a20145a0c0491c321e2c25a002412b11c2469640eb0055b0dcd5b690a20712c22042a5e805d89488248510e9a40408839306386aecf00105a2d618240d608c48839121510a60544d10ce134a1c1043844584844b6cdc00bf4045cab9c90a4b61401c34d114c40000b80548c8d1a2c4f9b0848473043636020cc70b56984d7aa884e821216572a4474266aa6180b1e1f744e8c3e411534008064c011167616483904b140c00ab4a75821068b10e0e422adb675517d800304fe4018b80712a984b6208806331622442e919f4c0f5410f1',
  miner: '0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c',
  mixHash:
   '0xe1cf50b6eacd6bbf7ce75f52b4607a4d2d1cdf5bc40e510bb533ed70a68982ce',
  nonce: '0x12b94e2804b40bf0',
  number: 8718227,
  parentHash:
   '0xfa49033887723e949bc0e574c201efbb06a36b2da845b531c622b54aa29e9c9a',
  receiptsRoot:
   '0x0e89e46ec24648e6162142ccfbd32757285b84fdfa8bdcd9d6bed095a6d5113a',
  sha3Uncles:
   '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  size: 39431,
  stateRoot:
   '0x358b86a6c724f4452d5685fdd8952650e90668bc195db1cbad8250f5cb98baec',
  timestamp: 1570767424,
  totalDifficulty: '12321314566668728607349',
  transactions:
   [ '0x39efacad04a436b8f4b85eb0662a75515b49b2e4f0ef561174af61a85e7f43a7',
     '0x29490573fe0ffaa74d82cd20525731af502f67c01e0c3a398b4c63d35b71404d',
     '0x7deb65748688654535207f58b7314fc12e953f40bfa37b5d037c1b78d3c6a94d',
     '0x8692fc311620a3724193a056dde964a5b9906eadf7b2481650559b073ed9ff82',
     '0x2e3b0a64b85560f8e9e7e40fee2eed7d274bc073a8453477700f4d72cf71f5ec',
     '0xd49bf6437d4a8cf95c2ff962fb25297db7c063d4eff7f47d33788bc3f50cc5a3',
     '0x67d3ab58866e6bf9ccbc453476dd0e3870f3efa4ccde28016c1e18f1145dfdf6',
     '0xb633acf36d94207ddc1a66e46202789de2fc8dc17bac441d082018f19fc32aeb',
     '0xd23e5010c20c3080aaa524c6aa5a5cd826fe97f00c926918cdaafb649319ac6c',
     '0xcb7397f32c089391a2ff623ae1f32b19df87c5bb5a7951e21eebaaec33e72da0',
     '0x927f05a8281a6125343d3ce451d36f670a95620c069ea3f2ecde0e3acd4e9f7e',
     '0x916a0a63977206d0cffc7b5c0ce068ca82ef5644398db05f3f388a0829b441e7',
     '0x9bb4a0ff5b48c760a8aa669756e17fd7382741249902d8533c92b06dca36455a',
     '0x7f7c66c46525a8fe4d68bfa85736ace83d3f71dd75a6930ad3e686934c1d7acd',
     '0xe6deb0669e3f11a769d685b832a4a91b12bc8faa37538d32d2cfa831c3cc2191',
     '0x756c73322db6dce065a161dafb305523cfee153ca25cfcb2ebecbe618ea2b7ee',
     '0x8e20c2dee70eaf4d3e0c44fb7d12385174e85cc477bacb5ea594ec79e8a2605e',
     '0x8dae2b28e2f2686f9ea1cb7f51bd7b28415a440d5d7b87b975ec6aec89cc8b4f',
     '0x4632300769e52b4c642e6c2cb4004d5df99c856e5e75fe74ab403c5b0e5a6da4',
     '0xe991aefe3f5c058209783f3305f56e5690465da19c812beeaf88fd85b18a4335',
     '0x92a8e40a4db5b3bf5f69acdf4a5529ecdd8b99d5fbe57c0e10cf806b046557d5',
     '0x1557d450acca12915ed5b9e251fad3e56240aaf81b2ec1b2f028a631031e1119',
     '0x75d1f56239c02ce08a416aa909b8cbfe5444e5befe695e1736ed7120604d2780',
     '0xb852c56f163bc7e542891ecec6232ca43c2f7aa9e9188c8d322d850a71adc32c',
     '0x8c6586aa3d4a2747123e769a2e39ad02f14c6eb06ee8be4dbdc944c01bcfae0e',
     '0xa9c06774e7d1a588d247f486a72dd9a6d7c7915bbd56f607f5ba90432b66e52a',
     '0xfe7072d20af6c8cd84d18f2b3c08a025dc134d3fbf366564a6c62ba56d0452bb',
     '0xe93f4c311d1798ea724e92b854d12323ec5d86386d8f9480a6ceb9db1772d1d2',
     '0x5debd076e2ff562602e2e6b57cdadbe0eae5365bd29306216ccbf7d7e8db6453',
     '0xd1ec0101c3127624f43a89a45489fd45d0b5c60c895f85ccbbfeff5aac3a7187',
     '0xdf883fb1ace7a964e7df3748141c4c487170e0e3b4caed6e035ce8e84c469ad8',
     '0x1ea899fa5b29894a8d944f20a3447aeacfe8548570f73b99bc9048ce861c691a',
     '0xed27234211d83738906abdea68cc87c585b26e51421e5ef832d1460b5d64032a',
     '0xf6d5e08201413773e67aa138ffd1a520e2be9f02369f15fa2a979296320c2f87',
     '0xf1bb650eba861bc0f706b084666583387efbdf43058f209d57f9641ffe3ace2c',
     '0xfe86895aa13707e00e8386379b0778e7beb347ce5930fa215017f3eee21e5bc3',
     '0xb16a95077bf1b896c69fd83973ead32fee368a9cc11d665eca48a43ab74ee5bc',
     '0x59f29fdc2916133d4ed7087dab46d78a3ba2f1f8bc7a13282f610f91415b1b60',
     '0xba7d360ca88443f432ea5ac47ecfc7605a14e4f0775dfef81a1d8e4ec8626b43',
     '0xc56618b59097b88d7703846d9bcb8b17e19a97bff62489b8e51b9dbb86ccec37',
     '0x9b4992780c7542def64aff4f7f4e78dbd34ed7764c7009796b29f41c6592eb43',
     '0x097541da69b56888f558aed7f894d6df5591fb37ab0d8cc625bf0531e13422eb',
     '0xb856ab21c481d266e0ecb430fbd4f0a1b79f0c2a4326e690022a951b04775402',
     '0xedf6fd6b7e1b1aea73c1b617d493068bdf292b2e212cfee8105d279eea681095',
     '0x269ec016518a709ee49b9d6b942ba5d0237bf033406fb51006c2c58e94122af1',
     '0x425bc0c6dd22646de1853223191c076540adc8e0084c232bf43a993be5eb4ecd',
     '0xf10ec5def0e2367523adc34aeb09f2fa3379d09474eaf7fd45625e86bb1d796c',
     '0x8702bff6f5e9925af880a4cba8064ce5172b6b81f2a01c67a5cff759e17859d0',
     '0x7c1c8978ea239dfec18f59505d10e369f789a7990b1da5ed2f6c2a2fe01610e3',
     '0x83290322ce59dd4ce5abe67abf36684eca0c7930e8e52eba92f558cc04de3e26',
     '0x7a5996311a6e663c6a8d3fd74c107354546d0f80936c7212b147d3f0f32c32e0',
     '0x2fa85b5aec4ec08217d1075b7f81095d3594b35d2668092f6360bd848178741d',
     '0x0d3589487fe8e5dd65f79cf6bf341ac34ef58a566996e0339b346a4672763217',
     '0x9e1485bf08a52934a35a5a7cbe17a57f814f6d63dbe99cb2bc2a9c20942569b5',
     '0x4a4fc81674ce63986eb962610319abf9c010e57aaadab01fffc1681f6fdd9b9c',
     '0x16ca7a4d4e933e5651cde6069cf3e5629bb96e3814b90e4708ea99f9dbf1dab2',
     '0x95f743211326746a36ed0695759114a5c32e4399ec21f40146a41d68d81b0f4b',
     '0x51de976cb24cb30628fb56fe4a2b58950f3aac2237c1a0e2242ed8a909b5ff3a',
     '0x55b2df315d1e523c8306cb9a87b9b8a5c8babe3192576eb4ef34c6a7a899db72',
     '0x4d778643eb548e625abdfec1a55059567f0ec7802c01595eb8baa23f2f194093',
     '0xec2fabf11c5af506d23c862eeb1697532f654684ccd8929b061546b284533bbe',
     '0xd4ab36db7ce1b0358371180e3ee8e4f621d412444534bd0f243b2f84e365c2a3',
     '0x7e6e7f08aff893fec7f96504ebd0ea0f00e2d2b1965b2627c07862ee3c00f04d',
     '0x67a6fb9e48ad0538e47cbdafaca98eda9ddb26792d8a81ceb000c5f807b303b6',
     '0x278f66cd176617781b39382a801fd367c527e14a261f81aff8ea4532a103ac1b',
     '0xb0e22a69896651950b9e19a2b7011aeacba1d2d404329bd7c255386005b67a51',
     '0xfb4aa6caae48229ffce434ef4f1e03f2e34d0e9937e412ecccbd4737a9443df6',
     '0x90652081c4c36a2df5be3fc46f51e8c4f8c24f00146c4a0799a266fcf8eab398',
     '0x0ecc2667fe957c3164f5280d0d06a81af486303c9331fc9997bf6444e2a95372',
     '0xd3b269292e546cafe0307653669ace3b32e53ac15c49352128a92c9348f952f0',
     '0xb38533278c3a5cd526e5c56a345d0cd32acf9a1ba514e5a5ecd571086c1a603d',
     '0x3cfa6f9f8c602ce5fa37921b4f84bc238ed4d7150768f190e68fd84e0092a65c',
     '0xda90733328609e422e994218a25a36eb0a901bef2c6f98a1cc178cb15f181fe6',
     '0xc6481a58573f9a422574124b59e0aa63e1e42d6c0dcff370a5764f525066eff6',
     '0xc789dde21ebb13c07fda9306d6583c23ee89b92c8ceb0c672d8034dac18414a9',
     '0xdc46a20511e0bfd7fc79a6cabc71f7759ebe96f9bdfeba6c16cdc5453c321b3d',
     '0x9bf30757b785d2f78ac5e5679190755749b616cf70e3808b689b0baff87de382',
     '0x431f18d1d743bdb1cd0a0e3e25e1495142bb68315492aadc366a86f05bf9c2bf',
     '0xe3ca880d348b6b154c9d767aa73020bf343e243d546d0d4df0c328ead44069ae',
     '0x26f7831d38a0772271f90253e51cbef2682078da0fc9dc86974ee9372bd94579',
     '0x9beb2ec543f604eafe217a553a80caed638318276c4a0e474dbbdbe7fb169bf3',
     '0xa531fe5ecfb7eb03c1a1e0c8a51a42f29e7e3b09d5c687008c5129f45f6c0715',
     '0x2413be58d5e505c7565cfdbac01a4ae844e5e2cfef6e5fe72049f62413e179e8',
     '0x48494a0aa677fa07416d02d6403278778d463ef3a613505df6dd7bb2eb889326',
     '0xce5d317b75a157f3db528fe152222a90bb6efe2529305bfdd567a31d67948760',
     '0x87219ddc00590bd163e4785b79caf78ed6bff7ec5717a549dfa410aa87f00cd2',
     '0x70b9be0a4b9cfac58d3c6deaa063e3313bb06cc0a6587a8159ad5b7d1168aacb',
     '0x371808a5afd8373bfe773a89f0124d109bca62efeeb679d0dd7b991a3145d7e7',
     '0xb581cdb0ee7be6c745a6e935dd97e7a173452153a407aa76a1874eb7465868c4',
     '0x1678a8b59dcbbc21cb115baaf6fc52a7dac6c05552b2f335535ab6cd4e5d2c4f',
     '0x9e31d11b2fb27574ed74cfb62304e5b6711a55a2d9bf2eb29984be02757fc758',
     '0x254135f4225fef757132ca99c0a77cf7f406c912e523bbc2b9cca30d537c4a61',
     '0xd11e2a23b8a7b9a4683c56c195d531fef263ec7fdf1d4810d9d84399bc1816f4',
     '0x975e239666f0b194e7df7c84a87ebfec49357b5eaf71e65040c6057c59d0c0a2',
     '0xeaf75bea0c1dcd5a5d01dade758995ada60e1aa023817f574c4e100ca51a7c7d',
     '0xe8a3cdaf1c5ec83a8b45fccf5a5cfeddb4accd06b67b4691f1950c0dd78861a1',
     '0x40879871e64ef925b9aa7996845e5ff28b3f0300300f074635cc8fd5b80b9e7b',
     '0x848540c065d1001ff58621779a6396c671fabf4026fd1e6d47d3c5472e9144a9',
     '0x5702c69daa0a31ef444cf9dbbcbd13a7457caade6f9f2b5f6f7a7f4e01a86837',
     '0x2426befe2ea17b8982e181424f9f001b7dc2c7753d0ef5328d7acf2bf8447867',
     ... 97 more items ],
  transactionsRoot:
   '0xefca4dbf85159b8f6cf265ac19cb8c9dffe8eae7fc78b7bb5cbc0dcceb0ac522',
  uncles: [] }

下一章:web3.js 查询指定区块

要查询指定区块,可以使用web3.eth.getBlock()函数,可以指定区块的序号或哈希值。示例app.jsconst Web3 = require('web3')const web3 = new Web3 ...