【emit】
我們可以寫一個函數裡面加上 emit 這個動詞來觸發事件。
舉例來說:如果我們先建立事件並且於函數裡觸發它。
pragma solidity ^0.8.11;
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 使用 web3.js 的套件呼叫合約事件,web3.js 在之後的章節會特別介紹!
var abi = /* abi as generated using compiler */;
// 要使用合約內容必須要得到合約在 compiler 之後輸出的 abi
var ClientReceipt = web3.eth.contract(abi);
var clientReceiptContract = ClientReceipt.at("0x1234...ab67" /* address */);
// 呼叫事件 Deposit:
var event = clientReceiptContract.Deposit(function(error, result) {
if (!error)console.log(result);
});在前端(dapp)回傳的結果如下:
如果我們今天輸入 0x05416460deb76d57af601be17e777b93592d8d4d4a4096c57876a91c84f4a712 也可以直接在 Remix 的下方互動環境看見一些資訊,包含 logs 等:
接下來我們會一一介紹 indexed, data, topics 等專有名詞。
Last updated
Was this helpful?