NEWTON
Asked
3 months ago
47
views
1
What are events in Cairo Language? What purpose do events serve?
Can you explain it so that everyone can understand?
Newton
asked
3 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
3 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?
Why I randomly get `HttpError: Bad Gateway` from starknet js?
Can tempvar be of a different type, say U256?
What is the technical difference of Kakarot compare to Nethermind's Warp?
How can I prepare my Cairo lang project for an audit?
How to divide felt in Cairo Lang using unsigned_div_rem?
How can I use meta-class of contract (starknetjs) for interaction?
Questions about useStarknetExecute. Do we update the transaction manager on our end? [StarkNet/Cairo]