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...
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.