我目前正在对复仇的平台(node.js中和坚固)。我的问题是我怎么触发如何使用Node.js在坚固(合同)的事件吗?
事件从功能内触发。所以,你可以通过调用调用事件的功能触发一个。下面是详细信息:Solidity Event Documentation。
这里是一个聪明的合同样本事件定义:
contract Coin {
//Your smart contract properties...
// Sample event definition: use 'event' keyword and define the parameters
event Sent(address from, address to, uint amount);
function send(address receiver, uint amount) public {
//Some code for your intended logic...
//Call the event that will fire at browser (client-side)
emit Sent(msg.sender, receiver, amount);
}
}
该生产线事件Sent(address from, address to, uint amount);
声明这是在功能event
的最后一行发射了所谓的“send
”。用户界面(以及课程的服务器应用程序)可以监听这些事件在blockchain被解雇没有太多的成本。一旦它被激发,听者也会收到的参数from
,to
和amount
,这使得它易于跟踪交易。为了侦听此事件,你可以使用。
Javascript代码,将捕获的事件,并写在浏览器控制台的一些消息:
Coin.Sent().watch({}, '', function(error, result) {
if (!error) {
console.log("Coin transfer: " + result.args.amount +
" coins were sent from " + result.args.from +
" to " + result.args.to + ".");
console.log("Balances now:\n" +
"Sender: " + Coin.balances.call(result.args.from) +
"Receiver: " + Coin.balances.call(result.args.to));
}
})
参考:http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
事件允许的EVM日志记录功能,这反过来又可以用来在DAPP,这侦听这些事件的用户界面,“呼”的JavaScript回调方便使用,您可以检查here查看详细
添加事件发出一个函数,比调用该函数。您还可以使用模拟合同(仅在必要时)的情况下,你只需要使用事件进行调试,不需要在合同本身的事件。在这种情况下,从你的合同函数返回到一个模拟的功能,比与返回值有触发事件。在JS你只需要只调用模拟的功能,然后读取的事件。