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

MetaMask web3 instance doesn't return events/logs #2393

Closed
bwheeler96 opened this issue Oct 18, 2017 · 65 comments
Closed

MetaMask web3 instance doesn't return events/logs #2393

bwheeler96 opened this issue Oct 18, 2017 · 65 comments
Assignees
Labels

Comments

@bwheeler96
Copy link

bwheeler96 commented Oct 18, 2017

I can't get logs from my contract. The following code works fine with the default web3 implementation that comes with truffle:

contract.MeEvent({}, { fromBlock: 0 }).get((err, data) => console.log(data))

But when I run the same code in a browser using the MetaMask version of web3, nothing happens. The promise never returns, and nothing is ever even sent to the network.

For example in the truffle web3 instance, the following requests are logged by testrpc:

eth_newFilter
eth_getFilterLogs

But when using the MetaMask implementation, nothing is even submitted to the testrpc instance.

I read the compatibility guide and am following it pretty closely. I can send transactions and call contracts just fine. I simple can not filter events.

The web3 version MetaMask is injecting is 0.20.2 and the version that truffle builds with is 0.16.0. Hopefully web3 doesn't have any breaking changes in minor versions

@bwheeler96
Copy link
Author

bwheeler96 commented Oct 19, 2017

As an update to this issue: The problem was caused by faulty network switching within MetaMask

See this stackoverflow: https://ethereum.stackexchange.com/questions/28747/cant-retrieve-event-logs-with-metamask-web3

I quit my browser and reopened my site, then everything worked. I'm happy to help out on either:

  1. Adding logic to throw or warn when this happens (a timeout could probably be useful)
  2. Fixing the network switching issue

Cheers,

Brian

@nteymory
Copy link

nteymory commented Oct 24, 2017

Also having this same problem on version 3.11.2

To be a bit more specific, MetaMask doesn't return any new events after the specified block number only when watching a specific contract address. If I watch without specifying a contract address, everything works fine

This works as expected, for events before and after someBlockNumber:

web3.eth.filter({fromBlock: someBlockNumber}, function(e, r) {});

These work for events on someBlockNumber, but not for any events created after someBlockNumber:

someContract.SomeEvent({}, {fromBlock: someBlockNumber}, function (e, r) {});
web3.eth.filter({fromBlock: someBlockNumber, address: someContract.address}, function(e, r) {});

@Overtorment
Copy link

Having same issue, my Dapp is not receiving events anymore (using Ropsten).
About 3 weeks ago everything was working fine.

@bwheeler96
Copy link
Author

@Overtorment various combinations of quitting and reopening your browser, toggling networks back and forth, and other hack voodoo wand waving seem to do the trick. Good luck

@GuigsEvt
Copy link

Is there any news about this issue ? Because I am still stuck with Metamask not getting any events from my contracts..

@nteymory
Copy link

Still having the same problem on 3.12.0

@GuigsEvt
Copy link

Do you know if it is working on older version ?

@pipex
Copy link

pipex commented Oct 29, 2017

I have the same issue. Events are not being received by my application when using metamask (it works fine using Web3 connected to local RPC or Mist). It does not matter if I'm using one of the test networks or I'm connected to a local RPC server through the extension.

@rawadrifai
Copy link

Is there a version of metamask that resolves this issue? Quitting and restarting my browser didn't do it. p.s. it works just fine without metamask (fallback to local node)

@tmashuang
Copy link
Contributor

tmashuang commented Nov 7, 2017

@rawadrifai try version 3.11.1 you would have to load it manually into the extensions list and there might be some other bugs in it as well. This was the version that fixes events/logs but introduced basic use errors.

@rawadrifai
Copy link

I have 3.12.0 and it's working now. thanks.

@johncpalmer
Copy link

I'm having a similar issue. If I set the fromBlock to be the block when my contract was created, I do not get any events back. However, if I use a block that is several thousand before the block when my contract was created, it magically works. Anyone know why? Very weird.

@danfinlay
Copy link
Contributor

that bug was caused in part by us switching our filter polyfill logic to this module, if you can find the issue in that, you’d fix it in MetaMask:
https://github.com/kumavis/eth-json-rpc-filters

@rawadrifai
Copy link

Guys we need to get this out of the chrome extension.. it's not very practical. Do you guys have progress on that?

@kumavis
Copy link
Member

kumavis commented Nov 20, 2017

@rawadrifai you reported that its working on 3.12.0, but your latest comment suggests you are still having a problem - what are you experiencing

