Ganache-cli: Contract event watch not returning past transactions, only new ones

Created on 10 May 2016  路  8Comments  路  Source: trufflesuite/ganache-cli

When I have something like:

var allEvt = contract.allEvents({}, {fromBlock:1, toBlock: 'latest'});
allEvt.watch(function(e, result) {
    console.log(result);
});

This returns only new events, not the histrory of all events since block 1 until the latest block as it would happen in an actual ethereum node.

Most helpful comment

I've faced the same issues here.
In my situation, I use the myContract.getPastEvents('allEvents', {fromBlock, toBlock}) api, and ganache only returns the events on the latest block.

my procedure like this:

  1. run ganache-cli
  2. deploy contracts
  3. set up js code
  4. make many transactions
  5. getPastEvents --> only get the events from the latest block

All 8 comments

I've also run across this issue. Here's code that should reproduce it.

Apologies for the late response on this. I've written tests for events recently and they seem to work fine on the latest TestRPC. Please check my comment here: https://github.com/ethereumjs/testrpc/issues/118#issuecomment-232201066

It's likely that you're setting up your listener _after_ the TestRPC mines a block. Thus, the TestRPC is exposing a race condition in your application you didn't know you had. Always set up your listener _before_ making the transaction that fires the event.

Please reopen if you're still seeing this issue.

@tcoulter thanks for your answer.

I am doing in it this way:

I usually run TestRPC from CLI:

testrpc -p 6767

Then in javascript:

var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:6767/'));
var contract = web3.eth.contract(contractInfo.abi).at(String(contractInfo.address));
var events = contract.allEvents({fromBlock: 0, toBlock: 'latest'});

events.watch(function(e, result){
   // handles the results here
});

Are you saying that I can't run TestRPC from CLI and should start it from JS?

Nope, that should work for you. But make sure you write the .watch statement before you write a transaction that triggers the event you're watching for.

That's how I've been doing.
My transactions run asynchronously, but this code I posted is part of a synchronous initialization script.

I still don't believe this is resolved. Specific contract events now do work with the fromBlock filter, however, the allEvents method doesn't, and I haven't seen any test coverage for the allEvents method.

To summarize this issue https://github.com/ethereumjs/testrpc/issues/113#issuecomment-234371807 seems to still persist with the allEvents method.

As an example

myContract.allEvents({}, { fromBlock: 0 }, (error, result) => {
  console.log(result);
});

The above only logs the events from the latest block still, not any preceding ones as would be expected by defining fromBlock to 0. There are several events preceding the latest block from the contract.

EDIT: Correction, my example is in fact faulty, it's supposed to be

myContract.allEvents({ fromBlock: 0 }, (error, result) => {
  console.log(result);
});

however, I have yet to test this on testrpc, but seeing this as the culprit, I am guessing it will work now.

I've faced the same issues here.
In my situation, I use the myContract.getPastEvents('allEvents', {fromBlock, toBlock}) api, and ganache only returns the events on the latest block.

my procedure like this:

  1. run ganache-cli
  2. deploy contracts
  3. set up js code
  4. make many transactions
  5. getPastEvents --> only get the events from the latest block

I had the same issue and I ended up creating ethnode, a tool to run an Ethereum node (Geth or Parity) for development. Let me know if it works for you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

varasev picture varasev  路  3Comments

dwalintukan picture dwalintukan  路  6Comments

dekz picture dekz  路  5Comments

DavidKuennen picture DavidKuennen  路  4Comments

ralph-pichler picture ralph-pichler  路  6Comments