-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
1,789 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
pub mod storage; | ||
mod test; | ||
|
||
use soroban_sdk::{contractclient, symbol_short, Address, Env}; | ||
|
||
/// Mintable Trait for Fungible Token | ||
/// | ||
/// The `Mintable` trait extends the `FungibleToken` trait to provide the | ||
/// capability to mint tokens. This trait is designed to be used in conjunction | ||
/// with the `FungibleToken` trait. | ||
/// | ||
/// Excluding the `mint` functionality from the `[FungibleToken]` trait | ||
/// is a deliberate design choice to accommodate flexibility and customization | ||
/// for various smart contract use cases. | ||
#[contractclient(name = "FungibleMintableClient")] | ||
pub trait FungibleMintable { | ||
/// Creates `amount` of tokens and assigns them to `account`. Updates | ||
/// the total supply accordingly. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
/// * `account` - The address receiving the new tokens. | ||
/// * `amount` - The amount of tokens to mint. | ||
/// | ||
/// # Events | ||
/// | ||
/// * topics - `["mint", account: Address]` | ||
/// * data - `[amount: i128]` | ||
/// | ||
/// # Notes | ||
/// | ||
/// We recommend using the [`crate::extensions::mintable::storage::mint()`] | ||
/// function from the `storage` module when implementing this function. | ||
/// | ||
/// 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 ################## | ||
|
||
/// Emits an event indicating a mint of tokens. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to Soroban environment. | ||
/// * `account` - The address receiving the new tokens. | ||
/// * `amount` - The amount of tokens to mint. | ||
/// | ||
/// # Events | ||
/// | ||
/// * topics - `["mint", account: Address]` | ||
/// * data - `[amount: i128]` | ||
pub fn emit_mint(e: &Env, account: &Address, amount: i128) { | ||
let topics = (symbol_short!("mint"), account); | ||
e.events().publish(topics, amount) | ||
} |
36 changes: 36 additions & 0 deletions
36
contracts/token/fungible/src/extensions/mintable/storage.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use soroban_sdk::{Address, Env}; | ||
|
||
use crate::{extensions::mintable::emit_mint, storage::update}; | ||
|
||
/// Creates `amount` of tokens and assigns them to `account`. Updates | ||
/// the total supply accordingly. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
/// * `account` - The address receiving the new tokens. | ||
/// * `amount` - The amount of tokens to mint. | ||
/// | ||
/// # Events | ||
/// | ||
/// * topics - `["mint", account: Address]` | ||
/// * data - `[amount: i128]` | ||
/// | ||
/// # Notes | ||
/// | ||
/// IMPORTANT: This function lacks authorization controls. It is the | ||
/// responsibility of the implementer to establish appropriate access | ||
/// controls to ensure that only authorized accounts can execute minting | ||
/// operations. Failure to implement proper authorization could lead to | ||
/// security vulnerabilities and unauthorized token creation. | ||
/// | ||
/// You probably want to do something like this (pseudo-code): | ||
/// | ||
/// ```ignore | ||
/// let admin = read_administrator(e)?; | ||
/// admin.require_auth()?; | ||
/// ``` | ||
pub fn mint(e: &Env, account: &Address, amount: i128) { | ||
update(e, None, Some(account), amount); | ||
emit_mint(e, account, amount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![cfg(test)] | ||
|
||
extern crate std; | ||
|
||
use soroban_sdk::{contract, testutils::Address as _, Address, Env}; | ||
|
||
use crate::{ | ||
extensions::mintable::storage::mint, | ||
storage::{balance, total_supply}, | ||
}; | ||
|
||
#[contract] | ||
struct MockContract; | ||
|
||
#[test] | ||
fn mint_works() { | ||
let e = Env::default(); | ||
let address = e.register(MockContract, ()); | ||
let account = Address::generate(&e); | ||
e.as_contract(&address, || { | ||
mint(&e, &account, 100); | ||
assert_eq!(balance(&e, &account), 100); | ||
assert_eq!(total_supply(&e), 100); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod burnable; | ||
pub mod mintable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.