Skip to content

Commit

Permalink
refactor: pop-api examples (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopgalvao authored May 5, 2024
1 parent bc41540 commit 47b563e
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pop-api/examples/balance-transfer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pop_api_extension_demo"
name = "balance_transfer"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
30 changes: 15 additions & 15 deletions pop-api/examples/balance-transfer/lib.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use pop_api::balances;
use pop_api::balances::*;

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

impl From<balances::Error> for ContractError {
fn from(value: balances::Error) -> Self {
impl From<Error> for ContractError {
fn from(value: Error) -> Self {
ContractError::BalancesError(value)
}
}

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

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

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

#[ink(message)]
pub fn transfer_through_runtime(
pub fn transfer(
&mut self,
receiver: AccountId,
value: Balance,
) -> Result<(), ContractError> {
ink::env::debug_println!(
"PopApiExtensionDemo::transfer_through_runtime: \nreceiver: {:?}, \nvalue: {:?}",
"BalanceTransfer::transfer: \nreceiver: {:?}, \nvalue: {:?}",
receiver,
value
);

pop_api::balances::transfer_keep_alive(receiver, value)?;
transfer_keep_alive(receiver, value)?;

ink::env::debug_println!("PopApiExtensionDemo::transfer_through_runtime end");
ink::env::debug_println!("BalanceTransfer::transfer end");
Ok(())
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ mod pop_api_extension_demo {
client.free_balance(receiver).await.expect("Failed to get account balance");

// when
let transfer_message = call_builder.transfer_through_runtime(receiver, TRANSFER_VALUE);
let transfer_message = call_builder.transfer(receiver, TRANSFER_VALUE);

let call_res = client
.call(&ink_e2e::alice(), &transfer_message)
Expand All @@ -131,4 +131,4 @@ mod pop_api_extension_demo {
Ok(())
}
}
}
}
2 changes: 1 addition & 1 deletion pop-api/examples/nfts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pop_api_nft_example"
name = "nfts"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
18 changes: 9 additions & 9 deletions pop-api/examples/nfts/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl From<Error> for ContractError {
}

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

#[ink(storage)]
Expand Down Expand Up @@ -64,34 +64,34 @@ mod pop_api_nfts {
receiver: AccountId,
) -> Result<(), ContractError> {
ink::env::debug_println!(
"Nfts::mint_through_runtime: collection_id: {:?} item_id {:?} receiver: {:?}",
"Nfts::mint: collection_id: {:?} item_id {:?} receiver: {:?}",
collection_id,
item_id,
receiver
);

// Check if item already exists (demo purposes only, unnecessary as would expect check in mint call)
if pop_api::nfts::item(collection_id, item_id)?.is_some() {
if item(collection_id, item_id)?.is_some() {
return Err(ContractError::ItemAlreadyExists);
}

// mint api
pop_api::nfts::mint(collection_id, item_id, receiver)?;
ink::env::debug_println!("Nfts::mint_through_runtime: item minted successfully");
mint(collection_id, item_id, receiver)?;
ink::env::debug_println!("Nfts::mint: item minted successfully");

// check owner
match pop_api::nfts::owner(collection_id, item_id)? {
match owner(collection_id, item_id)? {
Some(owner) if owner == receiver => {
ink::env::debug_println!(
"Nfts::mint_through_runtime success: minted item belongs to receiver"
"Nfts::mint success: minted item belongs to receiver"
);
},
_ => {
return Err(ContractError::NotOwner);
},
}

ink::env::debug_println!("Nfts::mint_through_runtime end");
ink::env::debug_println!("Nfts::mint end");
Ok(())
}

Expand All @@ -113,4 +113,4 @@ mod pop_api_nfts {
Nfts::new();
}
}
}
}
2 changes: 1 addition & 1 deletion pop-api/examples/place-spot-order/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pop_api_spot_order_example"
name = "spot_order"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
40 changes: 11 additions & 29 deletions pop-api/examples/place-spot-order/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use pop_api::nfts;

#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum ContractError {
InvalidCollection,
ItemAlreadyExists,
NftsError(nfts::Error),
NotOwner,
}

impl From<nfts::Error> for ContractError {
fn from(value: nfts::Error) -> Self {
ContractError::NftsError(value)
}
}

#[ink::contract(env = pop_api::Environment)]
mod pop_api_spot_order_example {
use super::ContractError;
mod spot_order {

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

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

Expand All @@ -37,21 +19,21 @@ mod pop_api_spot_order_example {
&mut self,
max_amount: Balance,
para_id: u32,
) -> Result<(), ContractError> {
) {
ink::env::debug_println!(
"Contract::place_spot_order: max_amount {:?} para_id: {:?} ",
"SpotOrder::place_spot_order: max_amount {:?} para_id: {:?} ",
max_amount,
para_id,
);

#[allow(unused_variables)]
let res = pop_api::cross_chain::coretime::place_spot_order(max_amount, para_id);
ink::env::debug_println!(
"Contract::place_spot_order: res {:?} ",
"SpotOrder::place_spot_order: res {:?} ",
res,
);

ink::env::debug_println!("Contract::place_spot_order end");
Ok(())
ink::env::debug_println!("SpotOrder::place_spot_order end");
}
}

Expand All @@ -61,7 +43,7 @@ mod pop_api_spot_order_example {

#[ink::test]
fn default_works() {
PopApiSpotOrderExample::new();
SpotOrder::new();
}
}
}
}
2 changes: 1 addition & 1 deletion pop-api/examples/read-runtime-state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pop_api_extension_demo"
name = "read_relay_blocknumber"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
10 changes: 5 additions & 5 deletions pop-api/examples/read-runtime-state/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract(env = pop_api::Environment)]
mod pop_api_extension_demo {
mod read_relay_blocknumber {
use pop_api::primitives::storage_keys::{
ParachainSystemKeys::LastRelayChainBlockNumber, RuntimeStateKeys::ParachainSystem,
};
Expand All @@ -13,12 +13,12 @@ mod pop_api_extension_demo {

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

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

Expand All @@ -32,4 +32,4 @@ mod pop_api_extension_demo {
});
}
}
}
}
20 changes: 9 additions & 11 deletions runtime/devnet/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/balance-transfer/target/ink/pop_api_extension_demo.wasm",
"../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm",
)
.unwrap();

