Skip to content

Commit

Permalink
Merge branch 'main' into oligodactylia-parmak
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Jun 23, 2023
2 parents 8c7f0da + 63940e1 commit 45c91af
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 59 deletions.
4 changes: 2 additions & 2 deletions soroban-sdk/src/tests/token_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate as soroban_sdk;

use soroban_sdk::{
token::{Spec, SPEC_XDR_INPUT, SPEC_XDR_LEN},
token::{StellarAssetSpec, SPEC_XDR_INPUT, SPEC_XDR_LEN},
xdr::{Error, ReadXdr, ScSpecEntry},
};

Expand All @@ -15,7 +15,7 @@ fn test_spec_xdr_len() {

#[test]
fn test_spec_xdr() -> Result<(), Error> {
let xdr = Spec::spec_xdr();
let xdr = StellarAssetSpec::spec_xdr();
let mut cursor = std::io::Cursor::new(xdr);
for spec_entry in ScSpecEntry::read_xdr_iter(&mut cursor) {
spec_entry?;
Expand Down
122 changes: 65 additions & 57 deletions soroban-sdk/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ 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.
pub struct Spec;

/// Interface for Token contracts, such as the Stellar Asset Contract.
#[contractspecfn(name = "Spec", export = false)]
#[contractspecfn(name = "StellarAssetSpec", export = false)]
#[contractclient(crate_path = "crate", name = "Client")]
pub trait Interface {
/// Returns the allowance for `spender` to transfer from `from`.
Expand Down Expand Up @@ -156,6 +152,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 = "StellarAssetSpec", export = false)]
#[contractclient(crate_path = "crate", name = "AdminClient")]
pub trait StellarAssetAdminInterface {
/// 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,65 +233,37 @@ 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;
}

/// 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 StellarAssetSpec;

pub(crate) const SPEC_XDR_INPUT: &[&[u8]] = &[
&Spec::spec_xdr_allowance(),
&Spec::spec_xdr_authorized(),
&Spec::spec_xdr_balance(),
&Spec::spec_xdr_burn(),
&Spec::spec_xdr_burn_from(),
&Spec::spec_xdr_clawback(),
&Spec::spec_xdr_decimals(),
&Spec::spec_xdr_decrease_allowance(),
&Spec::spec_xdr_increase_allowance(),
&Spec::spec_xdr_mint(),
&Spec::spec_xdr_name(),
&Spec::spec_xdr_set_admin(),
&Spec::spec_xdr_set_authorized(),
&Spec::spec_xdr_spendable_balance(),
&Spec::spec_xdr_symbol(),
&Spec::spec_xdr_transfer(),
&Spec::spec_xdr_transfer_from(),
&StellarAssetSpec::spec_xdr_allowance(),
&StellarAssetSpec::spec_xdr_authorized(),
&StellarAssetSpec::spec_xdr_balance(),
&StellarAssetSpec::spec_xdr_burn(),
&StellarAssetSpec::spec_xdr_burn_from(),
&StellarAssetSpec::spec_xdr_clawback(),
&StellarAssetSpec::spec_xdr_decimals(),
&StellarAssetSpec::spec_xdr_decrease_allowance(),
&StellarAssetSpec::spec_xdr_increase_allowance(),
&StellarAssetSpec::spec_xdr_mint(),
&StellarAssetSpec::spec_xdr_name(),
&StellarAssetSpec::spec_xdr_set_admin(),
&StellarAssetSpec::spec_xdr_set_authorized(),
&StellarAssetSpec::spec_xdr_spendable_balance(),
&StellarAssetSpec::spec_xdr_symbol(),
&StellarAssetSpec::spec_xdr_transfer(),
&StellarAssetSpec::spec_xdr_transfer_from(),
];

pub(crate) const SPEC_XDR_LEN: usize = 5900;

impl Spec {
impl StellarAssetSpec {
/// Returns the XDR spec for the Token contract.
pub const fn spec_xdr() -> [u8; SPEC_XDR_LEN] {
let input = SPEC_XDR_INPUT;
Expand Down

0 comments on commit 45c91af

Please sign in to comment.