I have 3.12.0 and it's working now. thanks.

@rawadrifai
Copy link

@kumavis it's very flaky to be honest, especially with events.
My last comment was asking for putting the functionality outside of a chrome extension. Maybe a native macOS and windows app?

We are developing dapps right now, and the metamask adoption is a limitation for many people. I don't know if something is cooking over at metamask, but I would think of a different solution that can operate in the background. Username/password that map to a keypair .. maybe.. and you can integrate that in your app. The main goal basically is not have the end user authenticate outside the app (the extension). I wish I had more clarity on the solution in my head.

@buhrmi
Copy link

buhrmi commented Nov 21, 2017

Hi there,

I'm also having some issues with events in 3.12.0. Just trying to show a simple notification when a token has been transferred.

My code looks like this:

contract.events.Transfer({
  filter: {_to: myAddress}
})
.on('data', (event) => {
  console.log(event)
})

(repo: https://github.com/buhrmi/nuxt-dapp/blob/master/store/index.js#L58, contract i'm trying to watch: https://rinkeby.etherscan.io/token/0x695d5f6060a92d730382dd06e8c3fb1d4012a07d)

But I don't see any events appearing on the console even though the transaction has been mined. Is it a problem with my code? Any help would be appreciated. Using getPastEvents I get the correct events, but I don't want to add polling.

@danfinlay
Copy link
Contributor

Could this be related to MetaMask/web3-provider-engine#188 ?

@buhrmi
Copy link

buhrmi commented Nov 22, 2017

Okay, when I run it this way:

contract.events.MyEvent({..}, function(err, ev) {
  console.error(err)
}

I get Error: The current provider doesn't support subscriptions: MetamaskInpageProvider. Seems to be duplicate of #2601

Is there something I can do to help out with that?

@danfinlay
Copy link
Contributor

@buhrmi That is #2350 that you’re experiencing, I think it’s different from this one. Could be fixed by writing a websocket subprovider, or worked around by using a pre-1.0 web3.js, or ethjs instead.

@ChainSafeSystems
Copy link

We have noticed this bug keeps appearing in newer versions.
Our solution:

  1. Switch to desired Network
  2. Close chrome and re-open

We believe that there may be an issue if you switch networks while it's trying to connect (i.e. sometimes it takes a while to load and you switch) and part of the code hangs without realizing you switched. This would cause metamask to look for events from the contract address on another network.

@nteymory
Copy link

nteymory commented Jan 7, 2018

My mistake. In 3.13.3 changing networks still causes events to stop reporting for me as well. However, it does appear to sometimes work, whereas before it never used to work. I tried some tests based on the comments made by @ChainSafe and it doesn't seem to matter how long I wait before switching between networks. It could be that if you switch quickly, it's more likely to fail.

@mattotodd
Copy link

mattotodd commented Jan 9, 2018

Just adding my 2 gwei to the conversation. I was not receiving contract events when using metamask, but events worked fine if i was connecting to local geth instance.

A) make sure your contract address is lower cased. (i'm assuming metamask is doing some filtering that uses your address and mine had upper case letters)
B) close all chrome tabs, close chrome. Reopen chrome and log back in to metamask.

After I made those changes I am able to see events coming through. Still a bit flaky, but better than nothing.

@m0wzer
Copy link

m0wzer commented Jan 12, 2018

Just spent half the day debugging why my DAPP frontend is not receiving events then found this issue. I can confirm this issue is still present in: 3.13.4

As already stated above, the following works as a solution - but hopefully we will get a fix:

  1. Switch to desired Network
  2. Close chrome and re-open

@samid737
Copy link

samid737 commented Jan 26, 2018

I can confirm that this issue is present in 3.13.7, switching networks and restarting seems to help.

Noticeable, perhaps related:

Console.logging the contract event objects shows that arguments (including callbacks) passed through do not seem to be processed, but after doing the above they are.

before restart:

console.log(instance.setName({},{fromBlock: 0, toBlock: 'pending'},function(err,result){console.log(result)}));

c {requestManager: s, options: {…}, implementation: {…}, filterId: null, callbacks: Array(0), …}
callbacks: []
filterId:"null"
formatter: ƒ ()
getLogsCallbacks: []
implementation: {newFilter: ƒ, uninstallFilter: ƒ, getLogs: ƒ, poll: ƒ}
options: {topics: Array(1). . .

after restart:

console.log(instance.setName({},{fromBlock: 0, toBlock: 'pending'},function(err,result){console.log(result)}));

c {requestManager: s, options: {…}, implementation: {…}, filterId: null, callbacks: Array(0), …}
callbacks: [f]
filterId:"0x3"
formatter: ƒ ()
getLogsCallbacks: []
implementation: {newFilter: ƒ, uninstallFilter: ƒ, getLogs: ƒ, poll: ƒ}
options: {topics: Array(1). . .

@felzix
Copy link

felzix commented Feb 6, 2018

Is there a bug bounty or something on this? I'm willing to put in a few finney if it gets this fixed sooner.

@gitcoinbot
Copy link

This issue now has a funding of 0.25 ETH (217.57 USD) attached to it.

  • If you would like to work on this issue you can claim it here.
  • If you've completed this issue and want to claim the bounty you can do so here
  • Questions? Get help on the Gitcoin Slack
  • $9273.71 more Funded OSS Work Available at: https://gitcoin.co/explorer

@0xean
Copy link

0xean commented Feb 9, 2018

I just added some funding! hopefully someone can tackle this.

@gitcoinbot
Copy link

Work has been started on the 0.25 ETH (214.39 USD) funding by:

  1. @lazaridiscom

Please work together and coordinate delivery of the issue scope. Gitcoin doesn't know enough about everyones skillsets / free time to say who should work on what, but we trust that the community is smart and well-intentioned enough to work together. As a general rule; if you start work first, youll be at the top of the above list ^^, and should have 'dibs' as long as you follow through.

On the above list? Please leave a comment to let the funder (@pelsasser) and the other parties involved what you're working, with respect to this issue and your plans to resolve it. If you don't leave a comment, the funder may expire your submission at their discretion.

@0xean
Copy link

0xean commented Feb 10, 2018

@lazaridiscom - yea, sorry my requirement doesn't really help alot since we would have to wait for this fix to make it into a new release. So I can remove that requirement so you can get paid in a timely fashion.

I can show you how to produce the error with our environment pretty easily - it is part of this issue here

Feel free to contact me [email protected] and I can help you get our development environment up to see the issue if you decide to go that route.

@smarx
Copy link

smarx commented Feb 10, 2018

@lazaridiscom Glad to see you're trying to help here! Note that there are likely several bugs around events. The issue you just linked to looks different from what people are talking about here. The two main bugs I observed in this thread are:

  1. Events don't match if you use a mixed-case contract address. This has already been fixed. (See incorrect case-sensitive comparison for addresses eth-json-rpc-filters#4).
  2. Events don't work if you switch networks. (I gave a "fix" in MetaMask web3 instance doesn't return events/logs #2393 (comment), but I suspect it's just hiding an underlying issue. It may, however, help you find a place to start.)

For a repro of the latter issue:

  1. Go to https://programtheblockchain.com/dapps/counter-with-events/ with MetaMask installed.
  2. Switch networks in MetaMask. It doesn't matter what network switch you do... just make sure you've switched and then ended up on Ropsten, since that's where the contract is deployed. (You can switch multiple times; the bug repros 100% once you've switched networks at least once.)
  3. Trigger an event by clicking the "Increment" button and note that no message is logged to indicate the event was observed.

To see the demo actually working properly, restart the browser (or disable/enable MetaMask) once you're on Ropsten and then don't switch networks after that. My repro works fine against ganache also, so you may want to deploy the same contract locally instead.

Contract source:

pragma solidity ^0.4.19;

contract Counter {
    uint256 public count = 0;

    event Increment(address who);   // declaring event

    function increment() public {
        Increment(msg.sender);      // logging event
        count += 1;
    }
}

@nteymory
Copy link

@lazaridiscom I tested your change (https://github.com/MetaMask/metamask-extension/pull/3224/files) and it appears to fix the problem on my end

@gitcoinbot
Copy link

Work for 0.25 ETH (214.25 USD) has been submitted .

If you are the claimee, please leave a comment to let the funder (@pelsasser) (and the other parties involved) that you've submitted you work. If you don't leave a comment, the funder may expire your submission at their discretion.

@KennethAshley
Copy link

KennethAshley commented Feb 13, 2018

@lazaridiscom Seems like your changes fixes this issue.

@0xean
Copy link

0xean commented Feb 13, 2018

@lazaridiscom - thanks for the patience, releasing funding now.

danfinlay added a commit that referenced this issue Feb 13, 2018
use the providers initial _blocktracker (fixes #2393)
@gitcoinbot
Copy link

The funding of 0.25 ETH (210.74 USD) attached to this issue has been approved & issued .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests