2016-02-22 32 views
3

我目前正在研究ethereum平臺(node.js和solidity)。我的問題是如何使用node.js觸發一個事件(契約)?固體事件觸發

+1

你可能想嘗試[Ethereum Stackexchange](h ttp://ethereum.stackexchange.com/)這類問題的網站。 – default

+3

我不同意:這是一個編程問題,因此完全適用於StackOverflow(非編程以太坊問題是另一種情況)。 – bortzmeyer

回答

2

這裏是一個聰明的合同樣本事件定義:

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) 
     Sent(msg.sender, receiver, amount); 
    } 
} 

線事件Sent(address from, address to, uint amount);聲明瞭一個所謂的這是在功能send的最後一行炒魷魚「event」。用戶界面(當然也包括服務器應用程序)可以在沒有太多成本的情況下偵聽在區塊鏈上被觸發的事件。一旦它被觸發,聽衆也將收到參數from,toamount,這可以很容易地跟蹤交易。爲了聽這個事件,你可以使用。

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

0

活動允許的EVM日誌工具的方便使用,這反過來又可以被用來在dapp的用戶界面中「調用」JavaScript回調,它監聽這些事件,你可以檢查here的細節