NEWTON

NEWTON


Popular tags

    Why will I use events in Cairo language?

    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?

      cairostarkneteventsempiric

    Newton

    asked

    3 months ago


    1 answers

    0

    Accepted answer

    Why use events in Cairo?

    To communicate return values to the front-end client

    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.

    As a cheaper means of storage

    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.

    More transparency

    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

    Your answer

    NEWTON

    NEWTON