首先,我的设置。
系统:ArchLinux
Ganache:v2.0.0
溶解度:0.5.8
web3:1.0.0-beta.37(任何高于37的水平,合同都不会部署)
节点:“最新”
一些背景和上下文信息
我有一个简单的智能合约,如下所示:
pragma solidity ^0.5.7;
contract CommerceChain {
// ------------------------------------------------------
// generic object events
event onNewCategory(uint productId);
// ------------------------------------------------------
// Blockchain variables
address owner;
mapping (uint => Category) categoryMapping;
uint[] categoryIndex;
struct Category {
uint id;
uint name;
uint description;
}
// ------------------------------------------------------
// Function modifier
modifier isOwner(){
require(msg.sender == owner, "You are not the owner");
_;
}
modifier notExistsCategory(uint id) {
require(categoryMapping[id].id != id, "Category already exists");
_;
}
// ------------------------------------------------------
// Default methods
constructor() public payable {
owner = msg.sender;
}
function() external payable {
}
function addCategory(uint id, uint _name, uint _description) public isOwner notExistsCategory(id) {
categoryMapping[id] = Category(id, _name, _description);
categoryIndex.push(id);
emit onNewCategory(id);
}
}
这是较大合同的一部分
1。。首先,通过以下方式获取合同实例:
const Web3 = require('web3);
const web3 = new Web3(new Web3.providers.WebsocketProvider(_config.provider.url));
//assuming the contract already exists
let contract = new web3.eth.Contract(abi, address, {
data: '0x' + byteCode,
gas: gasLimit,
gasPrice: gasPrice
});
2。,然后我通过以下方式订阅合同事件:
(我到目前为止尚未测试过3种订阅活动的方式)
方法1 来自eth event
contract.events.onNewCategory({
fromBlock: 0
},(error, event) => {
if (error) {
console.log(error);
}
if (event) {
console.log(event);
}
}).on('data', (event) => {
console.log(event); // same results as the optional callback above
}).on('changed', (event) => {
console.log(event);
}).on('error', (error) => {
console.error(error);
});
方法2 ,来自all events
contract.events.allEvents({
fromBlock: 0
}, (error, event) => {
if (error) {
console.error(error);
}
if (event) {
console.log(event);
}
});
方法3 来自eth subscriptions
const web3 = _web3Helper.getWeb3Instance();
let eventName = eventType.toString();
const eventJsonInterface = web3.utils._.find(contract._jsonInterface, o => o.name === eventName && o.type === 'event');
subscribedEvents[eventName] = web3.eth.subscribe('logs', {
address: config.wallet.address
}, (error, result) => {
if (!error) {
const eventObj = web3.eth.abi.decodeLog(eventJsonInterface.inputs, result.data, result.topics.slice(1));
callback(eventObj);
}
}).on("data", (log) => {
console.log(log);
}).on("changed", (log) => {
console.log(log);
});
3。,然后我致电:
contract.methods.addCategory(1, 2, 3).send({from: walletAddress}, callback)
为合同添加类别。
问题:
TL; DR
function addCategory(uint id, uint name, uint description);
event onNewCategory(uint id);
在remix.ethereum.org中,以及在使用localhost Ganache RPC进行测试时,我都可以看到事务已得到处理。
但是,交易事件永远不会触发!事件回调不会引发任何错误。
为什么我的活动永远不会触发?