How can i get a block info from a running ethereum node using geth or nodejs or any other language? For example to get a block data from bitcoin, there is a config file which run a blocknotify.sh file when a transaction is confirmed and in that blocknotify.sh file there is this command : bitcoin-cli getblock "$@" >> "$@.json"
which gets the block data then i can send a post request of that block data to an api. So i want to do the same thing that is to get the block data from ETH nodes and send a post request to an api when a transaction is confirmed. How can i do this ?
While running in geth console
, you can use the web3 library to get block information:
> var blockNumber;
undefined
> var blockInfo;
undefined
> web3.eth.getBlockNumber(function(e, r) { blockNumber = r; });
undefined
> blockNumber;
2515149
> web3.eth.getBlock(blockNumber, function(e, r) { blockInfo = r; });
undefined
> blockInfo
{ difficulty: 1319089762, extraData: "0xd58301070d8650617269747986312e32332e30826c69", gasLimit: 4700036, gasUsed: 0, hash: "0xf9c495b5e5bcd3935aa4a6fd5a43009de29ca7d7be77d4b7cc4c68b8704bc422", logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", miner: "0x94cd009bbba97f30a36845e2025edf7544d62439", mixHash: "0xb8794a6d1b777f8c6fdf509f04896642f67dab82dc872ae9cbe9bcbf85172972", nonce: "0x264c013815697c2f", number: 2515149, parentHash: "0x0d2b4185aec34d8963b4c0b8fa7abb8604e1452bcb7f7f7d7a75a3d1cfd85f92", receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", size: 537, stateRoot: "0xc3810c2e763669a6e0219c78990c92b91c634d23810f4bd67144c4d76b9cfe6e", timestamp: 1516811780, totalDifficulty: 7642933628775356, transactions: [], transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", uncles: [] }
The web3 documentation can be found here: v0.2x.x, v1.0
EDIT - Example transaction info retreival:
> web3.eth.getTransaction('0xaebaf7e8207c417f6bb7920f3820e5220738d6825b6f577e3c3d0736d3c95b49', function(e, r) { if (e) console.log(e); else res = r; });
undefined
> res
{ blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000", blockNumber: null, from: "0x0a78c28257b40d5076ea180bc6a9e4c597c5ea98", gas: 90000, gasPrice: 40000000000, hash: "0xaebaf7e8207c417f6bb7920f3820e5220738d6825b6f577e3c3d0736d3c95b49", input: "0x", nonce: 32, r: "0x902f7a7b7c3e4ebcab47014f9c3b81858cdc4b90ef36d9630adaf30b47ec370f", s: "0x6f09e7d66dc2146afa108322f433c12b629eadd149e1e113669b6bd2e0cd467e", to: "0xb8ac544e818d01a9533c9556d572d8366b91d6c1", transactionIndex: 0, v: "0x2a", value: 200000 }