Skip to content

Commit

Permalink
refactor: reposition constructor method
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Sep 19, 2024
1 parent 189720e commit 33f6abd
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ mod fungibles {
id: TokenId,
}

impl Fungibles {
/// Instantiate the contract and wrap around an existing token.
///
/// # Parameters
/// * - `token` - The token.
#[ink(constructor, payable)]
pub fn new_existing(id: TokenId) -> PSP22Result<Self> {
// Make sure token exists.
if !api::token_exists(id).unwrap_or_default() {
return Err(PSP22Error::Custom(String::from("Unknown")));
}
let contract = Self { id };
Ok(contract)
}

/// Instantiate the contract and create a new token. The token identifier will be stored
/// in contract's storage.
///
/// # Parameters
/// * - `id` - The identifier of the token.
/// * - `admin` - The account that will administer the token.
/// * - `min_balance` - The minimum balance required for accounts holding this token.
#[ink(constructor, payable)]
pub fn new(id: TokenId, admin: AccountId, min_balance: Balance) -> PSP22Result<Self> {
api::create(id, admin, min_balance).map_err(PSP22Error::from)?;
let contract = Self { id };
contract
.env()
.emit_event(Created { id, creator: contract.env().account_id(), admin });
Ok(contract)
}
}

impl Psp22 for Fungibles {
#[ink(message)]
fn total_supply(&self) -> Balance {
Expand Down Expand Up @@ -102,37 +135,4 @@ mod fungibles {
api::token_decimals(self.id).unwrap_or_default()
}
}

impl Fungibles {
/// Instantiate the contract and wrap around an existing token.
///
/// # Parameters
/// * - `token` - The token.
#[ink(constructor, payable)]
pub fn new_existing(id: TokenId) -> PSP22Result<Self> {
// Make sure token exists.
if !api::token_exists(id).unwrap_or_default() {
return Err(PSP22Error::Custom(String::from("Unknown")));
}
let contract = Self { id };
Ok(contract)
}

/// Instantiate the contract and create a new token. The token identifier will be stored
/// in contract's storage.
///
/// # Parameters
/// * - `id` - The identifier of the token.
/// * - `admin` - The account that will administer the token.
/// * - `min_balance` - The minimum balance required for accounts holding this token.
#[ink(constructor, payable)]
pub fn new(id: TokenId, admin: AccountId, min_balance: Balance) -> PSP22Result<Self> {
api::create(id, admin, min_balance).map_err(PSP22Error::from)?;
let contract = Self { id };
contract
.env()
.emit_event(Created { id, creator: contract.env().account_id(), admin });
Ok(contract)
}
}
}

0 comments on commit 33f6abd

Please sign in to comment.