简体   繁体   English

什么是 Solidity 事件

[英]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).很长一段时间以来,我一直在努力寻找对 Solidity(或区块链环境)中的事件的解释。 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?但这与普通的 ol' 变量有何不同? 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. Solidity 事件在 EVM 的日志记录功能之上提供了一个抽象。 Applications can subscribe and listen to these events through the RPC interface of an Ethereum client.应用程序可以通过以太坊客户端的 RPC 接口订阅和监听这些事件。

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 .特别是当变量不是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).查询历史事件日志(通过 JSON-RPC API 及其包装器,如 Web3 或 Ethers.js 很容易)与变量的历史更改(复杂,需要查询每个块的节点并查找主动改变)。

Example: The ERC-20 token standard defines the Transfer() event.示例: ERC-20 代币标准定义了Transfer()事件。 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. Solidity 中的事件可用于在 EVM 日志中记录某些事件。 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 .这些日志存储在区块链中的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.它有点像javascript中的console.log或python中的print

  • 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 ,并在执行完成后对其进行处理。 Transaction substate is a tuple that consists of 4 items.事务子状态是一个由 4 个项目组成的元组。 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.其中一项是Log Series ,它是一系列索引检查点,允许监视和通知对以太坊环境外部实体(例如应用程序前端)的合约调用。 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.存款合约是在以太坊 1.0 链上创建的。 This kind of smart contract is used for depositing ETH on the beacon chain.这种智能合约用于在信标链上存入 ETH。 An event is emitted every time a deposit is made.每次存款时都会发出一个事件。

  • There are two events that must be present in an ERC-20-compliant token:符合 ERC-20 的令牌中必须存在两个事件:

    • Transfer : This event must trigger when tokens are transferred, including any zero-value transfers. Transfer :此事件必须在代币转移时触发,包括任何零值转移。 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. Approval :当成功调用approval函数时必须触发此事件。
     event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Solidity events are pieces of data that are emitted and stored in the blockchain. Solidity 事件是在区块链中发出和存储的数据片段。 When you emit an event it creates a log that front-end applications can use to trigger changes in the UI.当您发出事件时,它会创建一个日志,前端应用程序可以使用该日志来触发 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阅读这篇文章了解更多信息,阅读这篇文章了解如何使用 Ethers.js 在 JavaScript 中获取事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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