1.10 Event 事件 - 地震演習

閱讀時間約 1 分鐘
raw-image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract EarthquakeAlertSystem {
// 定義地震警報事件
event EarthquakeAlert(string message, uint256 magnitude, address indexed issuer);

// 地震警報發布函數
function publishAlert(string memory message, uint256 magnitude) public {
emit EarthquakeAlert(message, magnitude, msg.sender);
}
}

event用於記錄和通知智能合約中的特定事件。以地震訊息發布演習為例,我們可以創建一個智能合約來管理地震訊息的發布和通知

首先,我們定義一個EarthquakeAlert事件,用於記錄地震警報的發布:

  • EarthquakeAlert事件用於記錄地震警報的發布。它包含一個訊息、地震強度(magnitude)和發布者的地址。發布者的地址被標記為索引,以便在監聽事件時更容易搜索和篩選。
  • publishAlert函數允許發布地震警報,並在發布成功時發出EarthquakeAlert事件。
    尋大神腳印, 亦步亦趨。
    留言0
    查看全部
    發表第一個留言支持創作者!
    從 Google News 追蹤更多 vocus 的最新精選內容