Events in Solidity
…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.
…when you call them, they cause the arguments to be stored in the transaction’s log – a special data structure in the blockchain. These logs are associated with the address of the contract that emitted them, are incorporated into the blockchain, and stay there as long as a block is accessible (forever as of now, but this might change in the future).In other words, events are an abstraction that allow you to store a transaction’s log information in the blockchain.
Lock.sol
contract that’s included by default in Hardhat.
The event is called Created
and includes the address of the creator and the amount that was sent during the creation of the smart contract. Then, emit
the event in the constructor:
receipts.logs[0].data
field that contains the information emitted by the event but not in a human-readable way, since it is encoded. For that reason, you can use AbiCoder
to decode the raw data.
By running npx hardhat test
, you should be able to see the following:
f39fd6e51aad88f6f4ce6ab8827279cfffb92266
is encoded in the property data, which is the address of the sender.
npx hardhat test
again, an error may occur because the decoding assumes that the data field contains an address
and a uint256
. But by adding the indexed attribute, you are instructing that the events will be added to a special data structure known as “topics”. Topics have some limitations, since the maximum indexed attributes can be up to three parameters and a topic can only hold a single word (32 bytes).
You then need to modify the decoding line in the test file with the following:
f39fd6e51aad88f6f4ce6ab8827279cfffb92266
.