
Solidity events are indispensable for smart contract developers, allowing us to test specific variables in smart contracts, change the front end in an automated way, etc. In general, knowing how to use events in Solidity can make smart contract development much easier.
Ethereum Virtual MachineEthereum Virtual Machine(EVM)'s log and event functionality, including what logs and events are for, indexing events, and how to use logs and events in Hardhat and Brownie.
loglogFunctions for "writing" data into data structures outside of smart contracts. One of the important data isSolidity events. Events allow us to "print" information on the blockchain in a way that is more searchable and less gas-consuming than saving to public storage variables in smart contracts.
A log is a special data structure on the blockchain. They cannot be accessed by smart contracts, but provide information about transactions and what happened in blocks. It is precisely because they cannot be accessed by smart contracts that they are cheaper to use.
You can also watch the following video about events and logging in Solidity:
https://www.bilibili.com/video/BV1iL4y1s7ro?spm_id_from=333.999.0.0
So, what is an event?
subscriptionsubscriptionThey come to "listen" to certain events. In fact, this isChainlink networkfirst level title
What can events be used for?
Now, if you're not a Chainlink or Ethereum node operator, you might be asking what happened to you. With Solidity events, you can:
Test specific variables in your smart contract;
Index variables to reconstruct stored state;
subgraph
createsubgraphto read data faster;
first level title
What did the event look like?
The following data structure is how an event is defined in Solidity:

You can think of events as a new special type. We've created an event "type" called storedNumber. The name of the event is storedNumber, which can hold some variables. In this event, there are two kinds of parameters: indexed and unindexed. Indexed parameters, also known as "topics", are searchable parameters in events. We'll talk more about these later.
We can then emit an event like this:

The following is a complete contract example:

Now, whenever we call the store function in this example, it emits an event of type storedNumber . Let's look at an example transaction that calls the store function with an input of 1. we can inKovan EtherscanSee this transaction.
Scrolling to the "log" section of the transaction, we can see the following:

An event can be broken down into:
Address: Address, the address of the contract or account that issued the event.
Topics: topic, the index parameter of the event.
you can at

you can atSolidityDocumentationRead more about the event in . "Log" and "event" are often used interchangeably, because as developers of smart contracts, we are usually only concerned with "events" in the log. However, technically, logs also include blockhash, address, and other data returned to your blockchain node by calling eth_getLogs. You can also read more aboutbloom filter, which is why these events can be easily queried.
Events in Hardhat
Now that we understand what events are, let's learn how to access and use them in Hardhat. You can clone the following repo and follow along:
git clone https://github.com/PatrickAlphaC/hardhat-events-logs
cd hardhat-events-logs
You need to follow the instructions in README.md to completeDependency toolinstallation, which includes Node, Yarn, and Git.
If you follow the README.md, you will be able to:
deploy a smart contract;
Create a transaction that emits an event;
View the context of these events.
If you encounter problems during this process, please contact theGithub repofirst level title

Events in Brownie
The events in Brownie are pretty much the same because the contract is exactly the same.
You can clone the following repo and follow along:
You need to follow the instructions in README.md to completeDependency toolinstallation, which includes Node, Python, eth-brownie, and Git.
If you follow the README.md, you will be able to:
deploy a smart contract;
Create a transaction that emits an event;
View the context of these events.
If you encounter problems during this process, please contact theGithub repoSummarize

Summarize
Logs and events are an important part of smart contract development, and are critical infrastructure for projects like Chainlink and The Graph. To learn more about developing powerful smart contracts (using your newfound event skills), be sure to check outChainlink Documentation, start creating now! .


