Skip to content

Commit

Permalink
merge pop-api
Browse files Browse the repository at this point in the history
  • Loading branch information
al3mart committed Mar 2, 2024
2 parents c15be1b + 6c2b419 commit 5fa282a
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 27 deletions.
12 changes: 12 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();
}
}
}
}
3 changes: 0 additions & 3 deletions pop-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2021"
pop-api-primitives = { path = "./primitives" }

ink = { version = "4.3.0", default-features = false }
# pallet-nfts = {version = "22.0.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"] }
Expand All @@ -23,12 +22,10 @@ crate-type = [
"rlib",
]


[features]
default = ["std"]
std = [
"ink/std",
# "pallet-nfts/std",
"sp-runtime/std",
"scale/std",
"scale-info/std",
Expand Down
75 changes: 75 additions & 0 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,78 @@ pub mod impls;
pub mod interfaces;

use pop_api_primitives::storage_keys;

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

View workflow job for this annotation

GitHub Actions / clippy

unused import: `pop_api_primitives::storage_keys`

warning: unused import: `pop_api_primitives::storage_keys` --> pop-api/src/lib.rs:6:5 | 6 | use pop_api_primitives::storage_keys; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
pub mod v0;

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

Check warning on line 10 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:10:1 | 10 | 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 24 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:24:6 | 24 | type Signature = MultiSignature; | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
type StringLimit = u32;
type KeyLimit = u32;

Check warning on line 26 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:26:6 | 26 | type KeyLimit = u32; | ^^^^^^^^
type MaxTips = u32;

Check warning on line 27 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:27:6 | 27 | 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 5fa282a

Please sign in to comment.