Solidity 事件未被调用

ben*_*852 3 javascript blockchain ethereum solidity smartcontracts

我正在尝试监视前端的事件,但出现了错误。

这是观看该事件的 JS

import web3 from './web3';

export async function callEvent () {
  await Contract.events.PracticeEvent().watch((response) => {
    console.log('the event has been called', response);
  }).catch((err) => {
    console.log(err);
  })

   await Contract.triggerEventFunc().call();
} 
Run Code Online (Sandbox Code Playgroud)

合约代码:

event PracticeEvent (string _message, uint _timestamp);

function checkEvent() public {
    emit PracticeEvent("event has been called", gts);
}
Run Code Online (Sandbox Code Playgroud)

---web3.js 文件

import Web3 from 'web3';

const web3 = new Web3(window.web3.currentProvider);

export default web3;
Run Code Online (Sandbox Code Playgroud)

所以当我运行该应用程序时,我收到一条错误消息

“未捕获(承诺中)TypeError:Contract.default.events.PracticeEvent(...).watch 不是函数”

这一切在 Remix 中工作正常,但当我尝试在实际应用程序中运行它时,它会变得混乱

我假设该错误与 web3 有关,但我不确定为什么,因为 web3 的东西在我的应用程序的其余部分工作正常。

有什么帮助吗?谢谢!

Ros*_*lis 5

事件只能在事务内部发出。你正在做一个.call(),它只是从区块链读取数据,并且不能发出事件。

如果您希望该函数发出您应该替换的事件

await Contract.triggerEventFunc().call();

await Contract.triggerEventFunc().sendTransaction();

这会将交易发送到区块链,并且发送需要花费以太币。在这个事务中,可以发出一个事件,因此您应该能够使用 web3 捕获它。