0

I'm trying to listen for an event I've created on remix IDE , from NodeJS. I call a smart contract method from where the event is triggered and I'm waiting for it in my console...but I don't receive anything :(

I'm using NodeJS with ExpressJS Web3 Version: 1.0.0-beta.46

The Smart contract code would be something like :

pragma solidity >= 0.4.22 < 0.6.0;

contract Test {


event test1(address a,uint16 b,uint16 c,uint16 d,uint16 e);
event test2(address a,uint128 f,uint16 g);
event test3(address a,uint128 f,bool h);

//method 1
function method1(uint16 a,uint16 b,uint16 c,uint16 d) external payable {
// ... some code ... //

//here I trigger the event
    emit test1(msg.sender,a,b,c,d);
}

//method 2
function method2(uint128 f,uint16 g) external payable {
// ... some code ... //

//here I trigger the event
    emit test2(msg.sender,f,g);
}

//method 3
function method3(uint128 f) external payable {
// ... some code ... //

//here I trigger the event
    emit test3(msg.sender,f,true);
}


}

And this is how I listen for the event , in NodeJS :

 SmartContract.events.test1({fromBlock: 0, toBlock: 'latest'} , (error, event) => { console.log(JSON.stringify(event)); })
 .on('data', (event) => {
console.log("The event is : " + JSON.stringify(event));
 }).on('changed', (event) => {
console.log("Changed event : " + JSON.stringify(event));
 }).on('error', console.error);

I've tried to listen without any parameters like fromBlock or toBlock as well,but didn't work... I don't get any error or something. I start listening before to perform the smart contract method and after I perform it

the mined block received has the status "0x1" so the transaction was ok.But I don't know why the listener isn't working...

EDIT : Instantiation of the smart contract :

 const address = "0xB740096F1a6642190c816EfE37697891c65Afc92";
 const theABI = require('./getABI.js');
 var SmartContract = new web3.eth.Contract(theABI.getABI() , address);

The getABI.js file has just 1 function that returns the ABI. I'm sure and I've just rechecked,the ABI contains the events.Here's a snippet of code from the ABI where an event is present:

    {
            "anonymous": false,
            "inputs": [
                    {
                            "indexed": false,
                            "name": "a",
                            "type": "address"
                    },
                    {
                            "indexed": false,
                            "name": "f",
                            "type": "uint128"
                    },
                    {
                            "indexed": false,
                            "name": "g",
                            "type": "uint16"
                    }
            ],
            "name": "test2",
            "type": "event"
    },

Instantiation of web3 :

 const Web3 = require('web3');
 const web3 = new Web3(Web3.givenProvider || "ws://IP SERVER:PORT");

I've also tried the following approach :

 // Receives all events from the smart contract
 const listener = SmartContract.events.allEvents({}, (error, event) =>{ console.log("THE CALLBACK EVENT IS : " + JSON.stringify(event)); })
 .on('data', async (event) => { console.log("THE EVENT IS : " + JSON.stringify(event)); })
 .on('receipt', async function(result) { console.log("THE RECEIPT EVENT IS : " + JSON.stringify(event)); })
 .on('transactionHash', function(hash){ console.log("THE HASH EVENT IS : " + JSON.stringify(event)); })
 .on('error', function(error){ console.log("THE ERROR EVENT IS : " + JSON.stringify(event)); });

Still no output from event listener...

4
  • Did you update const address after deploying your contract? And do you call one of the contracts functions method1(), method2(), method3()? Does that work? It sounds like you're just deploying the contract and then wait for events. May 2, 2019 at 20:00
  • Yes @TheFrozenOne , address is updated . And yes , I call the smart contract methods and I get the transaction output in receipt.(Status is "0x1")
    – DIIMIIM
    May 3, 2019 at 6:03
  • Are you using rinkeby or ganache?, what is about the topic parameter? . if you stuck here then try the new truffle-contract library. github.com/trufflesuite/truffle-contract . "nothing to do with the problem, but the first letter of an event name is a capital letter"
    – Majd TL
    May 3, 2019 at 8:37
  • Hi & thank you for help ! I don't use truffle , I use geth.
    – DIIMIIM
    May 3, 2019 at 9:23

2 Answers 2

1

Problem solved :

The mined smart contract address wasn't correct so the events were listening for nothing.

1
  • XD it happend to everyone :)
    – Majd TL
    May 7, 2019 at 15:26
0

You can use subscribe as alternative, littel bit complexer

web3.eth.subscribe('logs',........);

The web3.eth.subscribe function lets you subscribe to specific events in the blockchain.

https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html

// UPDATE

try truffle and also the truffle-contract library

I used it in my Angular-App and it works well, it is used in some Truffle-boxes if you are looking for examples

https://github.com/trufflesuite/truffle-contract

look something like this the component in typescript

constructor(private web3Service: Web3Service) {
    console.log('Constructor: ' + web3Service);
}

ngOnInit(): void {
    console.log('OnInit: ');
    this.web3Service.artifactsToContract(MyToken_artifacts)
      .then((MyTokenAbstraction) => {
        this.MyToken = MyTokenAbstraction;
        this.MyToken.deployed().then(deployed => {
          console.log(deployed);
          deployed.Transfer({}, (err, ev) => {
            console.log('Transfer event came in, refreshing balance');
          });
        });

      });
  }

and in the artifactsToContract method -> something like this

const contractAbstraction = contract(artifacts);
contractAbstraction.setProvider(this.web3.currentProvider);
7
  • Thanks for this option...hmmm..yes...sounds pretty legit...I've been using it for some other things,didn't try it here yet
    – DIIMIIM
    May 3, 2019 at 9:24
  • Hi again,I tried the web3.eth.subscribe() method,it didn't work :(
    – DIIMIIM
    May 3, 2019 at 11:01
  • are you on a private chain?
    – Majd TL
    May 3, 2019 at 11:32
  • yes,I'm on a private one
    – DIIMIIM
    May 3, 2019 at 11:49
  • I updated my answer, try it please. it could maybe be a geth problem, so you could maybe try Ganache and with your code first. It is much easier and provider a blockchain scanner where you can check your transactions easily. If this dont work then can you upload your project on github where can i test it and check all the code
    – Majd TL
    May 3, 2019 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.