NEWTON

NEWTON


Popular tags

    What are events in Cairo Language?

    Asked

    7 months ago

    54

    views


    1

    What are events in Cairo Language? What purpose do events serve?

    Can you explain it so that everyone can understand?

      cairostarkneteventsempiric

    Newton

    asked

    7 months ago


    1 answers

    0

    Accepted answer

    What are smart contract events?

    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.

    Events In Cairo

    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);
    

    Why Use Events?

    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

    7 months ago

    Your answer

    NEWTON

    NEWTON