2

I've got a simple contract that fires an event when a method is called. On my ethers.js client in an html page I've got the following code executed when the page is loaded:

        voterContract.on("StartVote", (from, question, choice_a, choice_b, choice_c, finishTime, event) => {
            console.log("StartVote EVENT", from, question, choice_a, choice_b, choice_c, finishTime, event);
            alert("Start vote event received! with data: " + from + " " + question + " " + choice_a + " " + choice_b + " " + choice_c + " " + finishTime);
        });

This works when the event is fired for the first time. However whenever I reload the page I am still getting the same event fired alert even though no new StartVote events have been performed. Any idea why is this? Is there a way for me to capture only new StartVote events (i.e only the ones that are fired after the page has been loaded) ?

Thank you!

1 Answer 1

4

Found it. This is related to the issue on https://github.com/ethers-io/ethers.js/issues/2310

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.

To fix it I need to use the following snippet to listen for events after the current block:

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

Using that it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.