Skip to content

Frank/feat pop api crate #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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 }

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)]
@@ -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
@@ -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 }

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


[features]
default = ["std"]
std = [
"ink/std",
# "pallet-nfts/std",
"sp-runtime/std",
"scale/std",
"scale-info/std",
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;
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;
type StringLimit = u32;
type KeyLimit = u32;
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