Muh*_*bba 10
以下是智能合约的示例事件定义:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
line事件Sent(address from, address to, uint amount);
声明了一个所谓的" event
",它在函数的最后一行被触发send
.用户界面(当然还有服务器应用程序)可以在没有太多成本的情况下监听在区块链上触发的事件.一旦它被激发,听者也会收到的参数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));
}
})
Run Code Online (Sandbox Code Playgroud)
参考:http: //solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html
归档时间: |
|
查看次数: |
9176 次 |
最近记录: |