Skip to content

Commit

Permalink
visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed May 22, 2024
1 parent 92a8465 commit f1df725
Show file tree
Hide file tree
Showing 8 changed files with 3,387 additions and 304 deletions.
2,766 changes: 2,766 additions & 0 deletions pop-api/examples/fungibles/expanded.rs

Large diffs are not rendered by default.

107 changes: 75 additions & 32 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

// Fungibles wrapper contract to allow contracts to interact with local fungibles without the pop api.
use pop_api::{primitives::{AccountId as AccountId32, AssetId}, assets::fungibles::*};
use ink::prelude::vec::Vec;
use pop_api::{
assets::fungibles::*,
primitives::{AccountId as AccountId32, AssetId},
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
Expand Down Expand Up @@ -31,6 +35,7 @@ pub enum FungiblesError {
ZeroRecipientAddress,
/// Sender's address is zero.
ZeroSenderAddress,
UndefinedError,
}

impl From<Error> for FungiblesError {
Expand All @@ -40,7 +45,7 @@ impl From<Error> for FungiblesError {
Error::InUse => FungiblesError::InUse,
Error::MinBalanceZero => FungiblesError::MinBalanceZero,
Error::Unknown => FungiblesError::Unknown,
_ => todo!()
_ => FungiblesError::UndefinedError,
}
}
}
Expand Down Expand Up @@ -74,41 +79,79 @@ mod fungibles {
}

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

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

#[ink(message)]
pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> {
// create(id, admin, min_balance).map_err(From::from)
ink::env::debug_println!(
"PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}",
id,
admin,
min_balance,
);
let result = create(id, admin, min_balance);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
}

#[ink(message)]
pub fn set_metadata(
&self,
id: AssetId,
name: Vec<u8>,
symbol: Vec<u8>,
decimals: u8,
) -> Result<()> {
// set_metadata(id, name, symbol, decimals).map_err(From::from)
ink::env::debug_println!(
"PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
id,
name,
symbol,
decimals,
);
let result = set_metadata(id, name, symbol, decimals);
ink::env::debug_println!("Result: {:?}", result);
result.map_err(From::from)
}

// #[ink(message)]
// pub fn token_name(id: AssetId) -> Result<Option<Vec<u8>>> {
// token_name(id)
// }
// pub fn transfer(
// &self,
// id: u32,
// beneficiary: AccountId32,
// amount: Balance,
// ) -> Result<()> {
// ink::env::debug_println!(
// "PopApiAssetsExample::mint_asset_through_runtime: id: {:?} beneficiary: {:?} amount: {:?}",
// id,
// beneficiary,
// amount
// );
//
// // Check if asset doesn't exist.
// // if !asset_exists(id)? {
// // return Err(FungiblesError::UnknownAsset);
// // }
//
// #[ink(message)]
// pub fn mint_asset(
// &mut self,
// id: u32,
// beneficiary: AccountId,
// amount: Balance,
// ) -> Result<()> {
// ink::env::debug_println!(
// "PopApiAssetsExample::mint_asset_through_runtime: id: {:?} beneficiary: {:?} amount: {:?}",
// id,
// beneficiary,
// amount
// );
//
// // Check if asset doesn't exist.
// if !asset_exists(id)? {
// return Err(FungiblesError::UnknownAsset);
// }
//
// // Mint asset via pop api.
// mint(id, beneficiary, amount)?;
// ink::env::debug_println!(
// "PopApiAssetsExample::mint_asset_through_runtime: asset(s) minted successfully"
// );
// Ok(())
// }
// // Mint asset via pop api.
// let result = mint(id, beneficiary, amount)?;
// ink::env::debug_println!("Result: {:?}", result);
// Ok(())
// }
}

#[cfg(test)]
Expand Down
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, Assets, UnknownStatusCode};
use crate::PopApiError::{Balances, Contracts, Nfts, Assets, UnknownStatusCode};
use ink::{prelude::vec::Vec, ChainExtensionInstance};
use primitives::{AccountId as AccountId32, 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, assets};
pub use v0::{balances, contracts, cross_chain, nfts, relay_chain_block_number, state, assets};

// type AccountId = <Environment as ink::env::Environment>::AccountId;
type AccountId = AccountId32;
Expand All @@ -26,6 +26,7 @@ pub enum PopApiError {
DecodingFailed,
SystemCallFiltered,
Balances(balances::Error),
Contracts(contracts::Error),
Nfts(nfts::Error),
Assets(assets::fungibles::Error),
Xcm(cross_chain::Error),
Expand All @@ -38,6 +39,7 @@ impl ink::env::chain_extension::FromStatusCode for PopApiError {
// CallFiltered originates from `frame_system` with pallet-index 0. The CallFiltered error is at index 5
5 => Err(PopApiError::SystemCallFiltered),
10_000..=10_999 => Err(Balances((status_code - 10_000).try_into()?)),
40_000..=40_999 => Err(Contracts((status_code - 40_000).try_into()?)),
50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)),
52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)),
_ => Err(UnknownStatusCode(status_code)),
Expand Down
Loading

0 comments on commit f1df725

Please sign in to comment.