juno icon indicating copy to clipboard operation
juno copied to clipboard

Support events

Open tkstanczak opened this issue 3 years ago • 0 comments

https://starknet.io/documentation/events/

EVENTS A contract may emit events throughout its execution. Each event contains the following fields:

from_address: address of the contract emitting the events keys: a list of field elements data: a list of field elements Keys are fields that can be used for indexing the events, while the data may contain any information that we wish to log (note that we are dealing with two separate lists of possibly varying size, rather than a list of key-value pairs).

EMITTING EVENTS Events can be defined in a contract using the @event decorator. Once an event E has been defined, the compiler automatically adds the function E.emit(). The following example illustrates how an event is defined and then emitted:

@event func message_received(a : felt, b: felt): end

message_received.emit(1,2) The emit function emits an event with a single key, which is an identifier of the event, given by sn_keccak(event_name), where event_name is the ASCII encoding of the event’s name. To emit custom keys, one should use the low level emit_event system call, which takes an array of keys and an array of data as input.

Note that the emit function may take structs or arrays (with an adjacent parameter indicating the array’s length) as arguments, which will be flattened into a single array of felts.

The emitted events are part of the transaction receipt.

EVENT ABI The event definition appears in the contract’s ABI. It contains the list of data fields (name and type) and the list of the custom keys (that is, all keys except the event identifier discussed above). Below is an example of an event inside the ABI:

{ "data": [ { "name": "a", "type": "felt" }, { "name": "b", "type": "felt" } ], "keys": [], "name": "message_received", "type": "event" } EVENT HASH The event hash is given by: h(h(h(h(0,from_address),keys_hash),data_hash),3)

Where: keys_hash, data_hash are the hashes of the keys list and data list correspondingly.

The hash of an array of n elements is given by: h(...h(h(0,a[1)),a[2]),...,a[n]),n) h is the pedersen hash function

The event hashes are eventually included in the event_commitment field of a block.

tkstanczak avatar Jan 29 '22 16:01 tkstanczak