Skip to content

Commit 6f324d8

Browse files
Bring back token event fns but deprecated (#1533)
### What Bring back the token event fns but marked deprecated instead of deleted. ### Why I deleted the event functions in #1489 to encourage use of the new event types, however they should be at least marked deprecated to soften the upgrade path, and help developers immediately find through meaningful deprecated notices what they need to use instead.
1 parent 5006165 commit 6f324d8

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

soroban-sdk/src/_migrating/v23_token_transfer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
//! token_impl::move_balance(&env, &from, &to, amount);
5858
//! // Publish an appropriate transfer event that includes the muxed ID
5959
//! // when it's non-None.
60-
//! token::publish_transfer_to_muxed_address_event(&env, &from, &muxed_to, amount);
60+
//! token::publish_transfer_event(&env, &from, &muxed_to, amount);
6161
//! }
6262
//!
6363
//! mod token_impl {

soroban-sdk/src/token.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,12 @@ pub struct Clawback {
288288
pub amount: i128,
289289
}
290290

291-
/// This is a helper function to publish either a `Transfer` or `TransferMuxed`
292-
/// event based on whether the `to` address is a muxed address or not (i.e.
293-
/// whether it has non-None ID).
294-
pub fn publish_transfer_to_muxed_address_event(
295-
env: &Env,
296-
from: &Address,
297-
to: &MuxedAddress,
298-
amount: i128,
299-
) {
291+
/// Publish a `transfer` event.
292+
///
293+
/// Publishes a [`Transfer`] event if the `to` has no muxed ID.
294+
///
295+
/// Publishes a [`TransferMuxed`] event if the `to` has a muxed ID.
296+
pub fn publish_transfer_event(env: &Env, from: &Address, to: &MuxedAddress, amount: i128) {
300297
if let Some(to_muxed_id) = to.id() {
301298
TransferMuxed {
302299
from: from.clone(),

soroban-token-sdk/src/event.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use soroban_sdk::{symbol_short, Address, Env, Symbol};
2+
3+
pub struct Events {
4+
env: Env,
5+
}
6+
7+
#[allow(deprecated)]
8+
impl Events {
9+
#[inline(always)]
10+
pub fn new(env: &Env) -> Events {
11+
Events { env: env.clone() }
12+
}
13+
14+
#[deprecated = "use soroban_sdk::token::Approve::publish"]
15+
pub fn approve(&self, from: Address, to: Address, amount: i128, expiration_ledger: u32) {
16+
let topics = (Symbol::new(&self.env, "approve"), from, to);
17+
self.env
18+
.events()
19+
.publish(topics, (amount, expiration_ledger));
20+
}
21+
22+
#[deprecated = "use soroban_sdk::token::publish_transfer_event"]
23+
pub fn transfer(&self, from: Address, to: Address, amount: i128) {
24+
let topics = (symbol_short!("transfer"), from, to);
25+
self.env.events().publish(topics, amount);
26+
}
27+
28+
#[deprecated = "use soroban_sdk::token::Mint::publish"]
29+
pub fn mint(&self, admin: Address, to: Address, amount: i128) {
30+
let topics = (symbol_short!("mint"), admin, to);
31+
self.env.events().publish(topics, amount);
32+
}
33+
34+
#[deprecated = "use soroban_sdk::token::Clawback::publish"]
35+
pub fn clawback(&self, admin: Address, from: Address, amount: i128) {
36+
let topics = (symbol_short!("clawback"), admin, from);
37+
self.env.events().publish(topics, amount);
38+
}
39+
40+
#[deprecated = "use soroban_sdk::token::SetAuthorized::publish"]
41+
pub fn set_authorized(&self, admin: Address, id: Address, authorize: bool) {
42+
let topics = (Symbol::new(&self.env, "set_authorized"), admin, id);
43+
self.env.events().publish(topics, authorize);
44+
}
45+
46+
#[deprecated = "use soroban_sdk::token::SetAdmin::publish"]
47+
pub fn set_admin(&self, admin: Address, new_admin: Address) {
48+
let topics = (symbol_short!("set_admin"), admin);
49+
self.env.events().publish(topics, new_admin);
50+
}
51+
52+
#[deprecated = "use soroban_sdk::token::Burn::publish"]
53+
pub fn burn(&self, from: Address, amount: i128) {
54+
let topics = (symbol_short!("burn"), from);
55+
self.env.events().publish(topics, amount);
56+
}
57+
}

soroban-token-sdk/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![no_std]
22

3+
use crate::event::Events;
34
use crate::metadata::Metadata;
45
use soroban_sdk::Env;
56

7+
pub mod event;
68
pub mod metadata;
79

810
#[derive(Clone)]
@@ -17,4 +19,8 @@ impl TokenUtils {
1719
pub fn metadata(&self) -> Metadata {
1820
Metadata::new(&self.0)
1921
}
22+
23+
pub fn events(&self) -> Events {
24+
Events::new(&self.0)
25+
}
2026
}

0 commit comments

Comments
 (0)