IntoEvent

Derive Macro IntoEvent 

Source
#[derive(IntoEvent)]
{
    // Attributes available to this derive:
    #[event_name]
    #[datum]
    #[data]
}
Expand description

Implements the Event trait for a Stellar contract event.

Fields without a #[data] attribute are used as topics, while fields with #[data] are used as event data. The event name can be specified with #[event_name(...)] or will default to the struct name in snake_case (minus “Event” suffix).

§Data payload encoding

#[data] publishes the data payload as a Vec<Val>, even when there is only one such field. #[datum] publishes a single field as a bare Val instead of a one-element Vec. Pick one encoding per event; the derive rejects at compile time:

  • a tuple struct, since unnamed fields cannot be emitted;
  • a field carrying more than one #[data]/#[datum] attribute;
  • more than one #[datum] field, or a #[datum] combined with any #[data] field, since only the first data field would be published.

§Example

ⓘ
use core::fmt::Debug;
use stellar_axelar_std::events::Event;
use stellar_axelar_std::IntoEvent;
use stellar_axelar_std::{Address, contract, contractimpl, Env, String};

#[derive(Debug, PartialEq, IntoEvent)]
#[event_name("transfer")]
pub struct TransferEvent {
    pub from: Address,
    pub to: Address,
    #[data]
    pub amount: String,
}

#[contract]
pub struct Token;

#[contractimpl]
impl Token {
    pub fn transfer(env: &Env, to: Address, amount: String) {
        // ... transfer logic ...

        // Generates event with:
        // - Topics: ["transfer", contract_address, to]
        // - Data: [amount]
        TransferEvent {
            from: env.current_contract_address(),
            to,
            amount,
        }.emit(env);
    }
}
}