Ganache-cli: can not receive the event?

Created on 29 Dec 2017  路  1Comment  路  Source: trufflesuite/ganache-cli

I am just installing latest ganache-cli yesterday, 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);

Expected Behavior

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)

At this time, i expect the js app could receive a callback event.

Current Behavior

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.

context

currently i am using web http not ws. And currently I am testing on the ganache-cli and testrpc.

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

Your Environment

  • ganache-cli latest
  • web3 @1.0.0-beta.26
  • Node v7.0.0
  • Mac Sierra 10.12.6

Most helpful comment

This is expected behaviour at present when using web3 1.0.0, as ganache-cli doesn't yet have support for websockets, which web3 1.0.0 requires for event subscriptions. This is something I'm actively working on this week. You can follow #257 or trufflesuite/ganache-core#14 for progress.

In the mean time the workaround is to use web3 0.x.x.

>All comments

This is expected behaviour at present when using web3 1.0.0, as ganache-cli doesn't yet have support for websockets, which web3 1.0.0 requires for event subscriptions. This is something I'm actively working on this week. You can follow #257 or trufflesuite/ganache-core#14 for progress.

In the mean time the workaround is to use web3 0.x.x.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

varasev picture varasev  路  3Comments

lastperson picture lastperson  路  4Comments

gskerry picture gskerry  路  3Comments

DavidKuennen picture DavidKuennen  路  4Comments

axic picture axic  路  5Comments