官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 汽车| 金湖县| 阳朔县| 元阳县| 温州市| 榆中县| 封丘县| 墨脱县| 太仆寺旗| 通城县| 勐海县| 霍林郭勒市| 玉门市| 库车县| 上饶县| 公安县| 琼海市| 都匀市| 开阳县| 乌恰县| 南江县| 罗定市| 达尔| 襄汾县| 定襄县| 平度市| 新宁县| 连城县| 常山县| 灵丘县| 镇安县| 贵定县| 莱州市| 滦南县| 渭源县| 兰西县| 财经| 古丈县| 武宁县| 余姚市| 榆社县|