Skip to content

Commit

Permalink
suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgunozerk committed Feb 19, 2025
1 parent fa067da commit 8d87a5f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 26 deletions.
43 changes: 35 additions & 8 deletions contracts/token/fungible/src/extensions/capped/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,26 @@ use crate::{
/// Storage key that maps to [`Cap`]
pub const CAP_KEY: Symbol = symbol_short!("CAP");

/// Set the maximum supply of tokens.
///
/// # Arguments
///
/// * `e` - Access to the Soroban environment.
///
/// # Errors
///
/// * [`FungibleTokenError::InvalidCap`] - Occurs when the provided cap is
/// negative.
///
/// # Notes
///
/// We recommend using this function in the constructor of your smart contract.
/// Cap functionality is designed to be used in junction with the `mintable`
/// extension.
pub fn set_cap(e: &Env, cap: i128) {
e.storage().instance().extend_ttl(INSTANCE_TTL_THRESHOLD, INSTANCE_EXTEND_AMOUNT);
if cap < 0 {
panic_with_error!(e, FungibleTokenError::InvalidCap);
}
e.storage().instance().set(&CAP_KEY, &cap);
}

Expand All @@ -19,27 +37,36 @@ pub fn set_cap(e: &Env, cap: i128) {
///
/// * `e` - Access to the Soroban environment.
///
/// # Errors
///
/// * [`FungibleTokenError::CapNotSet`] - Occurs when the cap has not been set.
///
/// # Returns
///
/// the maximum supply of tokens.
pub fn query_cap(e: &Env) -> i128 {
e.storage().instance().get(&CAP_KEY).unwrap_optimized()
e.storage()
.instance()
.get(&CAP_KEY)
.unwrap_or_else(|| panic_with_error!(e, FungibleTokenError::CapNotSet))
}

/// Panicks if new `amount` of tokens will exceed the maximum supply.
/// Panics if new `amount` of tokens will exceed the maximum supply.
///
/// # Arguments
///
/// * `e` - Access to the Soroban environment.
/// * `amount` - The new amount of tokens to be added to the total supply.
///
/// # Notes
/// # Errors
///
/// We recommend using [`crate::burnable::burn_from()`] when implementing
/// this function.
/// * [`FungibleTokenError::CapNotSet`] - Occurs when the cap has not been set.
pub fn check_cap(e: &Env, amount: i128) {
let cap: i128 = e.storage().instance().get(&CAP_KEY).unwrap_optimized();
e.storage().instance().extend_ttl(INSTANCE_TTL_THRESHOLD, INSTANCE_EXTEND_AMOUNT);
let cap: i128 = e
.storage()
.instance()
.get(&CAP_KEY)
.unwrap_or_else(|| panic_with_error!(e, FungibleTokenError::CapNotSet));
let total_supply = e.storage().instance().get(&StorageKey::TotalSupply).unwrap_or(0);
if cap < amount + total_supply {
panic_with_error!(e, FungibleTokenError::ExceededCap)
Expand Down
8 changes: 4 additions & 4 deletions contracts/token/fungible/src/extensions/mintable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub trait FungibleMintable {
/// We recommend using [`crate::mintable::mint()`] when implementing this
/// function.
///
/// IMPORTANT: Please do not forget that, you probably will want to have
/// some authorization controls for minting tokens.
///
/// NOTE: if you want to add `capped` functionality to this function,
/// If you want to add `capped` functionality to this function,
/// we recommend using [`crate::capped::check_cap()`] when implementing this
/// function. For more details on the `capped` functionality, check
/// [`crate::extensions::capped`], and check the `fungible-capped`
/// example.
///
/// IMPORTANT: Please do not forget that, you probably will want to have
/// some authorization controls for minting tokens.
fn mint(e: &Env, account: Address, amount: i128);
}
// ################## EVENTS ##################
Expand Down
2 changes: 2 additions & 0 deletions contracts/token/fungible/src/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub enum FungibleTokenError {
ExceededCap = 206,
/// Indicates the supplied `cap` is not a valid cap value.
InvalidCap = 207,
/// Indicates the Cap was not set.
CapNotSet = 208,
}

// ################## EVENTS ##################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"ext": "v0"
},
120960
4095
]
],
[
Expand All @@ -66,7 +66,7 @@
},
"ext": "v0"
},
120960
4095
]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"ext": "v0"
},
120960
4095
]
],
[
Expand All @@ -79,7 +79,7 @@
},
"ext": "v0"
},
120960
4095
]
]
]
Expand Down
8 changes: 0 additions & 8 deletions examples/fungible-capped/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ fn create_client<'a>(e: &Env, cap: &i128) -> ExampleContractClient<'a> {
ExampleContractClient::new(e, &address)
}

// #[test]
// fn query_cap_works() {
// let e = Env::default();
// e.register(ExampleContract, (&1000,));

// assert_eq!(query_cap(&e), 1000);
// }

#[test]
fn mint_under_cap() {
let e = Env::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"ext": "v0"
},
120960
4095
]
],
[
Expand All @@ -79,7 +79,7 @@
},
"ext": "v0"
},
120960
4095
]
]
]
Expand Down

0 comments on commit 8d87a5f

Please sign in to comment.