From 8712fd0f716134bb4de514a11c60787f7b4cc69f Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Fri, 20 Dec 2024 14:24:42 -0800 Subject: [PATCH] fix event section --- .../example-contracts/tokens.mdx | 52 ++----------------- 1 file changed, 3 insertions(+), 49 deletions(-) diff --git a/docs/build/smart-contracts/example-contracts/tokens.mdx b/docs/build/smart-contracts/example-contracts/tokens.mdx index 44fe7cb76..385802d09 100644 --- a/docs/build/smart-contracts/example-contracts/tokens.mdx +++ b/docs/build/smart-contracts/example-contracts/tokens.mdx @@ -78,7 +78,6 @@ mod admin; mod allowance; mod balance; mod contract; -mod event; mod metadata; mod storage_types; mod test; @@ -394,48 +393,6 @@ impl token::Interface for Token { } ``` - - - -```rust title="token/src/event.rs" -use soroban_sdk::{Address, Env, Symbol, symbol_short}; - -pub(crate) fn approve(e: &Env, from: Address, to: Address, amount: i128, expiration_ledger: u32) { - let topics = (Symbol::new(e, "approve"), from, to); - e.events().publish(topics, (amount, expiration_ledger)); -} - -pub(crate) fn transfer(e: &Env, from: Address, to: Address, amount: i128) { - let topics = (symbol_short!("transfer"), from, to); - e.events().publish(topics, amount); -} - -pub(crate) fn mint(e: &Env, admin: Address, to: Address, amount: i128) { - let topics = (symbol_short!("mint"), admin, to); - e.events().publish(topics, amount); -} - -pub(crate) fn clawback(e: &Env, admin: Address, from: Address, amount: i128) { - let topics = (symbol_short!("clawback"), admin, from); - e.events().publish(topics, amount); -} - -pub(crate) fn set_authorized(e: &Env, admin: Address, id: Address, authorize: bool) { - let topics = (Symbol::new(e, "set_authorized"), admin, id); - e.events().publish(topics, authorize); -} - -pub(crate) fn set_admin(e: &Env, admin: Address, new_admin: Address) { - let topics = (symbol_short!("set_admin"), admin); - e.events().publish(topics, new_admin); -} - -pub(crate) fn burn(e: &Env, from: Address, amount: i128) { - let topics = (symbol_short!("burn"), from); - e.events().publish(topics, amount); -} -``` - @@ -515,13 +472,10 @@ You have likely noticed that this example contract is broken into discrete modul For example, most of the token logic exists in the `contract.rs` module. Functions like `mint`, `burn`, `transfer`, etc. are written and programmed in that file. The Token Interface describes how some of these functions should emit events when they occur. However, keeping all that event-emitting logic bundled in with the rest of the contract code could make it harder to track what is happening in the code, and that confusion could ultimately lead to errors. -Instead, we have a separate `events.rs` module that takes away all the headache of emitting events when other functions run. Here is the function to emit an event whenever the token is minted: +Instead, we have a separate `soroban_token_sdk::TokenUtils` module that takes away all the headache of emitting events when other functions run. Here is the event emitted when a token is minted: ```rust -pub(crate) fn mint(e: &Env, admin: Address, to: Address, amount: i128) { - let topics = (symbol_short!("mint"), admin, to); - e.events().publish(topics, amount); -} +TokenUtils::new(&e).events().mint(admin, to, amount); ``` Admittedly, this is a simple example, but constructing the contract this way makes it very clear to the developer what is happening and where. This function is then used by the `contract.rs` module whenever the `mint` function is invoked: @@ -536,7 +490,7 @@ fn mint(e: Env, to: Address, amount: i128) { admin.require_auth(); receive_balance(&e, to.clone(), amount); // highlight-next-line - event::mint(&e, admin, to, amount); + TokenUtils::new(&e).events().mint(admin, to, amount); } ```