Skip to content

Commit

Permalink
fix event section
Browse files Browse the repository at this point in the history
  • Loading branch information
sisuresh committed Dec 20, 2024
1 parent 4824916 commit 8712fd0
Showing 1 changed file with 3 additions and 49 deletions.
52 changes: 3 additions & 49 deletions docs/build/smart-contracts/example-contracts/tokens.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ mod admin;
mod allowance;
mod balance;
mod contract;
mod event;
mod metadata;
mod storage_types;
mod test;
Expand Down Expand Up @@ -394,48 +393,6 @@ impl token::Interface for Token {
}
```

</TabItem>
<TabItem value="event">

```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);
}
```

</TabItem>
<TabItem value="metadata">

Expand Down Expand Up @@ -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:
Expand All @@ -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);
}
```

Expand Down

0 comments on commit 8712fd0

Please sign in to comment.