-
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.
* add metadata extension with tests * fix clippy
- Loading branch information
Showing
16 changed files
with
556 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod storage; | ||
pub use self::storage::*; | ||
|
||
mod test; |
70 changes: 70 additions & 0 deletions
70
contracts/token/fungible/src/extensions/metadata/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,70 @@ | ||
use soroban_sdk::{contracttype, symbol_short, unwrap::UnwrapOptimized, Env, String, Symbol}; | ||
|
||
use crate::storage::{INSTANCE_EXTEND_AMOUNT, INSTANCE_TTL_THRESHOLD}; | ||
|
||
/// Storage key that maps to [`Metadata`] | ||
pub const METADATA_KEY: Symbol = symbol_short!("METADATA"); | ||
|
||
/// Storage container for token metadata | ||
#[contracttype] | ||
pub struct Metadata { | ||
pub decimals: u32, | ||
pub name: String, | ||
pub symbol: String, | ||
} | ||
|
||
/// Returns the token metadata such as decimals, name and symbol. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
pub fn get_metadata(e: &Env) -> Metadata { | ||
e.storage().instance().get(&METADATA_KEY).unwrap_optimized() | ||
} | ||
|
||
/// Returns the token decimals. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
pub fn decimals(e: &Env) -> u32 { | ||
get_metadata(e).decimals | ||
} | ||
|
||
/// Returns the token name. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
pub fn name(e: &Env) -> String { | ||
get_metadata(e).name | ||
} | ||
|
||
/// Returns the token symbol. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
pub fn symbol(e: &Env) -> String { | ||
get_metadata(e).symbol | ||
} | ||
|
||
/// Sets the token metadata such as decimals, name and symbol. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `e` - Access to the Soroban environment. | ||
/// * `decimals` - The number of decimals. | ||
/// * `name` - The name of the token. | ||
/// * `symbol` - The symbol of the token. | ||
/// | ||
/// # Notes | ||
/// | ||
/// **IMPORTANT**: This function lacks authorization controls. You want to | ||
/// invoke it most likely from a constructor or from another function with | ||
/// admin-only authorization. | ||
pub fn set_metadata(e: &Env, decimals: u32, name: String, symbol: String) { | ||
let metadata = Metadata { decimals, name, symbol }; | ||
e.storage().instance().extend_ttl(INSTANCE_TTL_THRESHOLD, INSTANCE_EXTEND_AMOUNT); | ||
e.storage().instance().set(&METADATA_KEY, &metadata); | ||
} |
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,53 @@ | ||
#![cfg(test)] | ||
|
||
use soroban_sdk::{contract, Env, String}; | ||
|
||
use crate::extensions::metadata::{decimals, name, set_metadata, symbol}; | ||
|
||
#[contract] | ||
struct MockContract; | ||
|
||
#[test] | ||
fn set_and_get_metadata() { | ||
let e = Env::default(); | ||
let address = e.register(MockContract, ()); | ||
|
||
e.as_contract(&address, || { | ||
let test_decimals: u32 = 7; | ||
let test_name = String::from_str(&e, "Test Token"); | ||
let test_symbol = String::from_str(&e, "TEST"); | ||
|
||
set_metadata(&e, test_decimals, test_name.clone(), test_symbol.clone()); | ||
|
||
assert_eq!(decimals(&e), test_decimals); | ||
assert_eq!(name(&e), test_name); | ||
assert_eq!(symbol(&e), test_symbol); | ||
}); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn get_unset_metadata() { | ||
let e = Env::default(); | ||
let address = e.register(MockContract, ()); | ||
|
||
e.as_contract(&address, || { | ||
decimals(&e); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn metadata_update() { | ||
let e = Env::default(); | ||
let address = e.register(MockContract, ()); | ||
|
||
e.as_contract(&address, || { | ||
set_metadata(&e, 6, String::from_str(&e, "Initial Name"), String::from_str(&e, "INI")); | ||
|
||
set_metadata(&e, 8, String::from_str(&e, "Updated Name"), String::from_str(&e, "UPD")); | ||
|
||
assert_eq!(decimals(&e), 8); | ||
assert_eq!(name(&e), String::from_str(&e, "Updated Name")); | ||
assert_eq!(symbol(&e), String::from_str(&e, "UPD")); | ||
}); | ||
} |
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,2 +1,3 @@ | ||
pub mod burnable; | ||
pub mod metadata; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,6 @@ pub mod extensions; | |
pub mod fungible; | ||
pub mod storage; | ||
|
||
pub use extensions::metadata; | ||
|
||
mod test; |
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
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.