Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all of the events emitted by a Contract since it's inception?

I'm currently using the Truffle framework to check but all the documentation is around watching current events.

var meta = MetaCoin.deployed();
var events = meta.allEvents();
events.watch(function(error, result) {
  if (error == null) {
    console.log(result.args);
  }
}
like image 296
Klaus Avatar asked Oct 28 '25 17:10

Klaus


1 Answers

You need to specify the filter object to get all events from genesis (the crypto-people word for the first block) to now.

The following code should work:

MetaCoin.deployed().then(meta => {
  const allEvents = meta.allEvents({
    fromBlock: 0,
    toBlock: 'latest'
  });
  allEvents.watch((err, res) => {
    console.log(err, res);
  });
});

That said: Be careful with general lookups like this. You can easily crash your JSONRPC endpoint.

Further reading: https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-allevents

like image 173
Simon - ses.eth Avatar answered Oct 31 '25 12:10

Simon - ses.eth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!