2

Is there a way to get the entire block information and transaction receipts within the block all in one function/method?

Irwing Tello

1
  • It is possible to get the entire block information using the Moralis Block API. If you are ok with using API I can share an example of how you can do it.
    – JohnVersus
    Jun 22, 2023 at 7:14

1 Answer 1

2

This data can be fetched from the chain using just one single RPC, using the Single Flight RPC Add-on of QuickNode Marketplace.

First, enable the Single Flight RPC Add-on on your QuickNode Ethereum RPC endpoint. Then you can use the following ethers v6 script:

const ethers = require("ethers");
(async () => {
  const provider = new ethers.JsonRpcProvider("QUICKNODE_EETHEREUM_RPC_URL");
  const network = await provider.send(
    "qn_getBlockWithReceipts",
    ["block_number_in_hex_starting_with_0x"]
  );
  console.log(JSON.stringify(network));
})();

This Add-on has built-in retry logic, so even if your method call fails the first time, it'll retry until you get the output.

The output will look like something like this:

{
   "id":1,
   "jsonrpc":"2.0",
   "result":{
      "block":{
         "baseFeePerGas":"0x4bbdd7c2d",
         "difficulty":"0x0",
         "extraData":"0x",
         "gasLimit":"0x1c9c380",
         "gasUsed":"0x1a7919",
         "hash":"0x5e93e511585b002a56a2afee7313c2578b5e98bc67835d9c9e3ef42dff9e3961",
         "logsBloom":"0x010500000000200000000",
         "miner":"0x0000000000000000000000000000000000000000",
         "mixHash":"0x09c73612b8dccc0b126aa2276f8854ede7760f6d548938e58d4b1ca4bef01516",
         "nonce":"0x0000000000000000",
         "number":"0xf7d1ec",
         "parentHash":"0xb739eab5d2c9ebfe1716af26446e09a3c349f1d417269bf70196a19f47fcaa81",
         "receiptsRoot":"0x61980ee0a5e9bdb789624ce64640a64ea9895cabd44b744e7de92bf4994ee0d5",
         "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
         "size":"0x2391",
         "stateRoot":"0xb2d04d19837a20839e2fb07c9604677020b2798c3f27be2051b099944cf2b3b6",
         "timestamp":"0x63a47743",
         "totalDifficulty":"0xc70d815d562d3cfa955",
         "transactions":[
            {
               "accessList":null,
               "blockHash":"0x5e93e511585b002a56a2afee7313c2578b5e98bc67835d9c9e3ef42dff9e3961",
               "blockNumber":"0xf7d1ec",
               "chainId":"0x1",
               "from":"0x5041ed759dd4afc3a72b8192c143f72f4724081a",
               "gas":"0x668a0",
               "gasPrice":"0x87f91cd00",
               "hash":"0x8169c8ec321e6a3de3b6f5384cfede82e92cfe4a8494cb3f5460213cb920d955",
               "input":"0xa9059cbb00000000000013185de0",
               "maxFeePerGas":"",
               "maxPriorityFeePerGas":"",
               "nonce":"0x14a02f",
               "r":"0x972cc5d000dd64ffb6ad46e77f49956bf717346b4d737e03b74f8dd39c72e867",
               "s":"0x68076e50ae03e870e6970a3228e92a9e4edb17d3e1bff903ea4eb0777cc25f5f",
               "to":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
               "transactionIndex":"0x0",
               "type":"0x0",
               "v":"0x26",
               "value":"0x0"
            },
            "...."
         }
      },
      "transactionsRoot":"0xcd2c9b517771b7edf621f92a2f85e9178fbf45319917db199bb3117c3797973e",
      "uncles":[
         
      ]
   },
   "receipts":[
      {
         "blockHash":"0x5e93e511585b002a56a2afee7313c2578b5e98bc67835d9c9e3ef42dff9e3961",
         "blockNumber":"0xf7d1ec",
         "contractAddress":null,
         "cumulativeGasUsed":"0x10059",
         "effectiveGasPrice":"0x87f91cd00",
         "from":"0x5041ed759dd4afc3a72b8192c143f72f4724081a",
         "gasUsed":"0x10059",
         "logs":[
            {
               "address":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
               "blockHash":"0x5e93e511585b002a56a2afee7313c2578b5e98bc67835d9c9e3ef42dff9e3961",
               "blockNumber":"0xf7d1ec",
               "data":"0x0000000000000000000000000000000000000000000000000000000013185de0",
               "logIndex":"0x0",
               "removed":false,
               "topics":[
                  "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                  "0x0000000000000000000000005041ed759dd4afc3a72b8192c143f72f4724081a",
                  "0x000000000000000000000000c82bc6286e1126b05e80eeeeee88af089beef27b"
               ],
               "transactionHash":"0x8169c8ec321e6a3de3b6f5384cfede82e92cfe4a8494cb3f5460213cb920d955",
               "transactionIndex":"0x0"
            }
         ],
         "logsBloom":"0x00000000000000000000000000000000000000000000000000000000002000000000001000000000000000000000000000000000000000000000000000000000000000000000000008000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000004000000000000000010000000000000000000000000000000000200008000000000000000000000000000000000000000000000000002002000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000",
         "status":"0x1",
         "to":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
         "transactionHash":"0x8169c8ec321e6a3de3b6f5384cfede82e92cfe4a8494cb3f5460213cb920d955",
         "transactionIndex":"0x0",
         "type":"0x0"
      },
      "...."
   }
}

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.