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

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.

主站蜘蛛池模板: 南部县| 启东市| 汉川市| 闸北区| 涡阳县| 保亭| 白银市| 洛南县| 清远市| 海盐县| 兴义市| 缙云县| 当雄县| 启东市| 平江县| 苏尼特左旗| 洛川县| 建阳市| 万山特区| 安溪县| 兴国县| 金阳县| 搜索| 招远市| 巫山县| 纳雍县| 色达县| 海淀区| 翼城县| 镇江市| 铁岭县| 保康县| 长汀县| 东山县| 通州市| 潮安县| 新津县| 鲁山县| 清流县| 浙江省| 龙江县|