【问题标题】:Solidity event isn't fired没有触发 Solidity 事件
【发布时间】:2019-09-16 00:19:21
【问题描述】:

我正在尝试从 NodeJS 监听我在 remix IDE 上创建的事件。 我从触发事件的位置调用智能合约方法,我在控制台中等待它......但我没有收到任何东西:(

我将 NodeJS 与 ExpressJS 一起使用 Web3 版本: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 的代码的 sn-p:

    {
            "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"
    },

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)); });

事件监听器仍然没有输出...

【问题讨论】:

  • 您能否也发布您如何实例化合同以及使用哪个提供者?
  • 抱歉迟到了...我已经更新了我的问题。您能再看一下吗? @Jagrut

标签: node.js events ethereum solidity smartcontracts


【解决方案1】:

问题解决了:

挖掘的智能合约地址不正确,因此事件没有监听。

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2019-11-02
    • 2012-06-30
    相关资源
    最近更新 更多