Skip to content
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

Subscribe.on fires past events #3069

Closed
Caruso33 opened this issue Jun 10, 2022 · 5 comments
Closed

Subscribe.on fires past events #3069

Caruso33 opened this issue Jun 10, 2022 · 5 comments
Assignees
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@Caruso33
Copy link

Ethers Version

5.6.5

Search Terms

subscribe new events filter

Describe the Problem

Hi,

when I am subscribing to a contract's events I am also seeing past events even the filter fromBlock: 'latest' is set.

Am I doing something incorrectly?

   import { useProvider } from "wagmi";

    const provider = useProvider();

    const contract = await new ethers.Contract(
    address,
    abi,
    provider
  );

    const listener: Listener = (id: BigNumber, user: string) =>
    console.dir("id", id.toString(), "user", user)

    contract.on(
    // "CancelOrderEvent",
    {
      topics: [utils.id("CancelOrderEvent(uint256,address)")],
      fromBlock: "latest",
    },
    listener
  )

I should mention, I am using wagmi however, I don't think that changes the way ethers works as I just work with the ethers.Contract.

Help appreciated, cheers 🙏🏼

Code Snippet

No response

Contract ABI

No response

Errors

No response

Environment

Ethereum (mainnet/ropsten/rinkeby/goerli), node.js (v12 or newer), Hardhat, Ganache

Environment (Other)

No response

@Caruso33 Caruso33 added the investigate Under investigation and may be a bug. label Jun 10, 2022
@ricmoo
Copy link
Member

ricmoo commented Jun 17, 2022

I'm unfamiliar with wagmi, but you shouldn't need to use await when using new in JavaScript, and the Contract contractor is synchronous.

You also cannot pass a fromBlock into the contract.on, as it accepts the EventFilter type. For your purpose you should be able to use the string "CancelOrderEvent".

But neither of those should affect your code... Can you try your code outside of wagmi to see if has anything to do with that?

@ricmoo ricmoo added discussion Questions, feedback and general information. and removed investigate Under investigation and may be a bug. labels Jun 17, 2022
@kvanstee
Copy link

I'm getting previous events fired with contract.on using ganache testnet. Can I assume this is an artifact of ganache?

@hickscorp
Copy link

It doesn't seem that fromBlock is honoured properly with the latest version of Ethers...

@ricmoo
Copy link
Member

ricmoo commented Jul 20, 2022

Thanks! I have found the bug.

Yes, this was introduced with the fix for networks which have non-consistent event indexing (e.g. Polygon and BSC).

This fix will go out in v5.7.0.

@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. fixed/complete This Bug is fixed or Enhancement is complete and published. and removed discussion Questions, feedback and general information. labels Jul 20, 2022
@ricmoo
Copy link
Member

ricmoo commented Aug 19, 2022

This has been published in v5.7.0.

Let me know if you still experience any problems.

Thanks! :)

@ricmoo ricmoo closed this as completed Aug 19, 2022
@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

4 participants