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

Why does contract event fire when no events have happened? #2310

Closed
adriandelgg opened this issue Nov 20, 2021 · 3 comments
Closed

Why does contract event fire when no events have happened? #2310

adriandelgg opened this issue Nov 20, 2021 · 3 comments
Labels
investigate Under investigation and may be a bug.

Comments

@adriandelgg
Copy link

adriandelgg commented Nov 20, 2021

Describe the bug
I'm using contract.on(); & whenever I reload the page it triggers the event being fired even though no event has happened yet. I thought the expected behavior was that it ONLY triggers when an event is fired?

Correct me if I'm wrong, but shouldn't it only fire if an event happens? Meaning that when you call contract.on(event, listener) it will listener passively but not trigger the listener until the event actually happens?

Reproduction steps

useEffect(() => {
function newToken(
   by: string,
   address: string,
   symbol: string
) {
   window.ethereum.request({
     method: 'wallet_watchAsset',
     params: {
        type: 'ERC20',
        options: {
           address,
           symbol,
           decimals: 18
        }
     }
   });
}
factoryContract.on('TokenCreated', newToken);
}, [factoryContract]);

Environment:
Hardhat, Ethers.js, Next.js/React

@adriandelgg adriandelgg added the investigate Under investigation and may be a bug. label Nov 20, 2021
@ricmoo
Copy link
Member

ricmoo commented Nov 20, 2021

What is the event being shown? Keep in mind in v5 the events are scanned for starting in the current block, so if the current block has that event it will show up.

In v6 only future blocks will emit an event.

@adriandelgg
Copy link
Author

What is the event being shown? Keep in mind in v5 the events are scanned for starting in the current block, so if the current block has that event it will show up.

In v6 only future blocks will emit an event.

Ah, I see. Is there a way to start using v6 already, or is it already out? If not, when does it release?

@ricmoo
Copy link
Member

ricmoo commented Nov 30, 2021

No, v6 is still just local on my computer. For v5, the best way to accomplish what you want above is:

provider.once("block", () => {
    factoryContract.on('TokenCreated', newToken);
});

That will start the event handler after the next block occurs.

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

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
investigate Under investigation and may be a bug.
Projects
None yet
Development

No branches or pull requests

2 participants