Skip to content

Commit

Permalink
refactor: chain extension tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed May 22, 2024
1 parent 418aa63 commit 92a8465
Show file tree
Hide file tree
Showing 31 changed files with 2,244 additions and 1,904 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scale-info = { version = "2.6", default-features = false, features = ["derive"]
sp-io = { version = "23.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-runtime = { version = "24.0", default-features = false }

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

[lib]
name = "pop_api"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pop_api_trust_backed_assets_example"
name = "fungibles"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
123 changes: 123 additions & 0 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#![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::*};

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum FungiblesError {
// AssetsError(Error),
// /// The origin of the call doesn't have the right permission.
// BadOrigin,
// /// Custom error type for cases in which an implementation adds its own restrictions.
// Custom(String),
/// Not enough balance to fulfill a request is available.
InsufficientBalance,
/// Not enough allowance to fulfill a request is available.
InsufficientAllowance,
/// The asset status is not the expected status.
IncorrectStatus,
/// The asset ID is already taken.
InUse,
/// Minimum balance should be non-zero.
MinBalanceZero,
/// The signing account has no permission to do the operation.
NoPermission,
// /// Safe transfer check fails (e.g. if the receiving contract does not accept tokens).
// SafeTransferCheckFailed(String),
/// The given asset ID is unknown.
Unknown,
/// Recipient's address is zero.
ZeroRecipientAddress,
/// Sender's address is zero.
ZeroSenderAddress,
}

impl From<Error> for FungiblesError {
fn from(error: Error) -> Self {
match error {
// Error::BalanceLow => Err(InsufficientBalance),
Error::InUse => FungiblesError::InUse,
Error::MinBalanceZero => FungiblesError::MinBalanceZero,
Error::Unknown => FungiblesError::Unknown,
_ => todo!()
}
}
}

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

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

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

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

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

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

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

// #[ink(message)]
// pub fn token_name(id: AssetId) -> Result<Option<Vec<u8>>> {
// token_name(id)
// }
//
// #[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(())
// }
}

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

#[ink::test]
fn default_works() {
PopApiAssetsExample::new();
}
}
}
74 changes: 0 additions & 74 deletions pop-api/examples/trust_backed_assets/lib.rs

This file was deleted.

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

use crate::PopApiError::{Balances, Nfts, TrustBackedAssets, UnknownStatusCode};
use crate::PopApiError::{Balances, Nfts, Assets, UnknownStatusCode};
use ink::{prelude::vec::Vec, ChainExtensionInstance};
use primitives::{cross_chain::*, storage_keys::*};
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};

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

Expand All @@ -38,7 +39,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()?)),
52_000..=52_999 => Err(Assets((status_code - 52_000).try_into()?)),
_ => Err(UnknownStatusCode(status_code)),
}
}
Expand Down
Loading

0 comments on commit 92a8465

Please sign in to comment.