Below is a simple programme which includes web3 :-

let web3 = require('web3');
var real = new web3(web3.givenProvider)    //  web3.givenProvider is having value of my infura endpoint


async function discuss()
let dif = await real.eth.getBlock(16532022)
let di = dif.size
console.log(di)             //    Line 1 :- displays the size

console.log(typeof dif )    // Line 2 :- returns object  

let   diftwo = await real.eth.getBlock(16532022).size ;

console.log(diftwo) ;    // Line 3 :- displays undefined

When I used the .size directly in Line 3 it is not showing the size of block. I checked the typeof of the .getBlock and saw that it is an object which means size is key of that object. So why is it not showing the result when I used the size using the dot notation.

Below code works

let   diftwo = (await real.eth.getBlock(16532022)).size ;  // associating await with .getblock
console.log(diftwo) ;

But in this manner , system has to fetch entire block info and then go to size property. Suppose if I have to get the size of numerous block for some use-case then waiting for fetching of entire block info will be too cumbersome. That is why , I am trying somehow trying that await should not get associated.

Can anyone please tell me the mistake which I am doing in Line 3.

  • just a small comment: a blockis made of a Header, Transactions and Receipts. These 3 pieces make a complete block that can be exported or imported by geth . After you implement your functionality to get the block size via RPC, please verify the size versus the same block number, but exported via "export" command. The block data is RLP encoded, so it is a long string of hexadecimal values and it contains everything, one line per block inside the export-file.
    – Nulik
    Feb 1, 2023 at 15:23
  • it is just a suggestion to implement quality assurance for your code
    – Nulik
    Feb 1, 2023 at 15:25

1 Answer 1


Hi Developer advocate from Chainstack here

You are really close to finding out the answer.

DL,DR The first time, you are getting size attribute from a block object. the second time, you are getting the size attribute from eth.getBlock().

Basically, web3.eth.getBlock() is a asynchronous function. It returns a promise when it is created. After a promise is "resolved" - meaning finish running and get the result from RPC provider(btw Chainstack is a good RPC provider too)- it becomes a block object, which contains the "size" attribute. await keyword before a promise lets your script pause and wait for the result.

The second time you call web3.eth.getBlock().size. Here you are trying to get size from a promise, instead of a block object.

web3.eth.getBlock().size is syntactically correct but of course, there is no such attribute before it is resolved, so It is undefined. in this case, the await keyword doesn't do anything.

Hope it helps.

  • So means as per you , (await real.eth.getBlock(16532022)).size is the solution ? Feb 9, 2023 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.