Skip to content

Commit

Permalink
feat: pallet assets pop api integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Apr 9, 2024
1 parent 5708c0d commit 2e8f2d9
Show file tree
Hide file tree
Showing 17 changed files with 1,189 additions and 225 deletions.
308 changes: 159 additions & 149 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions pop-api/examples/trust_backed_assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
25 changes: 25 additions & 0 deletions pop-api/examples/trust_backed_assets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "pop_api_trust_backed_assets_example"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink = { version = "5.0.0", default-features = false }
pop-api = { path = "../../../pop-api", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"pop-api/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []
69 changes: 69 additions & 0 deletions pop-api/examples/trust_backed_assets/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use pop_api::assets::trust_backed as trust_backed_assets;

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
TrustBackedAssetsError(trust_backed_assets::Error),
UnknownAsset,
}

impl From<trust_backed_assets::Error> for ContractError {
fn from(value: trust_backed_assets::Error) -> Self {
ContractError::TrustBackedAssetsError(value)
}
}

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

#[ink(storage)]
#[derive(Default)]
pub struct PopApiExtensionDemo;

impl PopApiExtensionDemo {
#[ink(constructor, payable)]
pub fn new() -> Self {
ink::env::debug_println!("Contract::new");
Default::default()
}

#[ink(message)]
pub fn mint_asset_through_runtime(
&mut self,
id: u32,
beneficiary: AccountId,
amount: Balance,
) -> Result<(), ContractError> {
ink::env::debug_println!(
"Contract::mint_asset_through_runtime: id: {:?} beneficiary: {:?} amount: {:?}",
id,
beneficiary,
amount
);

// Check if asset doesn't exist.
if !trust_backed_assets::asset_exists(id)? {
return Err(ContractError::UnknownAsset);
}

// Mint asset via pop api.
trust_backed_assets::mint(id, beneficiary, amount)?;
ink::env::debug_println!("Contract::mint_asset_through_runtime: asset(s) minted successfully");
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[ink::test]
fn default_works() {
PopApiExtensionDemo::new();
}
}
}

6 changes: 4 additions & 2 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
pub mod primitives;
pub mod v0;

use crate::PopApiError::{Balances, Nfts, UnknownStatusCode};
use crate::PopApiError::{Balances, Nfts, TrustBackedAssets, UnknownStatusCode};
use ink::{prelude::vec::Vec, ChainExtensionInstance};
use primitives::{cross_chain::*, storage_keys::*};
pub use sp_runtime::{BoundedVec, MultiAddress, MultiSignature};
use v0::RuntimeCall;
pub use v0::{balances, cross_chain, nfts, relay_chain_block_number, state};
pub use v0::{balances, cross_chain, nfts, relay_chain_block_number, state, assets};

type AccountId = <Environment as ink::env::Environment>::AccountId;
type Balance = <Environment as ink::env::Environment>::Balance;
Expand All @@ -26,6 +26,7 @@ pub enum PopApiError {
SystemCallFiltered,
Balances(balances::Error),
Nfts(nfts::Error),
TrustBackedAssets(assets::trust_backed::Error),
Xcm(cross_chain::Error),
}

Expand All @@ -37,6 +38,7 @@ impl ink::env::chain_extension::FromStatusCode for PopApiError {
5 => Err(PopApiError::SystemCallFiltered),
10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)),
50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)),
52_000..=52_999 => Err(TrustBackedAssets((status_code - 52_000).try_into()?)),
_ => Err(UnknownStatusCode(status_code)),
}
}
Expand Down
1 change: 1 addition & 0 deletions pop-api/src/v0/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod trust_backed;
Loading

0 comments on commit 2e8f2d9

Please sign in to comment.