0

I have some JavaScript that's connecting to a private ethereum network via JSON-RPC. I am trying to extract the transaction hash of the blocks but am unable to do so.

// BlockNumber
clientNodeOne.call({"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1},
        function(err, res) {
          if (err) console.log(err)
          console.log("MINER ONE block number: " + parseInt(res.result, 16) )
          miner_one_block_number = parseInt(res.result, 16);
        })

        // console.log("BLOCK NUMBER: " + miner_one_block_number);

// Block Hash
clientNodeOne.call({"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":[miner_one_block_number, false],"id":1},
      function(err, res) {
        if (err) console.log(err)
        console.log("MINER ONE transaction hash: " + res.result)
      })

The first call to read the block number works great. I am saving the block number to another variable, miner_one_block_number. I am then passing that variable into the input parameters for the call to eth_getBlockByNumber. Instead of getting the transaction hash for that block, I am instead getting a value of undefined. Any help would be appreciated!

1

This is failing because the block number passed as the first parameter to eth_gasBlockByNumber must be a hex string. Instead of doing parseInt(res.result, 16), just store res.result directly and pass that in.

| improve this answer | |
  • Hey I removed the parse int and stored the res.result directly. But instead of undefined, I get an [object Object]. Any ideas? Thank you for your help btw. – Sameet Ahmed Nov 13 '19 at 17:06
  • Nevermind got it! Used stringify to get the result. – Sameet Ahmed Nov 13 '19 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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