web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.11k stars 4.89k forks source link

web3.eth.Contract.allEvents does not work in Web3 1.0.0-beta16 using WebSockets #989

Closed onbjerg closed 6 years ago

onbjerg commented 7 years ago

Using allEvents in Web3 1.0.0 does not return any events. My code is as follows:

this.contract.events.allEvents({
  fromBlock: 0,
  toBlock: 'latest'
}, (err, event) => {
  console.log(err, event)
})

Using getPastEvents works. It seems like the subscription is never given an ID(?):

Subscription {
  id: null,
  callback: [Function],
  arguments:
   [ { toBlock: 'latest',
       address: '0x59c3b17749502874f237467818dfe46170ba92d8',
       topics: [] } ],
  _reconnectIntervalId: null,
  options:
   { subscription:
      { params: 1,
        inputFormatter: [Array],
        outputFormatter: [Function: bound ],
        subscriptionHandler: [Function: subscriptionHandler] },
     type: 'eth',
     requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} },
     params:
      { toBlock: 'latest',
        address: '0x59c3b17749502874f237467818dfe46170ba92d8',
        topics: [] } },
  subscriptionMethod: 'logs' }

I am using geth 1.6.7-stable.

Forbu commented 7 years ago

I am using web3 1.0.0 and I have somewhat the same problem. I did some test with this simple contract (using testrpc as the rpc provider) :

contract eventc {

event Top(string yolo);

function event_testing(string test){
  Top(test);
}
}
contract_event.methods.event_testing("tessssst").send({from: address_pers,gas: 1500000,
      gasPrice: 15000000}).then(console.log);

contract_event.events.Top({
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, event){
console.log("event : ",event);
});

The console only return : event : null (with the method transaction) despite multiple events in the blockchain. As for me the getPastEvents works fine.

gchinis commented 7 years ago

Hello,

Same for me, getPastEvents works allEvents does not.

The following code raises:

const eventHandler = clientReceipt.events.allEvents((error, event) => {
    if(error) {
        throw error
    }

    console.log('Event:');
    console.log(event);
});
Error: Returned error: Method not found
    at Object.ErrorResponse (/home/vagrant/app/node_modules/web3/packages/web3-core-helpers/src/errors.js:29:16)
    at Object.<anonymous> (/home/vagrant/app/node_modules/web3/packages/web3-core-requestmanager/src/index.js:137:36)
    at callback (/home/vagrant/app/node_modules/web3/packages/web3-providers-ipc/src/index.js:65:40)
    at Array.forEach (<anonymous>)
    at Socket.<anonymous> (/home/vagrant/app/node_modules/web3/packages/web3-providers-ipc/src/index.js:76:51)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)
    at readableAddChunk (_stream_readable.js:253:11)
    at Socket.Readable.push (_stream_readable.js:211:10)
    at Pipe.onread (net.js:585:20)

Versions:

dskvr commented 6 years ago

Also having this issue, getPastEvents() works like a dream but allEvents() and consequently contract.events.Event() do not work as expected.

They do not poll but will return an (unreliable) result under very specific circumstances as outlined below...

It should be noted that passing {} as an argument doesn't seem to return any result unlike the above edge case, despite being noted in the documentation that the arguments are optional.

It's possible the aforementioned edge case may shed light on the source of this problem, sans the milkshake

I've tried both WS and IPC transport with similar results.

beetleman commented 6 years ago

same story with: web3.eth.subscribe ;(

NicholasJarr commented 6 years ago

I subscribed to the event but it was not working. The callback was never called. Then I found out that I needed to activate the WebSockets server (--ws and similar in geth). Found it in this Ethereum Stack Exchange Post

Oxyaction commented 6 years ago

Issuing the same problem with 1.0.0-beta.24 When trying to in nodejs script

contract.events.allEvents({ fromBlock: 'latest' })
    .on('data', console.log)
    .on('changed', console.log)
    .on('error', console.log)

Nothing happens, script doesn't log anything and just exits while expecting it to hang and listen to the events, when I change code to this: contract.events.allEvents({ fromBlock: 'latest' }, console.log) Getting the error Error: The current provider doesn't support subscriptions: HttpProvider

Connecting to local json rpc client const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); which launched with following params geth --rpcapi "db,eth,net,web3,personal,web3" --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --identity "MyTestNode" --datadir "./data" --testnet --fast

Oxyaction commented 6 years ago

Tested with websocket providers - everything works fine

alexey-ernest commented 6 years ago

@Oxyaction could you please share the full code snippet, can't make it work either.

Oxyaction commented 6 years ago

@alexey-ernest started geth node with:

geth --rpcapi "db,eth,net,web3,personal" --ws --wsaddr "localhost" --wsport "8545" --wsorigins "*" --identity "MyTestNode" --datadir "./data" --testnet --fast

connected to web3:

const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));

listening:

contract.events.allEvents({ fromBlock: 'latest' }, console.log)
AndreyPatseiko commented 6 years ago

I can't use webSokets. How I can catch contract events? web3 - version 1.0.0-beta23

Oxyaction commented 6 years ago

@AndreyPatseiko Suppose it is impossible by it's nature, event should be initiated by the server and it is impossible with plain http protocol

AndreyPatseiko commented 6 years ago

You right. I was angry because about it any single word in the web3 documentation. Thanks for you example - if use webSocets all work correct, like as described in web3 documentation.

TSIApatruno commented 6 years ago

Anyone having a problem with watch all events not working after about 10 minutes?

Deams51 commented 6 years ago

Works if "latest" is used as fromBlock param, but does not work here if the fromBlock is different from latest.

Getting the following error: Error: CONNECTION ERROR: Couldn't connect to node on IPC.

bohendo commented 6 years ago

Was just exploring this same issue. Expanding @dskvr's observations, I wrapped my event listeners in a call to web3.eth.getBlock('latest') and explicitly pass the latest block number as fromBlock. I'm doing this in docker w nodemon so it's been starting & restarting pretty wildly without any issues (so far). My setup looks something like:

web3.eth.getBlock('latest').then(res => {
  console.log(`Starting event watchers from block ${res.number}`)
  contract.events.allEvents({ fromBlock: res.number }, eventHandler)
})

Earlier I was debugging something similar and realized I only halfway enabled my websockets connection. Remember to specify websockets both while specifying the web3 provider ws://localhost:8546 and while starting geth --ws --wsorigins="*".

khaschuluu commented 6 years ago

Same here using parity and web3 1.0.0-beta21

vitaliy-kuzmich commented 6 years ago

omg it is still not fixed since 2015 0_O

onairelav commented 6 years ago

no, here is working

onbjerg commented 6 years ago

Sorry, forgot to close it. It works for me most of the time, but there are some things to keep in mind:

Happy hacking

bohendo commented 6 years ago

To expand the comments by @onbjerg I also want to suggest one more troubleshooting tip for those who find themselves here:

See parity issue 6637 for details although it looks like this might have been fixed a couple days ago... Geth, on the other hand, has always been extremely reliable while fetching historical events.

JimLynchCodes commented 2 years ago

Eth is straight garbage, I think. I am using websocket provider, but my web3 side hears ZERO events being emitted in my smart contract deployed on polygon Mumbai... wtf? 🤔