From 8638b7874cd09ba9bdb74933e43aa9b7d557ac97 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Tue, 10 Sep 2024 23:21:52 +0700 Subject: [PATCH] fix: add more tests and todos --- pop-sandbox/examples/flipper/Cargo.toml | 2 +- pop-sandbox/examples/fungibles/Cargo.toml | 2 +- pop-sandbox/examples/fungibles/lib.rs | 75 ++++++---------------- pop-sandbox/examples/proxy/Cargo.toml | 38 ++++++++++++ pop-sandbox/examples/proxy/lib.rs | 76 +++++++++++++++++++++++ 5 files changed, 134 insertions(+), 59 deletions(-) create mode 100755 pop-sandbox/examples/proxy/Cargo.toml create mode 100755 pop-sandbox/examples/proxy/lib.rs diff --git a/pop-sandbox/examples/flipper/Cargo.toml b/pop-sandbox/examples/flipper/Cargo.toml index dfac5fcc..3ce7c90d 100755 --- a/pop-sandbox/examples/flipper/Cargo.toml +++ b/pop-sandbox/examples/flipper/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "flipper" +name = "api_example_flipper" edition = "2021" version = "0.1.0" authors = ["R0GUE "] diff --git a/pop-sandbox/examples/fungibles/Cargo.toml b/pop-sandbox/examples/fungibles/Cargo.toml index dc4d7a91..326a1755 100755 --- a/pop-sandbox/examples/fungibles/Cargo.toml +++ b/pop-sandbox/examples/fungibles/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "api_example" +name = "api_example_fungibles" version = "0.1.0" authors = ["[your_name] <[your_email]>"] edition = "2021" diff --git a/pop-sandbox/examples/fungibles/lib.rs b/pop-sandbox/examples/fungibles/lib.rs index 8afc88c1..59d0ea7e 100755 --- a/pop-sandbox/examples/fungibles/lib.rs +++ b/pop-sandbox/examples/fungibles/lib.rs @@ -3,6 +3,7 @@ #[ink::contract] mod create_token_in_constructor { use pop_api::{ + primitives::AssetId, v0::assets::fungibles::{self as api}, StatusCode, }; @@ -11,77 +12,32 @@ mod create_token_in_constructor { #[ink(storage)] pub struct Fungible { - id: u32, + id: AssetId, } impl Fungible { - // #[ink(constructor, payable)] - // pub fn new() -> Result { - // let id = 0; - // let min_balance = 1; - // let contract = Self { id }; - // // AccountId of the contract which will be set to the owner of the fungible token. - // let owner = contract.env().account_id(); - // api::create(id, owner, min_balance)?; - // Ok(contract) - // } - #[ink(constructor)] - pub fn new() -> Self { - let contract = Self { id: 0 }; + pub fn new(id: AssetId, min_balance: Balance) -> Result { + ink::env::debug_println!("Fungible::call() asset_id={id}, min_balance={min_balance}"); + let contract = Self { id }; + // AccountId of the contract which will be set to the owner of the fungible token. let owner = contract.env().account_id(); - contract + // TODO: Calling POP API caused DeploymentReverted + api::create(id, owner, min_balance)?; + Ok(contract) } - // #[ink(constructor)] - // pub fn new() -> Self { - // let id = 0; - // let min_balance = 1; - // let contract = Self { id }; - // // AccountId of the contract which will be set to the owner of the fungible token. - // let owner = contract.env().account_id(); - // api::create(id, owner, min_balance).unwrap(); - // contract - // } - #[ink(message)] pub fn asset_exists(&self) -> Result { - // api::asset_exists(self.id) - Ok(true) + api::asset_exists(self.id) } } } -// #[ink::contract] -// mod create_token_in_constructor { -// use super::*; - -// #[ink(storage)] -// pub struct Fungible { -// id: AssetId, -// } - -// impl Fungible { -// #[ink(constructor)] -// pub fn new(id: AssetId, min_balance: Balance) -> Result { -// let contract = Self { id }; -// // AccountId of the contract which will be set to the owner of the fungible token. -// let owner = contract.env().account_id(); -// api::create(id, owner, min_balance)?; -// Ok(contract) -// } - -// #[ink(message)] -// pub fn asset_exists(&self) -> Result { -// api::asset_exists(self.id) -// } -// } -// } - /// We put `drink`-based tests as usual unit tests, into a test module. #[cfg(test)] mod tests { - use drink::session::{Session, NO_SALT, NO_ARGS}; + use drink::session::{Session, NO_ARGS, NO_SALT}; #[drink::contract_bundle_provider] enum BundleProvider {} @@ -89,8 +45,13 @@ mod tests { #[drink::test(sandbox = pop_sandbox::PopSandbox)] fn deploy_and_call_a_contract(mut session: Session) -> Result<(), Box> { let contract_bundle = BundleProvider::local()?; - let _contract_address = - session.deploy_bundle(contract_bundle, "new", NO_ARGS, NO_SALT, None)?; + let _contract_address = session.deploy_bundle( + contract_bundle, + "new", + &[1.to_string(), 1_000.to_string()], + NO_SALT, + None, + )?; Ok(()) } } diff --git a/pop-sandbox/examples/proxy/Cargo.toml b/pop-sandbox/examples/proxy/Cargo.toml new file mode 100755 index 00000000..918de444 --- /dev/null +++ b/pop-sandbox/examples/proxy/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "api_example_proxy" +version = "0.1.0" +authors = ["[your_name] <[your_email]>"] +edition = "2021" + +[dependencies] +ink = { version = "=5.0.0", default-features = false, features = ["ink-debug"] } +pop-api = { path = "../../../pop-api", default-features = false, features = [ + "fungibles", +] } +codec = { package = "parity-scale-codec", version = "3", default-features = false, features = [ + "derive", +] } +scale-info = { version = "2.6", default-features = false, features = [ + "derive", +], optional = true } +frame-system = { version = "29.0.0", default-features = false } + +[dev-dependencies] +drink = { path = "../../../../pop-drink/drink" } +pop-sandbox = { path = "../../../pop-sandbox", default-features = false } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +e2e-tests = [] +ink-as-dependency = [] +std = [ + "ink/std", + "pop-api/std", + "pop-sandbox/std", + "frame-system/std", + "codec/std", + "scale-info/std", +] diff --git a/pop-sandbox/examples/proxy/lib.rs b/pop-sandbox/examples/proxy/lib.rs new file mode 100755 index 00000000..b0116301 --- /dev/null +++ b/pop-sandbox/examples/proxy/lib.rs @@ -0,0 +1,76 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +use ink::{ + env::chain_extension::{ChainExtensionMethod, FromStatusCode}, + prelude::vec::Vec, +}; + +#[ink::contract] +mod proxy_contract { + use super::*; + + // Simple contract for proxying a call to a chain extension. + #[ink(storage)] + #[derive(Default)] + pub struct Proxy; + + impl Proxy { + #[ink(constructor)] + pub fn new() -> Self { + ink::env::debug_println!("Proxy::new()"); + Default::default() + } + + #[ink(message)] + pub fn call(&self, func_id: u32, input: Vec) -> Result, StatusCode> { + ink::env::debug_println!("Proxy::call() func_id={func_id}, input={input:?}"); + ChainExtensionMethod::build(func_id) + .input::>() + .output::, StatusCode>, true>() + .handle_error_code::() + .call(&input) + } + } + + #[ink::scale_derive(Encode, Decode, TypeInfo)] + pub struct StatusCode(u32); + impl FromStatusCode for StatusCode { + fn from_status_code(status_code: u32) -> Result<(), Self> { + match status_code { + 0 => Ok(()), + _ => Err(StatusCode(status_code)), + } + } + } + + impl From for StatusCode { + fn from(_: ink::scale::Error) -> Self { + StatusCode(u32::MAX) + } + } +} + +/// We put `drink`-based tests as usual unit tests, into a test module. +#[cfg(test)] +mod tests { + use codec::Encode; + use core::fmt::Debug; + use drink::session::{Session, NO_ARGS, NO_SALT}; + + #[drink::contract_bundle_provider] + enum BundleProvider {} + + #[drink::test(sandbox = pop_sandbox::PopSandbox)] + fn deploy_contract_and_call(mut session: Session) -> Result<(), Box> { + let contract_bundle = BundleProvider::local()?; + let contract_address = + session.deploy_bundle(contract_bundle, "new", NO_ARGS, NO_SALT, None)?; + + let input : Vec = vec![0, 7, 112, 111, 112].encode(); + let converted_input : Vec = input.into_iter().map(|b| b.to_string()).collect::>(); + // DispatchCall::RuntimeSystem::Remark + session.call_with_address(contract_address, "call", &[vec![0u32.to_string()], converted_input].concat(), None)??; + + Ok(()) + } +}