Expand Down Expand Up @@ -569,10 +569,9 @@ mod tests {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm",
)
.unwrap();
let (wasm_binary, _) =
load_wasm_module::<Runtime>("../../pop-api/examples/nfts/target/ink/nfts.wasm")
.unwrap();

let init_value = 100;

Expand Down Expand Up @@ -649,10 +648,9 @@ mod tests {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm",
)
.unwrap();
let (wasm_binary, _) =
load_wasm_module::<Runtime>("../../pop-api/examples/nfts/target/ink/nfts.wasm")
.unwrap();

let init_value = 100;

Expand Down Expand Up @@ -715,7 +713,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/read-runtime-state/target/ink/pop_api_extension_demo.wasm",
"../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm",
)
.unwrap();

Expand Down Expand Up @@ -774,7 +772,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/place-spot-order/target/ink/pop_api_spot_order_example.wasm",
"../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm",
)
.unwrap();

Expand Down
20 changes: 9 additions & 11 deletions runtime/testnet/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/balance-transfer/target/ink/pop_api_extension_demo.wasm",
"../../pop-api/examples/balance-transfer/target/ink/balance_transfer.wasm",
)
.unwrap();

Expand Down Expand Up @@ -415,10 +415,9 @@ mod tests {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm",
)
.unwrap();
let (wasm_binary, _) =
load_wasm_module::<Runtime>("../../pop-api/examples/nfts/target/ink/nfts.wasm")
.unwrap();

let init_value = 100;

Expand Down Expand Up @@ -495,10 +494,9 @@ mod tests {
new_test_ext().execute_with(|| {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm",
)
.unwrap();
let (wasm_binary, _) =
load_wasm_module::<Runtime>("../../pop-api/examples/nfts/target/ink/nfts.wasm")
.unwrap();

let init_value = 100;

Expand Down Expand Up @@ -561,7 +559,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/read-runtime-state/target/ink/pop_api_extension_demo.wasm",
"../../pop-api/examples/read-runtime-state/target/ink/read_relay_blocknumber.wasm",
)
.unwrap();

Expand Down Expand Up @@ -620,7 +618,7 @@ mod tests {
let _ = env_logger::try_init();

let (wasm_binary, _) = load_wasm_module::<Runtime>(
"../../pop-api/examples/place-spot-order/target/ink/pop_api_spot_order_example.wasm",
"../../pop-api/examples/place-spot-order/target/ink/spot_order.wasm",
)
.unwrap();

Expand Down

0 comments on commit 47b563e

Please sign in to comment.