NEWTON
Asked
3 months ago
47
views
1
Why will I use events in the Cairo language? Can I use it on a front-end client application side?
Newton
asked
3 months ago
0
Accepted answer
One of the caveats of external functions is the fact that your returned values are not accessible outside the contract.
This means that when you make state changes within your external functions you can’t easily access the newly returned state from your front-end client. However thanks to events we can easily emit the new states which are stored in logs and available to our UI.
Below is an example:
// Event declaration
@event
func stored_name(caller: felt, name: felt, status: felt) {}
// External function that emits event
@external
func store_name{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
} (_name: felt) -> (status: felt)
{
let (caller) = get_caller_address();
names.write(caller, _name);
stored_name.emit(caller, _name, 1);
return (1);
}
By default we cannot have direct access to the returned value “status”, but through events we can emit and access it from our front-end client.
Whenever an event is emitted the corresponding logs are written to the blockchain.
These logs contain useful information which can further be accessed by the front-end client, which is way cheaper than normal storage.
Event logs contain the historical data for all emitted events. This can be accessed at any point in time by anyone on StarkNet.
Our contracts are a great example of this. We leverage events to provide transparent and verifiable oracle data.
Swagtimus.eth
answered
3 months ago
What are events in Cairo Language?
Cairo Automatic Events
How to use Access Control in Cairo language securely?
What libraries are secure to use in Cairo?
Is there a way to find events in a block without getting receipt for all transactions on Starknet?
Namespaces in Cairo
Is there a way to search a block for events without using `get_transaction_receipt` for each transaction?
How do I connect my wallet to my Pathfinder node?
Does writing it to zero actually do something?
Testing Starknet Cairo contract function with address
Cairo Array Item Removal
Cairo: Else if statement
Is there any other way to do 2**n without using pow.cairo?
What's the best advanced StarkNet guide?