- Mastering Ethereum
- Merunas Grincalaitis
- 292字
- 2021-06-24 15:01:06
Events
Events are a special type of function. Their purpose is to log data on the blockchain and actions that you want to retrieve at a later date. They can be subscribed to to receive an update whenever a new event is generated, almost in real time.
Essentially, you want them to keep a registry of the things that are happening inside your smart contract to later analyze them in order to fix bugs and to understand what happened if you need to read the past in an easy way.
Here's how you declare events inside your smart contract in Solidity:
pragma solidity 0.5.0
contract EventsExample {
event LogUserAddress(address userAddress);
function registerUser() public {
emit LogUserAddress(msg.sender);
}
}
In this example, you can see how an event is declared and emitted. When you declare your event, you have to decide the parameters that it will be able to receive; all of them are always optional, so you can omit them.
When you emit the event inside a function, you must make sure that they are of the right type. In the declaration, you can add a name for each parameter, or you can leave it with just the type, as follows:
event LogUserAddress(address);
It's good practice to name the parameters inside the event, to help others understand the purpose of each of those parameters.
You can also add an optional keyword called indexed. It's a modifier to the parameter of the event that allows you to search past events for that specific event. Think of indexed parameters as searchable entries in a database:
event LogUserAddress(address indexed userAddress);
Note that you must name the parameters that are indexed. Later you'll see how to retrieve those events and search for specific ones with web3.js.
- 普林斯頓微積分讀本(修訂版)
- 感官的盛宴:數(shù)學(xué)之眼看藝術(shù)(萬物皆數(shù)學(xué))
- Hands-On Blockchain Development in 7 Days
- 奧妙的數(shù)學(xué)問答
- 瘋狂的數(shù)學(xué)游戲
- Foundations of Blockchain
- 一個(gè)定理的誕生:我與菲爾茨獎(jiǎng)的一千個(gè)日夜
- 粗糙集的論域擴(kuò)展理論及在專家系統(tǒng)中的應(yīng)用
- The Modern C# Challenge
- 一個(gè)數(shù)學(xué)家的辯白(雙語版)
- 科學(xué)的數(shù)學(xué)本質(zhì)
- 高等數(shù)學(xué)(下冊)
- 高等數(shù)學(xué)同步練習(xí)指導(dǎo)
- 概率論與數(shù)理統(tǒng)計(jì)
- 說不盡的圓周率