Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/fungible-capped/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ExampleContract {
}

pub fn mint(e: &Env, to: Address, amount: i128) {
check_cap(e, amount);
check_cap(e, amount, Base::total_supply(e));
Base::mint(e, &to, amount);
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/tokens/src/fungible/extensions/capped/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
/// This module provides the following helper functions:
/// - `set_cap`: Sets the maximum token supply.
/// - `query_cap`: Returns the maximum token supply.
/// - `check_cap`: Panics if minting a specified `amount` would exceed the cap.
/// Should be used before calling `mint()`.
/// - `check_cap`: Panics if minting a specified `amount` added to the given
/// `total_supply` would exceed the cap. Should be used before calling
/// `mint()`.
mod storage;
pub use self::storage::{check_cap, query_cap, set_cap, CapStorageKey};
#[cfg(test)]
Expand Down
13 changes: 6 additions & 7 deletions packages/tokens/src/fungible/extensions/capped/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_sdk::{contracttype, panic_with_error, Env};

use crate::fungible::{FungibleStorageKey, FungibleTokenError};
use crate::fungible::FungibleTokenError;

/// Storage key for the cap value
#[contracttype]
Expand Down Expand Up @@ -54,25 +54,24 @@ pub fn query_cap(e: &Env) -> i128 {
.unwrap_or_else(|| panic_with_error!(e, FungibleTokenError::CapNotSet))
}

/// Panics if new `amount` of tokens added to the current supply will exceed the
/// maximum supply.
/// Panics if new `amount` of tokens added to the given `total_supply` 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.
/// * `total_supply` - The current total supply of tokens.
///
/// # Errors
///
/// * refer to [`query_cap`] errors.
/// * [`FungibleTokenError::MathOverflow`] - Occurs when the sum of the new
/// amount and the current total supply will overflow.
/// * [`FungibleTokenError::ExceededCap`] - Occurs when the new amount of tokens
/// will exceed the cap.
pub fn check_cap(e: &Env, amount: i128) {
/// * refer to [`query_cap`] errors.
pub fn check_cap(e: &Env, amount: i128, total_supply: i128) {
let cap: i128 = query_cap(e);
let total_supply: i128 =
e.storage().instance().get(&FungibleStorageKey::TotalSupply).unwrap_or(0);
let Some(sum) = total_supply.checked_add(amount) else {
panic_with_error!(e, FungibleTokenError::MathOverflow);
};
Expand Down
12 changes: 6 additions & 6 deletions packages/tokens/src/fungible/extensions/capped/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_mint_under_cap() {
e.as_contract(&contract_address, || {
set_cap(&e, 1000);

check_cap(&e, 500);
check_cap(&e, 500, Base::total_supply(&e));
Base::mint(&e, &user, 500);
Comment thread
brozorec marked this conversation as resolved.
Resolved

assert_eq!(Base::balance(&e, &user), 500);
Expand All @@ -36,7 +36,7 @@ fn test_mint_exact_cap() {
e.as_contract(&contract_address, || {
set_cap(&e, 1000);

check_cap(&e, 1000);
check_cap(&e, 1000, Base::total_supply(&e));
Base::mint(&e, &user, 1000);

assert_eq!(Base::balance(&e, &user), 1000);
Expand All @@ -54,7 +54,7 @@ fn test_mint_exceeds_cap() {
e.as_contract(&contract_address, || {
set_cap(&e, 1000);

check_cap(&e, 1001);
check_cap(&e, 1001, Base::total_supply(&e));
Base::mint(&e, &user, 1001); // This should panic
});
}
Expand All @@ -70,14 +70,14 @@ fn test_mint_multiple_exceeds_cap() {
set_cap(&e, 1000);

// Mint 600 tokens first
check_cap(&e, 600);
check_cap(&e, 600, Base::total_supply(&e));
Base::mint(&e, &user, 600);

assert_eq!(Base::balance(&e, &user), 600);
assert_eq!(Base::total_supply(&e), 600);

// Attempt to mint 500 more tokens (would exceed cap)
check_cap(&e, 500);
check_cap(&e, 500, Base::total_supply(&e));
Base::mint(&e, &user, 500); // This should panic
});
}
Expand All @@ -93,7 +93,7 @@ fn test_check_cap_overflows() {
set_cap(&e, i128::MAX);
Base::mint(&e, &user, i128::MAX);

check_cap(&e, 1); // should overflow
check_cap(&e, 1, Base::total_supply(&e)); // should overflow
});
}

Expand Down
Loading