1

When i use this command :

curl --data '{"method":"eth_getBlockByNumber","params":["latest", true],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8544

Then I am getting this response :

{"jsonrpc":"2.0","result":{"author":"0x2a65aca4d5fc5b5c859090a6c34d164135398226","difficulty":"0x23796dec9350","extraData":"0xd783010404844765746887676f312e362e32856c696e7578","gasLimit":"0x47e7c4","gasUsed":"0x1e8d3","hash":"0x8918e1c221fca4bbd2686e2b991eedb55e5e07a56867b6444d6064db7a1f2132","logsBloom":"0x00000010000000020000000000020000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000002000000000000000000000000000040000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x2a65aca4d5fc5b5c859090a6c34d164135398226","mixHash":"0xd7f23f011e484617fda230b8a123050e9ddb68db0e2107b6e92c354d8d030d36","nonce":"0x7cb02500056bd498","number":"0x178def","parentHash":"0x309ccfe954d223a41165d5f8e79084b8cd8949602d30d5960d837d9fd2c7ab97","receiptsRoot":"0xbdfe515f906a594396675ea4417f2de701e147f6263525fcd2a8e63e955a8d70","sealFields":["0xa0d7f23f011e484617fda230b8a123050e9ddb68db0e2107b6e92c354d8d030d36","0x887cb02500056bd498"],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x398","stateRoot":"0x3eca3ea525993d54b46b18a3f65752ee4d18c54d2c4c8e413864aa5b14867327","timestamp":"0x573d35cd","totalDifficulty":"0x11c4f37beb4be0c5c","transactions":[{"blockHash":"0x8918e1c221fca4bbd2686e2b991eedb55e5e07a56867b6444d6064db7a1f2132","blockNumber":"0x178def","chainId":null,"condition":null,"creates":null,"from":"0xea674fdde714fd979de3edf0f56aa9716b898ec8","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xf21a395a97066b135278eb589d6e0472fd9d1e592aaada32ad61dc4e34fa7e29","input":"0x","nonce":"0x191a1","publicKey":"0x5d8af02c52b1fb40e45d6e04ae2351d443d1f7327e9b6dbb5ae1d918414899c625690c4c236889e23d9705f9db88876542b959d947a3497a30d0d922ee30015f","r":"0xc34afbc29e616513a28503097652f7ae62b9dbffb9eecc592dd625a0bf34c940","raw":"0xf870830191a18504a817c80083015f909451c7d68f3a4089e6e6d142dee0569b6770e6e335880de326b2f64b16b0801ba0c34afbc29e616513a28503097652f7ae62b9dbffb9eecc592dd625a0bf34c940a018717fbbeeda52a6adb8f2a059e05b72fcff03433e86aeca453eee06ef70cdc6","s":"0x18717fbbeeda52a6adb8f2a059e05b72fcff03433e86aeca453eee06ef70cdc6","standardV":"0x0","to":"0x51c7d68f3a4089e6e6d142dee0569b6770e6e335","transactionIndex":"0x0","v":"0x1b","value":"0xde326b2f64b16b0"},{"blockHash":"0x8918e1c221fca4bbd2686e2b991eedb55e5e07a56867b6444d6064db7a1f2132","blockNumber":"0x178def","chainId":null,"condition":null,"creates":null,"from":"0x4bc6c54e71ea68d069c895983812d426dc296b77","gas":"0x15f90","gasPrice":"0x4a817c800","hash":"0xcceefc4aaab15712582d5c96e974dd05e5e61bb7c5f5a4bdf5d101ecfa01dc8e","input":"0x","nonce":"0x8","publicKey":"0x4fcb530d5d1140522eee9c3f9263c5bc5aef49b448b7b1aa6cade7706d6d3bb465d732db660caab962f49ce74794bb67dd0510e24c94de0b45832f545733cc4d","r":"0xc321ec233a88e8cbf08c905e4de6fe729f3913fab9f0b36477f609adda5a69bb","raw":"0xf86d088504a817c80083015f909452929b839b37738439337c5b6a107418f6cb8c3588896909229be8a800801ba0c321ec233a88e8cbf08c905e4de6fe729f3913fab9f0b36477f609adda5a69bba02e76bf3d6b8c210968876d6ec7132f7103915d66e9f1ee481126f030227d59d0","s":"0x2e76bf3d6b8c210968876d6ec7132f7103915d66e9f1ee481126f030227d59d0","standardV":"0x0","to":"0x52929b839b37738439337c5b6a107418f6cb8c35","transactionIndex":"0x1","v":"0x1b","value":"0x896909229be8a800"},{"blockHash":"0x8918e1c221fca4bbd2686e2b991eedb55e5e07a56867b6444d6064db7a1f2132","blockNumber":"0x178def","chainId":null,"condition":null,"creates":null,"from":"0xfbb1b73c4f0bda4f67dca266ce6ef42f520fbb98","gas":"0x38eb3","gasPrice":"0x4a817c800","hash":"0x93c27acf3b0bca776975c674eea2be0e4dd103964dc62741de6848f756d95655","input":"0xbaac5300000000000000000000000000f8357a814f15517494d3bac7c93cd405be5a6c06","nonce":"0x4ca3","publicKey":"0x8178af6e1ae9a747de289f2106a85875288b24a2c6bdb1f069cad506409abdd4f1bd1ac05507615c852a9a74cf3013f7b13b9e0f03870011993e575a357a308d","r":"0x55802fbddf75a40105b04ba7915aaf01479c3637b102c38d70682ccda8133f66","raw":"0xf893824ca38504a817c80083038eb394bb9bc244d798123fde783fcc1c72d3bb8c189413888aa20afbc9ce7000a4baac5300000000000000000000000000f8357a814f15517494d3bac7c93cd405be5a6c061ba055802fbddf75a40105b04ba7915aaf01479c3637b102c38d70682ccda8133f66a00c632501b44e702f530f6a4ac6bfc9c3e74b691e5861ad6372c69909eb5ed948","s":"0xc632501b44e702f530f6a4ac6bfc9c3e74b691e5861ad6372c69909eb5ed948","standardV":"0x0","to":"0xbb9bc244d798123fde783fcc1c72d3bb8c189413","transactionIndex":"0x2","v":"0x1b","value":"0x8aa20afbc9ce7000"}],"transactionsRoot":"0x5528494eb8fd3038468c30050e85ef7cca4603c75ad5a3f222630eb92585eb73","uncles":[]},"id":1}

In the above response the block number is : 0x178def . How to convert this block number to readable block number, like example block number 545 ?

1 Answer 1

1

On a Linux command line, without using some hideous grep/regex? :-)

Here's a Python2 method (using Infura as the backend):

$ curl -s -X POST --data '{"id":1,"jsonrpc":"2.0", "method":"eth_getBlockByNumber","params":["latest", true]}' -H "Content-Type: application/json" https://mainnet.infura.io/<my_infura_key> | python -c "import sys, json; print(int(json.load(sys.stdin)['result']['number'], 0))"
5065179

So the important part:

<curl_command> | python -c "import sys, json; print(int(json.load(sys.stdin)['result']['number'], 0))"

This pipes the output of your curl command to a Python command, which parses the JSON, reads the correct field, and converts from a hex string into an integer.

I also passed -s to curl to run in silent mode.

You could also pipe the output of the curl command to the jq tool, and do something similar.

Also note that you could just use the eth_blockNumber method, which returns a more simple JSON response.

2
  • Thanks but i am getting this error in python : File "<string>", line 1, in <module> , is this command correct ? : curl --data '{"method":"eth_getBlockByNumber","params":["latest", true],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8544 | \python -c "import sys, json; print(int(json.loads(sys.stdin)['result']['number'], 0))" . Also grep/regex way will also work since i want to run this code inside shell script.
    – Farhan
    Feb 10, 2018 at 5:13
  • Okay - I was testing with a JSON file, not a stream. I've updated my answer to actually work :-) Feb 10, 2018 at 13:44

Your Answer

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

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