Events
#![no_std]
use soroban_sdk::{contract, contractimpl, symbol_short, Env, Symbol, log};
// Define a constant symbol for the counter name
const COUNTER: Symbol = symbol_short!("COUNTER");
#[contract]
pub struct CounterEvent;
// Implement the functionality for CounterEvent
#[contractimpl]
impl CounterEvent {
/// This function increments a counter and returns the new value.
pub fn increment(env: Env) {
// Get the current counter value. If it doesn't exist, assume it's 0.
let mut count = env.storage().instance().get(&COUNTER).unwrap_or(0);
// Increase the count by 1.
count += 1;
// Save the new count in storage.
env.storage().instance().set(&COUNTER, &count);
// Announce that the counter has been incremented.
// This includes the counter name, "increment" as the event type,
// and the new count as data.
const INCREMENT: Symbol = symbol_short!("increment");
env.events().publish((COUNTER, INCREMENT), count);
// Return the new counter value.
log!(&env, "The incremented count is {}", count);
}
}
Guide
#![no_std]
This attribute prevents linking to the standard library, making the code lighter and more efficient for Soroban contracts. It's big so we save on size.
use soroban_sdk::{contract, contractimpl, Env, log}
Imports stuffs from the Soroban SDK. Env
is basic Soroban type, we need it because we can't use the Rust standard library.
const COUNTER
Sets a counter variable to be used in setting the increment to track event functionality.
#[contract]
Marks the struct as a Soroban smart contract. Soroban smart contracts are defined as Rust structs.
#[contractimpl]
Marks the implementation block as containing contract methods and transforms it to code that Soroban can evaluate directly
pub
Marks a function as external, meaning it can be invoked outside of the context of the contract code it
self.
Explanation
Uses the instance method on the storage module within env to get and settl the count, previously set and tracked in the COUNTER variable.
Sets an increment variable to increase the counter value per interation. Count is increased by one with each run of the function. The output is a log of the incremented count.
Run in Playground
Loading playground...