Skip to content

Commit

Permalink
# This is a combination of 8 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

refactor: general

# This is the commit message #2:

init

# This is the commit message #3:

begin refactor

# This is the commit message #4:

refactor: error handling

# This is the commit message #5:

tests: add error handling tests

# This is the commit message #6:

WIP

# This is the commit message #7:

finalise error handling

# This is the commit message #8:

refactor: easier review
  • Loading branch information
Daanvdplas committed Jun 20, 2024
1 parent ecc82e2 commit e81f78a
Show file tree
Hide file tree
Showing 24 changed files with 3,335 additions and 4,905 deletions.
2,723 changes: 1,788 additions & 935 deletions Cargo.lock

Large diffs are not rendered by default.

2,766 changes: 0 additions & 2,766 deletions pop-api/examples/fungibles/expanded.rs

This file was deleted.

94 changes: 23 additions & 71 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,12 @@
///
use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::*,
assets::use_cases::fungibles as api,
error::PopApiError,
primitives::{AccountId as AccountId32, AssetId},
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
/// The asset is not live; either frozen or being destroyed.
AssetNotLive,
/// The amount to mint is less than the existential deposit.
BelowMinimum,
/// Unspecified dispatch error, providing the index and its error index (if none `0`).
DispatchError { index: u8, error: u8 },
/// Not enough allowance to fulfill a request is available.
InsufficientAllowance,
/// Not enough balance to fulfill a request is available.
InsufficientBalance,
/// The asset ID is already taken.
InUse,
/// Minimum balance should be non-zero.
MinBalanceZero,
/// Unspecified pallet error, providing pallet index and error index.
ModuleError { pallet: u8, error: u16 },
/// The account to alter does not exist.
NoAccount,
/// The signing account has no permission to do the operation.
NoPermission,
/// The given asset ID is unknown.
Unknown,
}

impl From<FungiblesError> for ContractError {
fn from(error: FungiblesError) -> Self {
match error {
FungiblesError::AssetNotLive => ContractError::AssetNotLive,
FungiblesError::BelowMinimum => ContractError::BelowMinimum,
FungiblesError::DispatchError { index, error } => {
ContractError::DispatchError { index, error }
},
FungiblesError::InsufficientAllowance => ContractError::InsufficientAllowance,
FungiblesError::InsufficientBalance => ContractError::InsufficientBalance,
FungiblesError::InUse => ContractError::InUse,
FungiblesError::MinBalanceZero => ContractError::MinBalanceZero,
FungiblesError::ModuleError { pallet, error } => {
ContractError::ModuleError { pallet, error }
},
FungiblesError::NoAccount => ContractError::NoAccount,
FungiblesError::NoPermission => ContractError::NoPermission,
FungiblesError::Unknown => ContractError::Unknown,
}
}
}

/// The fungibles result type.
pub type Result<T> = core::result::Result<T, ContractError>;
pub type Result<T> = core::result::Result<T, PopApiError>;

#[ink::contract(env = pop_api::Environment)]
mod fungibles {
Expand All @@ -74,7 +25,7 @@ mod fungibles {
impl Fungibles {
#[ink(constructor, payable)]
pub fn new() -> Self {
ink::env::debug_println!("PopApiAssetsExample::new");
ink::env::debug_println!("PopApiFungiblesExample::new");
Default::default()
}

Expand All @@ -90,12 +41,12 @@ mod fungibles {
#[ink(message)]
pub fn total_supply(&self, id: AssetId) -> Result<Balance> {
total_supply(id).map_err(From::from)
api::total_supply(id)
}

#[ink(message)]
pub fn balance_of(&self, id: AssetId, owner: AccountId32) -> Result<Balance> {
balance_of(id, owner).map_err(From::from)
api::balance_of(id, owner)
}

#[ink(message)]
Expand All @@ -105,21 +56,21 @@ mod fungibles {
owner: AccountId32,
spender: AccountId32,
) -> Result<Balance> {
allowance(id, owner, spender).map_err(From::from)
api::allowance(id, owner, spender)
}

#[ink(message)]
pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::transfer: id: {:?}, to: {:?} value: {:?}",
"PopApiFungiblesExample::transfer: id: {:?}, to: {:?} value: {:?}",
id,
to,
value,
);

let result = transfer(id, to, value);
let result = api::transfer(id, to, value);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
result
}

#[ink(message)]
Expand All @@ -129,20 +80,21 @@ mod fungibles {
from: Option<AccountId32>,
to: Option<AccountId32>,
value: Balance,
// Size needs to be known at compile time or ink's `Vec`
// In the standard a `[u8]`, but the size needs to be known at compile time ink's `Vec`
// has to be used.
data: Vec<u8>,
) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}",
"PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}",
id,
from,
to,
value,
);

let result = transfer_from(id, from, to, value, &data);
let result = api::transfer_from(id, from, to, value, &data);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
result
}

/// 2. PSP-22 Metadata Interface:
Expand All @@ -162,14 +114,14 @@ mod fungibles {
#[ink(message)]
pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}",
"PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}",
id,
admin,
min_balance,
);
let result = create(id, admin, min_balance);
let result = api::create(id, admin, min_balance);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
result
}

#[ink(message)]
Expand All @@ -181,20 +133,20 @@ mod fungibles {
decimals: u8,
) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
"PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
id,
name,
symbol,
decimals,
);
let result = set_metadata(id, name, symbol, decimals);
let result = api::set_metadata(id, name, symbol, decimals);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
result
}

#[ink(message)]
pub fn asset_exists(&self, id: AssetId) -> Result<bool> {
asset_exists(id).map_err(From::from)
api::asset_exists(id)
}
}

Expand All @@ -204,7 +156,7 @@ mod fungibles {

#[ink::test]
fn default_works() {
PopApiAssetsExample::new();
PopApiFungiblesExample::new();
}
}
}
Loading

0 comments on commit e81f78a

Please sign in to comment.