事件触发稳固

Yaj*_*Rai 7 ethereum solidity

我目前正在研究以太坊平台(node.js和solidity).我的问题是如何使用node.js在solidity(contract)中触发事件?

unf*_*res 10

事件由函数内部触发.因此,您可以通过调用调用事件的函数来触发一个.以下是更多信息:Solidity Event Documentation.


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