Skip to content

Commit

Permalink
refactor: draft final design
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jul 4, 2024
1 parent 7757319 commit 924df85
Show file tree
Hide file tree
Showing 25 changed files with 1,013 additions and 1,160 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ edition = "2021"
enumflags2 = { version = "0.7.7" }
ink = { version = "5.0.0", default-features = false }
sp-io = { version = "31.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-runtime = { version = "32.0.0", default-features = false }

pop-primitives = { path = "../primitives", features = ["devnet"], default-features = false }
pop-primitives = { path = "../primitives", default-features = false }

[lib]
name = "pop_api"
Expand All @@ -25,7 +24,6 @@ std = [
"ink/std",
"pop-primitives/std",
"sp-io/std",
"sp-runtime/std",
]
assets = ["pop-primitives/assets"]
balances = []
Expand Down
119 changes: 31 additions & 88 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::{self as api},
error::StatusCode,
primitives::{AccountId as AccountId32, AssetId},
primitives::AssetId,
StatusCode,
};

pub type Result<T> = core::result::Result<T, StatusCode>;

#[ink::contract(env = pop_api::Environment)]
#[ink::contract]
mod fungibles {
use super::*;

Expand All @@ -41,141 +41,84 @@ mod fungibles {

#[ink(message)]
pub fn total_supply(&self, id: AssetId) -> Result<Balance> {
// api::total_supply(id).map_err(|e| e.into())
api::total_supply(id)
}

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

#[ink(message)]
pub fn allowance(
&self,
id: AssetId,
owner: AccountId32,
spender: AccountId32,
) -> Result<Balance> {
// api::allowance(id, owner, spender).map_err(|e| e.into())
pub fn allowance(&self, id: AssetId, owner: AccountId, spender: AccountId) -> Balance {
api::allowance(id, owner, spender)
}

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

let result = api::transfer(id, to, value);
ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
result
pub fn transfer(&self, id: AssetId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(id, to, value)?;
Ok(())
}

#[ink(message)]
pub fn transfer_from(
&self,
id: AssetId,
from: AccountId32,
to: AccountId32,
from: AccountId,
to: AccountId,
value: Balance,
// In the standard a `[u8]`, but the size needs to be known at compile time.
_data: Vec<u8>,
) -> Result<()> {
ink::env::debug_println!(
"PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}",
id,
from,
to,
value,
);

let result = api::transfer_from(id, from, to, value);
ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
result
api::transfer_from(id, from, to, value)?;
Ok(())
}

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

let result = api::approve(id, spender, value);
ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
result
pub fn approve(&self, id: AssetId, spender: AccountId, value: Balance) -> Result<()> {
api::approve(id, spender, value)?;
Ok(())
}

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

let result = api::increase_allowance(id, spender, value);
ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
result
api::increase_allowance(id, spender, value)?;
Ok(())
}

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

let result = api::decrease_allowance(id, spender, value);
ink::env::debug_println!("Result: {:?}", result);
// result.map_err(|e| e.into())
result
api::decrease_allowance(id, spender, value)?;
Ok(())
}

/// 2. PSP-22 Metadata Interface:
/// - token_name
/// - token_symbol
/// - token_decimals
// 2. PSP-22 Metadata Interface:
// - token_name
// - token_symbol
// - token_decimals

#[ink(message)]
pub fn token_name(&self, id: AssetId) -> Result<Vec<u8>> {
// api::token_name(id).map_err(|e| e.into())
pub fn token_name(&self, id: AssetId) -> Vec<u8> {
api::token_name(id)
}

#[ink(message)]
pub fn token_symbol(&self, id: AssetId) -> Result<Vec<u8>> {
// api::token_symbol(id).map_err(|e| e.into())
pub fn token_symbol(&self, id: AssetId) -> Vec<u8> {
api::token_symbol(id)
}

#[ink(message)]
pub fn token_decimals(&self, id: AssetId) -> Result<u8> {
// api::token_decimals(id).map_err(|e| e.into())
pub fn token_decimals(&self, id: AssetId) -> u8 {
api::token_decimals(id)
}

Expand All @@ -189,7 +132,7 @@ mod fungibles {
// - clear_metadata

// #[ink(message)]
// pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> {
// pub fn create(&self, id: AssetId, admin: AccountId, min_balance: Balance) -> Result<()> {
// ink::env::debug_println!(
// "PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}",
// id,
Expand All @@ -198,8 +141,8 @@ mod fungibles {
// );
// let result = api::create(id, admin, min_balance);
// ink::env::debug_println!("Result: {:?}", result);
// // result.map_err(|e| e.into())
// result
// result.map_err(|e| e.into())
// result
// }

// #[ink(message)]
Expand Down
4 changes: 2 additions & 2 deletions pop-api/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ frame-support = { version = "29.0.0", default-features = false }
frame-system = { version = "29.0.0", default-features = false }
pallet-balances = { version = "29.0.2", default-features = false }
pallet-contracts = { version = "28.0.0", default-features = false }
pop-api = { path = "../.", default-features = false, features = ["assets"] }
pop-primitives = { path = "../../primitives", default-features = false, features = ["assets"] }
pop-runtime-devnet = { path = "../../runtime/devnet", default-features = false }
sp-io = { version = "31.0.0", default-features = false }
sp-runtime = { version = "32.0.0", default-features = false }
Expand All @@ -23,7 +23,7 @@ std = [
"frame-system/std",
"pallet-balances/std",
"pallet-contracts/std",
"pop-api/std",
"pop-primitives/std",
"pop-runtime-devnet/std",
"scale/std",
"sp-io/std",
Expand Down
11 changes: 8 additions & 3 deletions pop-api/integration-tests/src/local_fungibles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use pop_api::error::{
use pop_primitives::error::{
ArithmeticError::*,
Error::{self, *},
TokenError::*,
Expand All @@ -8,7 +8,10 @@ use pop_api::error::{
const ASSET_ID: AssetId = 1;

fn decoded<T: Decode>(result: ExecReturnValue) -> T {
<T>::decode(&mut &result.data[2..]).unwrap()
match <T>::decode(&mut &result.data[2..]) {
Ok(value) => value,
Err(_) => panic!("\nTest failed by trying to decode result: {:?} into `T`\n", result),
}
}

fn allowance(
Expand Down Expand Up @@ -78,7 +81,9 @@ fn transfer(
) -> ExecReturnValue {
let function = function_selector("transfer");
let params = [function, asset_id.encode(), to.encode(), value.encode()].concat();
do_bare_call(addr, params, 0).expect("should work")
let result = do_bare_call(addr, params, 0).expect("should work");
println!("Transfer result: {:?}", result);
result
}

fn transfer_from(
Expand Down
Loading

0 comments on commit 924df85

Please sign in to comment.