Skip to content

Commit

Permalink
feat pop api crate refactoring and test (#15)
Browse files Browse the repository at this point in the history
* refactor: simplify api usage from contract

* test: add NFT mint unit test to pop api extension

---------

Co-authored-by: Frank Bell <frank@r0gue.io>
  • Loading branch information
peterwht and evilrobot-01 committed Mar 2, 2024
1 parent ce62aa9 commit 20357df
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 145 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions contracts/pop-api-examples/nfts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ edition = "2021"

[dependencies]
ink = { version = "4.3.0", default-features = false }

pop-api = { path = "../../../pop-api", 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 }

sp-io = { version = "23.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-runtime = { version = "24.0.0", default-features = false }

Expand Down
47 changes: 29 additions & 18 deletions contracts/pop-api-examples/nfts/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::{
env::Environment,
prelude::vec::Vec,
};
use pop_api::PopApiError;

use ink::primitives::AccountId;
use sp_runtime::MultiAddress;
use pop_api::impls::pop_network::PopEnv;
use pop_api::impls::pop_network::PopApiError;
#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
SomeError,
}

#[ink::contract(env = crate::PopEnv)]
mod pop_api_extension_demo {
impl From<PopApiError> for ContractError {
fn from(_value: PopApiError) -> Self {
ContractError::SomeError
}
}

use super::PopApiError;
use scale::Encode;
use ink::env::Error as EnvError;
#[ink::contract(env = pop_api::PopEnv)]
mod pop_api_extension_demo {
use super::ContractError;

#[ink(storage)]
#[derive(Default)]
Expand All @@ -34,14 +35,24 @@ mod pop_api_extension_demo {
collection_id: u32,
item_id: u32,
receiver: AccountId,
) {
) -> Result<(), ContractError> {
ink::env::debug_println!("PopApiExtensionDemo::mint_through_runtime: collection_id: {:?} \nitem_id {:?} \nreceiver: {:?}, ", collection_id, item_id, receiver);

let call = pop_api::impls::pop_network::Nfts::mint(collection_id, item_id, receiver);
self.env().extension().dispatch(call);
// simplified API call
pop_api::nfts::mint(collection_id, item_id, receiver)?;

ink::env::debug_println!("PopApiExtensionDemo::mint_through_runtime end");

Ok(())
}
}

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

#[ink::test]
fn default_works() {
PopApiExtensionDemo::new();
}
}
}
}
5 changes: 1 addition & 4 deletions pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ edition = "2021"

[dependencies]
ink = { version = "4.3.0", default-features = false }
# pallet-nfts = {version = "22.0.0", default-features = false}
ink_env = { version = "4.3.0", default-features = false }
sp-runtime = { version = "24.0.0", 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 }

Expand All @@ -20,12 +19,10 @@ crate-type = [
"rlib",
]


[features]
default = ["std"]
std = [
"ink/std",
# "pallet-nfts/std",
"sp-runtime/std",
"scale/std",
"scale-info/std",
Expand Down
2 changes: 0 additions & 2 deletions pop-api/src/impls/mod.rs

This file was deleted.

100 changes: 0 additions & 100 deletions pop-api/src/impls/pop_network.rs

This file was deleted.

1 change: 0 additions & 1 deletion pop-api/src/interfaces/mod.rs

This file was deleted.

77 changes: 75 additions & 2 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,77 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub mod interfaces;
pub mod impls;
pub mod v0;

use ink::{env::Environment, prelude::vec::Vec, ChainExtensionInstance};
use scale;

Check warning on line 6 in pop-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this import is redundant

warning: this import is redundant --> pop-api/src/lib.rs:6:1 | 6 | use scale; | ^^^^^^^^^^ help: remove it entirely | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports = note: `#[warn(clippy::single_component_path_imports)]` on by default
use sp_runtime::MultiSignature;
pub use v0::nfts;
use v0::RuntimeCall;

// Id used for identifying non-fungible collections.
pub type CollectionId = u32;

// Id used for identifying non-fungible items.
pub type ItemId = u32;

type AccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
type Balance = <ink::env::DefaultEnvironment as Environment>::Balance;
type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
type Signature = MultiSignature;

Check warning on line 20 in pop-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

type alias `Signature` is never used

warning: type alias `Signature` is never used --> pop-api/src/lib.rs:20:6 | 20 | type Signature = MultiSignature; | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
type StringLimit = u32;
type KeyLimit = u32;

Check warning on line 22 in pop-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

type alias `KeyLimit` is never used

warning: type alias `KeyLimit` is never used --> pop-api/src/lib.rs:22:6 | 22 | type KeyLimit = u32; | ^^^^^^^^
type MaxTips = u32;

Check warning on line 23 in pop-api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

type alias `MaxTips` is never used

warning: type alias `MaxTips` is never used --> pop-api/src/lib.rs:23:6 | 23 | type MaxTips = u32; | ^^^^^^^

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

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum PopApiError {
PlaceholderError,
}

impl ink::env::chain_extension::FromStatusCode for PopApiError {
fn from_status_code(status_code: u32) -> core::result::Result<(), Self> {
match status_code {
0 => Ok(()),
1 => Err(Self::PlaceholderError),
_ => panic!("encountered unknown status code"),
}
}
}

impl From<scale::Error> for PopApiError {
fn from(_: scale::Error) -> Self {
panic!("encountered unexpected invalid SCALE encoding")
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum PopEnv {}

impl Environment for PopEnv {
const MAX_EVENT_TOPICS: usize = <ink::env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;

type AccountId = <ink::env::DefaultEnvironment as Environment>::AccountId;
type Balance = <ink::env::DefaultEnvironment as Environment>::Balance;
type Hash = <ink::env::DefaultEnvironment as Environment>::Hash;
type BlockNumber = <ink::env::DefaultEnvironment as Environment>::BlockNumber;
type Timestamp = <ink::env::DefaultEnvironment as Environment>::Timestamp;

type ChainExtension = PopApi;
}

#[ink::chain_extension]
pub trait PopApi {
type ErrorCode = PopApiError;

#[ink(extension = 0xfecb)]
#[allow(private_interfaces)]
fn dispatch(call: RuntimeCall) -> crate::Result<Vec<u8>>;
}

fn call_runtime(call: RuntimeCall) -> Result<Vec<u8>> {
<<PopEnv as Environment>::ChainExtension as ChainExtensionInstance>::instantiate()
.dispatch(call)
}
1 change: 1 addition & 0 deletions pop-api/src/v0/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

8 changes: 8 additions & 0 deletions pop-api/src/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod balances;
pub mod nfts;

#[derive(scale::Encode)]
pub(crate) enum RuntimeCall {
#[codec(index = 50)]
Nfts(nfts::NftCalls),
}
Loading

0 comments on commit 20357df

Please sign in to comment.