NEWTON
Asked
5 months ago
51
views
1
What are events in Cairo Language? What purpose do events serve?
Can you explain it so that everyone can understand?
Newton
asked
5 months ago
0
Accepted answer
Events allow a smart contract to log a change of state to the blockchain in a specific format to allow the VM (virtual machine) to easily retrieve and filter them. In StarkNet, when a transaction occurs, the smart contract can emit certain events which your front-end client application can listen to.
In simpler words: events are ways to communicate with a client application that something has happened on the blockchain.
To create an event in Cairo we need the
@event
decorator.
For example, the most common event in smart contracts is the Transfer event that is emitted by ERC20 tokens when someone transfers tokens.
@eventfunc Transfer(from: felt, to: felt, value: Uint256) {}
As you can see, we start by declaring an event decorator. Then we specify a function Transfer that emits three arguments: the address of the sender, (from)
, the address of the receiver (to)
, and the amount being sent.
The event signature is usually declared at the top of the contract code and can be emitted in any function within the contract using the emit
keyword like this:
Transfer.emit(from, to, amount);
Here are three reasons as for why you should use events:
-To communicate return values to the front-end client.
-As a cheaper means of storage.
-More transparency.
Swagtimus.eth
answered
5 months ago
Why will I use events in Cairo language?
Cairo Automatic Events
Is there a way to search a block for events without using `get_transaction_receipt` for each transaction?
What Implicit arguments are in Cairo lang?
How to verify Empiric’s Data Entries Using Events?
Is there a way to find events in a block without getting receipt for all transactions on Starknet?
What libraries are secure to use in Cairo?
Is Kakarot zkEVM a smart contract or a blockchain?
What are multi-calls in Account Abstraction? What will it bring to the UX?
Cairo: What is the difference between let and tempvar?
Any idea why this code doesn’t connect to argentX in brave?
What are function argument types in starknet.js?
How do I connect my DAPP to a private Starknet network using starknetjs?
Implementing the connect wallet. Can you help about connectors?