Describe the bug
Multiple events are emitted when using the MetaMask provider and subscribing to a contract event. The number of duplicate events seems to be the product of the number of events subscribed and the number of events emitted by the solidity contract.
Instructions are in this github repository
As a dapp developer, I want to be notified once per Solidity contract event.

This screenshot shows side-by-side the result of clicking the SimpleStorage button which triggers one event, SimpleSet to be fired by the SimpleStorage Smart Contract.

This screenshot shows side-by-side the result of clicking the FimpleStorage button which triggers two events, FimpleSet and FimpleSet2 to be fired from the FimpleStorage Smart Contract ((don't judge, names are hard).
Using Firefox v67.0, and the reproduction repo above, I verified the issue on firefox browser with MetaMask. The issue is not present when using Firefox's private browsing feature (sans MetaMask extension)
I can reproduce this down to MetaMask v4.17.1 (did not try it with older versions) in Chrome and Brave.
When I deploy the contract to ropsten and use infura as a provider (no metamask involved),
events behave as expected.
When I connect MetaMask to ropsten and use it as provider, the issue appears again.
This leads me to the conclusion that the issue must lie inside MetaMask, and is not produced by Ganache (or other evm's).
I have the same issue with v5.3.5
Any help would be appreciated
@bdresser have you been able to reproduce this with the steps provided by @cds-amal ?
This bug is really critical for me, as events have side effects (they are handled in a non idempotent way) in my dapp.
The events also appear multiple times when the metamask user is not the initiator of the transaction, so for all metamask users connected to the dapp.
@kumavis could you have a look into this or give me a head start by pointing me to which package/component/method i should look into first?
@bbjay I believe the issue is in https://github.com/MetaMask/eth-json-rpc-filters. It looks like it's subscribing too all the events, and once one is called returning all subscriptions. This results in one call + number of subscriptions firing. Maybe here, https://github.com/MetaMask/eth-json-rpc-filters/blob/master/subscriptionManager.js#L31?
I am using v7.1.1 and the issue still exists
I am using v7.7.8 and chrome v80.0.3987.163 (Official Build) (64-bit). this issue still exists. it fires 4 times for same event as i am listening to four different events using drizzle.
Also try it on firefox 74.0.1 (64-bit) privite and normal mode. still exits this error
@TAN-ZIXUAN you can try this workaround if you're using drizzle and a middleware to extract the events
@TAN-ZIXUAN you can try this workaround if you're using drizzle and a middleware to extract the events
thanks!
I created a Set to add events to. If the event has already been handled then I ignore it. Something like:
let processed_events = new Set();
contract.events.YourEvent({fromBlock: 'latest'}, function (error, event) {
if (!error) {
if (processed_events.has(event.transactionHash)) {
return; // ignore event since we've already handled it
}
// do something
processed_events.add(event.transactionHash);
} else {
console.log(error);
}
});`
Most helpful comment
I can reproduce this down to MetaMask v4.17.1 (did not try it with older versions) in Chrome and Brave.
When I deploy the contract to ropsten and use infura as a provider (no metamask involved),
events behave as expected.
When I connect MetaMask to ropsten and use it as provider, the issue appears again.
This leads me to the conclusion that the issue must lie inside MetaMask, and is not produced by Ganache (or other evm's).