- Solidity 教程
- Solidity - 首頁
- Solidity - 概述
- Solidity - 環境設定
- Solidity - 基本語法
- Solidity - 第一個應用程式
- Solidity - 註釋
- Solidity - 資料型別
- Solidity - 變數
- Solidity - 變數作用域
- Solidity - 運算子
- Solidity - 迴圈
- Solidity - 決策
- Solidity - 字串
- Solidity - 陣列
- Solidity - 列舉
- Solidity - 結構體
- Solidity - 對映
- Solidity - 型別轉換
- Solidity - 以太坊單位
- Solidity - 特殊變數
- Solidity - 樣式指南
- Solidity 函式
- Solidity - 函式
- Solidity - 函式修飾符
- Solidity - View 函式
- Solidity - Pure 函式
- Solidity - 回退函式
- 函式過載
- 數學函式
- 加密函式
- Solidity 常用模式
- Solidity - 提款模式
- Solidity - 限制訪問
- Solidity 高階
- Solidity - 合約
- Solidity - 繼承
- Solidity - 建構函式
- Solidity - 抽象合約
- Solidity - 介面
- Solidity - 庫
- Solidity - 彙編
- Solidity - 事件
- Solidity - 錯誤處理
- Solidity 有用資源
- Solidity - 快速指南
- Solidity - 有用資源
- Solidity - 討論
Solidity - 事件
事件是合約的可繼承成員。當事件被觸發時,它會將傳遞的引數儲存在交易日誌中。這些日誌儲存在區塊鏈上,並且可以使用合約地址訪問,直到合約存在於區塊鏈上。生成的事件無法從合約內部訪問,即使是建立和觸發它們的合約也無法訪問。
可以使用 event 關鍵字宣告事件。
//Declare an Event event Deposit(address indexed _from, bytes32 indexed _id, uint _value); //Emit an event emit Deposit(msg.sender, _id, msg.value);
示例
嘗試以下程式碼來了解事件如何在 Solidity 中工作。
首先建立一個合約並觸發一個事件。
pragma solidity ^0.5.0;
contract Test {
event Deposit(address indexed _from, bytes32 indexed _id, uint _value);
function deposit(bytes32 _id) public payable {
emit Deposit(msg.sender, _id, msg.value);
}
}
然後在 JavaScript 程式碼中訪問合約的事件。
var abi = /* abi as generated using compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceiptContract = ClientReceipt.at("0x1234...ab67" /* address */);
var event = clientReceiptContract.Deposit(function(error, result) {
if (!error)console.log(result);
});
它應該列印類似以下內容的詳細資訊 -
輸出
{
"returnValues": {
"_from": "0x1111...FFFFCCCC",
"_id": "0x50...sd5adb20",
"_value": "0x420042"
},
"raw": {
"data": "0x7f...91385",
"topics": ["0xfd4...b4ead7", "0x7f...1a91385"]
}
}
廣告