web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

can not receive the events

Open helxsz opened this issue 8 years ago • 11 comments

My web3 version is [email protected], I am deploying the contract on the localhost:8545.

Here is my contract example.

pragma solidity ^0.4.18;
contract LocalEthereum {

    address public owner;
    event Created(bytes32 _tradeHash);
    function createEvent() onlyOwner external {
        Created(0x01);
    } 
}

and here is my js code

init the contract instance

var Contract = new this.web3.eth.Contract(abi_json,address);

button click events

  Created(account){
      Contract.methods.Created().send({from: account,gas:210000,gasPrice:5000000000})
                      .on('transactionHash', function(hash){
                          console.log('hash',hash);
                      })
                      .on('receipt', function(receipt){
                          console.log('receipt',receipt);
                      })
                      .on('confirmation', function(confirmationNumber, receipt){
                          console.log('confirmation',confirmationNumber);
                      })
                      .on('error', console.error);
  }

watch events

  this.Contract.events.Created({},{ fromBlock: 0, toBlock: 'latest' }, function(error, event){ console.log(event); })
                        .on('data', function(event){
                              console.log(event);
                        })
                        .on('changed', function(event){
                             console.log('on changed');
                        })
                        .on('error', console.error);

The js app can load the contract and connect the ethereum network, when I click the button, Contract will generate a 'Created' rpc call to the ethereum, the transaction can be received immediately. On the testrpc interface I could also see the transaction as shown below.


  Transaction: 0x2469f2085c8f6f23ad8aa30bc6cf99ade40ea2f0368b9bb008496ba05d927d84
  Gas usage: 21272
  Block Number: 6
  Block Time: Thu Dec 28 2017 20:38:02 GMT+0000 (GMT)

Everything works well, however the event call is never activated after the method call. There is only one time event call in the app intialization but still the

  this.Contract.events.Created({},{ fromBlock: 0, toBlock: 'latest' }, function(error, event){ console.log(event); })
                        .on('data', function(event){
                              console.log(event);
                        })

the console outputs the null.

Wondering what is the problem?

helxsz avatar Dec 28 '17 20:12 helxsz

i wonder is it because of web3 provider? currently i am using web http not ws. And currently I am testing on the testrpc.

this.web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

is http provider doesn't provide event?

helxsz avatar Dec 29 '17 14:12 helxsz

From what i understood you can only watch events trough ws (but i might be wrong) and so far i could only get them to work running a own geth instance with the params ... --rpccorsdomain="*" --ws --wsorigins="*" --wsaddr 0.0.0.0

and then

w3 = new Web3('ws://127.0.0.1:8546');

7Ds7 avatar Dec 30 '17 00:12 7Ds7

I have tried

geth --identity "MyTestNetNode" --rpccorsdomain="*" --ws --wsport=8546 --wsorigins="*" --wsaddr 0.0.0.0 -- --cache=512 --rpc --rpcapi="personal,eth,network" --rpcport=8545 --fast datadir /Users/abc/Documents/projects/ngx-admin-master

it gives me INFO [01-01|12:39:07] Generated ethash verification cache      epoch=2 elapsed=3.111s
INFO [01-01|12:39:07] Imported new block headers               count=2048 elapsed=483.670ms number=37120 hash=a27938…2d697f ignored=0
INFO [01-01|12:39:08] Imported new block headers               count=2048 elapsed=518.097ms number=39168 hash=1a1cd2…514d2d ignored=0
INFO [01-01|12:39:08] Imported new state entries   

i used the metamask to connect the localhost:8545, but it can't connect to it, any ideas?

helxsz avatar Jan 01 '18 12:01 helxsz

You use port 8546 (for web sockets) not 8545, from what i get if you use web3 = new Web3(Web3.givenProvider); to use with meta mask, and you use another instance to look for events w3 = new Web3('ws://127.0.0.1:8546');

7Ds7 avatar Jan 01 '18 19:01 7Ds7

I am facing the similar problem, unable to detect events. However, I am able to get all past events using getPastEvents.

ankitxjoshi avatar Apr 04 '18 13:04 ankitxjoshi

Oh. Got it with the solution you provided. But it used to work in the earlier version when event.watch was being used.

ankitxjoshi avatar Apr 04 '18 13:04 ankitxjoshi

I came across this problem too, the events listening part never worked after methods send? Only getPastEvents() works, and I tested with rinkeby net, the same result. So, the reason is that web3js 1.0 does not work? Why?

oo7ww avatar May 24 '18 04:05 oo7ww

I'm using this code:

this.crowdsale.instance.events
      .TokenPurchase({
        fromBlock: 'latest'
      })
      .on('data', event => {
        console.log(event)
      }).on('changed', event => {
        console.log(event)
      }).on('error', error => {
        console.log(error)
      })

But the EventEmitter won't trigger anything.

I used to get it working via websockets:

subscribeToEvents(){
    this.setWebsocketByNetwork()

    let provider = new Web3.providers.WebsocketProvider(this.websocket)
    let web3 = new Web3(provider)

    return web3.eth.subscribe('logs', {
      address: this.address,
      topics: [],
    })
  }

but since [email protected] it is not working anymore.

gus-canya avatar Aug 01 '18 20:08 gus-canya

I am also using getPastEvents with "web3": "^1.0.0-beta.36", Not able to get all the events

heypran avatar Oct 22 '18 13:10 heypran

@gus-canya Your reason is that the query start block is error. you can used it

  myContract.events.MyEvent({
  filter: {},
  fromBlock: 0,
  toBlock: 'latest
  }, function(error, event){ console.log(event); })

fengyuyizai avatar Jan 07 '19 03:01 fengyuyizai

I've tested and fixed the events handling during the refactoring. Please update to the latest version of Web3.js.

nivida avatar Apr 02 '19 09:04 nivida