Я пытаюсь прослушать событие, которое я создал в IDE-ремиксе от NodeJS.Я вызываю метод смарт-контракта, из которого происходит событие, и жду его в своей консоли ... но я ничего не получаю: (
Я использую NodeJS с ExpressJS Web3 Version:1.0.0-beta.46
Код смарт-контракта будет выглядеть примерно так:
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);
}
}
И вот как я слушаю событие в 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);
Я пытался слушать без каких-либо параметров, таких как fromBlock или toBlock, но не сработало ... У меня нет ошибок или чего-то в этом роде. Я начинаю слушать, прежде чем выполнить метод смарт-контракта, и после того, каквыполните это
полученный добытый блок имеет статус "0x1", поэтому транзакция была в порядке. Но я не знаю, почему слушатель не работает ...
РЕДАКТИРОВАТЬ: Создание экземпляраумный контракт:
const address = "0xB740096F1a6642190c816EfE37697891c65Afc92";
const theABI = require('./getABI.js');
var SmartContract = new web3.eth.Contract(theABI.getABI() , address);
В файле getABI.js есть только 1 функция, которая возвращает ABI. Я уверен, и я только что перепроверил, ABI содержит события. Вот фрагмент кода изABI, где присутствует событие:
{
"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"
},
Instанти-web3:
const Web3 = require('web3');
const web3 = new Web3(Web3.givenProvider || "ws://IP SERVER:PORT");
Я также попробовал следующий подход:
// 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)); });
Все еще нет вывода из прослушивателя событий ...