Skip to content

Commit

Permalink
Split up the token interface trait (#1012)
Browse files Browse the repository at this point in the history
### What
Separate admin functions in the token interface out to a separate trait.

### Why
The primary standard interface of tokens won't include the admin
functions. Although many tokens on Soroban will have the admin functions
because all Stellar Asset Contracts will have them.

We still want it to be convenient for folks to follow that same admin
pattern if its a fit, but contracts shouldn't have to implement them if
their follow a different pattern for admin operations.

This issue was discussed [on
Discord](https://discord.com/channels/897514728459468821/1114325793926037605),
and some folks suggested allowances should also be broken into a
separate trait. However this seemed more controversial and there were
also opinions that they shouldn't be separated, so I've kept them in for
now.

Note that the Spec value in the SDK will continue to hold the entire
token interface. It's not particularly useful to split that up, as the
only user of it is the soroban-cli when interacting with Stellar Asset
Contracts. I've hidden it from docs since it isn't routinely useful to
most.

Close #965
  • Loading branch information
leighmcculloch authored Jun 23, 2023
1 parent c20b456 commit 846a8de
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions soroban-sdk/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use crate::{contractclient, contractspecfn, Address, Env, String};
// 2. The implementations have been replaced with a panic.
// 3. &Host type usage are replaced with Env

/// Spec contains the contract spec of Token contracts, such as the Stellar
/// Asset Contract.
/// Spec contains the contract spec of Token contracts, including the general
/// interface, as well as the admin interface, such as the Stellar Asset
/// Contract.
#[doc(hidden)]
pub struct Spec;

/// Interface for Token contracts, such as the Stellar Asset Contract.
Expand Down Expand Up @@ -156,6 +158,46 @@ pub trait Interface {
/// i128]`
fn burn_from(env: Env, spender: Address, from: Address, amount: i128);

/// Returns the number of decimals used to represent amounts of this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn decimals(env: Env) -> u32;

/// Returns the name for this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn name(env: Env) -> String;

/// Returns the symbol for this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn symbol(env: Env) -> String;
}

/// Interface for admin capabilities for Token contracts, such as the Stellar
/// Asset Contract.
#[contractspecfn(name = "Spec", export = false)]
#[contractclient(crate_path = "crate", name = "AdminClient")]
pub trait AdminInterface {
/// Sets the administrator to the specified address `new_admin`.
///
/// # Arguments
///
/// * `new_admin` - The address which will henceforth be the administrator
/// of this token contract.
///
/// # Events
///
/// Emits an event with topics `["set_admin", admin: Address], data =
/// [new_admin: Address]`
fn set_admin(env: Env, new_admin: Address);

/// Sets whether the account is authorized to use its balance. If
/// `authorized` is true, `id` should be able to use its balance.
///
Expand Down Expand Up @@ -197,40 +239,6 @@ pub trait Interface {
/// Emits an event with topics `["clawback", admin: Address, to: Address],
/// data = [amount: i128]`
fn clawback(env: Env, from: Address, amount: i128);

/// Sets the administrator to the specified address `new_admin`.
///
/// # Arguments
///
/// * `new_admin` - The address which will henceforth be the administrator
/// of this token contract.
///
/// # Events
///
/// Emits an event with topics `["set_admin", admin: Address], data =
/// [new_admin: Address]`
fn set_admin(env: Env, new_admin: Address);

/// Returns the number of decimals used to represent amounts of this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn decimals(env: Env) -> u32;

/// Returns the name for this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn name(env: Env) -> String;

/// Returns the symbol for this token.
///
/// # Panics
///
/// If the contract has not yet been initialized.
fn symbol(env: Env) -> String;
}

pub(crate) const SPEC_XDR_INPUT: &[&[u8]] = &[
Expand Down

0 comments on commit 846a8de

Please sign in to comment.