Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event listener is giving old events #1096

Closed
outdoteth opened this issue Oct 8, 2020 · 9 comments
Closed

Event listener is giving old events #1096

outdoteth opened this issue Oct 8, 2020 · 9 comments

Comments

@outdoteth
Copy link

outdoteth commented Oct 8, 2020

The following code is logging out events that have happened in the past.

myContract.on("someEvent", () => console.log("event"))

Expected behavior is that it logs out events only as they come in but it is logging events from 1 block in the past on initial run. I don't want to see the events from 1 block in the past - only the new events.

@outdoteth outdoteth reopened this Oct 8, 2020
@shivam0x
Copy link

@Dylan-Kerler how you resolved this issue?

@AleSua93
Copy link

I ran into this same issue, anybody has a solution?

@outdoteth
Copy link
Author

@ShivamDev31 @AleSua93 I am quite certain that it is an issue with ganache not ethersJS. Are you guys using ganache?

@AleSua93
Copy link

@Dylan-Kerler thanks for the answer! I'm using the Hardhat network... I haven't deployed anywhere else yet though, so it might certainly be an issue with the development network, but I'd have to check.

@yogh333
Copy link

yogh333 commented May 26, 2021

Hello, I have the same issue when using ganache-cli. Are you sure that the issue comes from ganache ?

@zemse
Copy link
Collaborator

zemse commented May 26, 2021

Can you see #1504 (comment) if that helps?

@yogh333
Copy link

yogh333 commented Jun 3, 2021

Hi zemse,

Thank you for your feedback, it helped me to understand and workaround the "issue" ;)

@johnson86tw
Copy link

Temporary solution:

const startBlockNumber = await provider.getBlockNumber();

contract.on(filter, (...args) => {
  const event = args[args.length - 1];
  if(event.blockNumber <= startBlockNumber) return; // do not react to this event
  
  // further logic
})

@ricmoo
Copy link
Member

ricmoo commented Aug 24, 2021

You can also use:

provider.once("block", () => {
  contract.on(filter, (...args) => { ... })
});

@ethers-io ethers-io locked and limited conversation to collaborators Aug 24, 2021
@ricmoo ricmoo closed this as completed Aug 24, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants