0

Is it possible to know when a block is mined? Is there any timestamp or event/subscription?

  • Did you try web3.eth.subscribe('newBlockHeaders') ? – Ismael Feb 12 '19 at 16:20
  • Yes, but I think you are notify when the block is added or created in the node, but no mined. I am running a testnet with two nodes(one miner and a peer), and first, I get a notification about block X, and then, one of my transactions is added to this block X. – sapiensapiens Feb 12 '19 at 18:16
1

Yes, of course. See the manual

  • Is it possible using javascript? – sapiensapiens Feb 12 '19 at 14:13
0

You can use web3js filter to watch for the latest blocks. You can specify the filtering parameters and wait in a watch callback to be triggered on the event of the specified parameters matched.

The below example can work for the newest blocks.

var filter = web3.eth.filter({toBlock:'latest'});
filter.watch(function (error, result) {
     if(!error)
         console.log(result.blockNumber, " has been added in chain"); 
});

You can take a look here for further explanation Webjs Filter API

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.