简体   繁体   中英

What are Solidity Events

I have been struggling for quite some time with finding an explanation to what events are in Solidity (or in the Blockchain context). As far as I understand, they are a way of storing (or logging) information on the particular contract that can then be updated throughout the life of that contract. But how is this different than a plain ol' variable? Why can't I just create a variable that is then simply updated with new information?

From the docs :

Solidity events give an abstraction on top of the EVM's logging functionality. Applications can subscribe and listen to these events through the RPC interface of an Ethereum client.

It's easier for an off-chain app to subscribe to new event logs than to a variable change. Especially when the variable is not public .

Same goes for querying historical event logs (easy through the JSON-RPC API and its wrappers such as Web3 or Ethers.js), vs. historical changes of the variable (complicated, would need to query a node for each block and look for the changes proactively).

Example: The ERC-20 token standard defines the Transfer() event. A token contract emits this event each time a transfer (of its tokens) occurs. This allows a blockchain explorer (or any other off-chain app) to react to this event - for example to update their own database of token holders. Without the event, they would have no way (or a very complicated way at least) to learn about the transfer.

  • Events in Solidity can be used to log certain events in EVM logs. These are quite useful when external interfaces are required to be notified of any change or event in the contract. These logs are stored on the blockchain in transaction logs . Logs cannot be accessed from the contracts but are used as a mechanism to notify change of state or the occurrence of an event in the contract.

  • Events are piece of data executed on the blockchain and stored in the blockchain but not accessible by any smart contracts. it is kinda console.log in javascript or print in python.

  • Events are much more gas efficient than using a storage variable

  • During the execution of the transaction, a transaction substate gets created and it is processed after the execution completes. Transaction substate is a tuple that consists of 4 items. One of the items is Log Series which is an indexed series of checkpoints that allows the monitoring and notification of contract calls to the entities external to the Ethereum environment, such as application frontends. It works like a trigger mechanism that is executed every time a specific function is invoked, or a specific event occurs. Logs are created in response to events occurring in the smart contract and this is the area where the output of the events emitting from the smart contracts is stored.

  • Deposit contracts were created on the Ethereum 1.0 chain. This kind of smart contract is used for depositing ETH on the beacon chain. An event is emitted every time a deposit is made.

  • There are two events that must be present in an ERC-20-compliant token:

    • Transfer : This event must trigger when tokens are transferred, including any zero-value transfers. The event is defined as follows:
     event Transfer(address indexed _from, address indexed _to, uint256 _value)
    • Approval : This event must trigger when a successful call is made to the approve function.
     event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Solidity events are pieces of data that are emitted and stored in the blockchain. When you emit an event it creates a log that front-end applications can use to trigger changes in the UI.

It's a cheap form of storage.

You can define an event like this:

event Message(address indexed sender, address indexed recipient, string message);

and emit an event like this:

emit Message(msg.sender, _recipient, "Hello World!");

Read this article for more information and this one to learn how to get events in JavaScript using Ethers.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM