event

Attribute Macro event 

Source
#[event]
Expand description

Defines event for using within Gear and Ethereum ecosystem.

Trait SailsEvent provides a uniform interface to encode an event into a tuple of variant name and data payload.

Trait EthEvent provides a uniform interface to convert an event into the topics and data payload that are used to emit logs in the Ethereum Virtual Machine (EVM). The logs generated by the EVM consist of:

  • Topics: An array of 32-byte values. The first topic is always the keccak256 hash of the event signature, while the remaining topics correspond to indexed fields. For dynamic types (as determined by <T as alloy_sol_types::SolType>::IS_DYNAMIC), the ABI-encoded value is hashed before being stored. For static types, the ABI-encoded value is left-padded with zeros to 32 bytes.
  • Data: A byte array containing the ABI-encoded non-indexed fields of the event, encoded as a tuple.

This is intended to be used with the #[sails_rs::event] procedural macro, which automatically implements the trait for your enum-based event definitions.

§Examples

Given an event definition:

#[sails_rs::event]
#[derive(sails_rs::Encode, sails_rs::TypeInfo)]
#[codec(crate = sails_rs::scale_codec)]
#[scale_info(crate = sails_rs::scale_info)]
pub enum Events {
    MyEvent {
        #[indexed]
        sender: uint128,
        amount: uint128,
        note: String,
    },
}

Calling the methods:

let event = Events::MyEvent {
    sender: 123,
    amount: 1000,
    note: "Hello, Ethereum".to_owned(),
};

let topics = event.topics();
let data = event.data();

The first topic will be the hash of the event signature (e.g. "MyEvent(uint128,uint128,String)"), and additional topics and the data payload will be computed based on the field attributes.

§Methods

  • topics(): Returns a vector of 32-byte topics (alloy_primitives::B256) for the event.
  • data(): Returns the ABI-encoded data payload (a Vec<u8>) for the non-indexed fields.