Skip to content

Commit

Permalink
fix: add more tests and todos
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Sep 10, 2024
1 parent 6fbe95b commit 8638b78
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pop-sandbox/examples/flipper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "flipper"
name = "api_example_flipper"
edition = "2021"
version = "0.1.0"
authors = ["R0GUE <go@r0gue.io>"]
Expand Down
2 changes: 1 addition & 1 deletion pop-sandbox/examples/fungibles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "api_example"
name = "api_example_fungibles"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
Expand Down
75 changes: 18 additions & 57 deletions pop-sandbox/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[ink::contract]
mod create_token_in_constructor {
use pop_api::{
primitives::AssetId,
v0::assets::fungibles::{self as api},
StatusCode,
};
Expand All @@ -11,86 +12,46 @@ mod create_token_in_constructor {

#[ink(storage)]
pub struct Fungible {
id: u32,
id: AssetId,
}

impl Fungible {
// #[ink(constructor, payable)]
// pub fn new() -> Result<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)?;
// Ok(contract)
// }

#[ink(constructor)]
pub fn new() -> Self {
let contract = Self { id: 0 };
pub fn new(id: AssetId, min_balance: Balance) -> Result<Self> {
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<bool> {
// 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<Self> {
// 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<bool> {
// 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 {}

#[drink::test(sandbox = pop_sandbox::PopSandbox)]
fn deploy_and_call_a_contract(mut session: Session) -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}
38 changes: 38 additions & 0 deletions pop-sandbox/examples/proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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",
]
76 changes: 76 additions & 0 deletions pop-sandbox/examples/proxy/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u8>) -> Result<Vec<u8>, StatusCode> {
ink::env::debug_println!("Proxy::call() func_id={func_id}, input={input:?}");
ChainExtensionMethod::build(func_id)
.input::<Vec<u8>>()
.output::<Result<Vec<u8>, StatusCode>, true>()
.handle_error_code::<StatusCode>()
.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<ink::scale::Error> 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<dyn std::error::Error>> {
let contract_bundle = BundleProvider::local()?;
let contract_address =
session.deploy_bundle(contract_bundle, "new", NO_ARGS, NO_SALT, None)?;

let input : Vec<u8> = vec![0, 7, 112, 111, 112].encode();
let converted_input : Vec<String> = input.into_iter().map(|b| b.to_string()).collect::<Vec<String>>();
// DispatchCall::RuntimeSystem::Remark
session.call_with_address(contract_address, "call", &[vec![0u32.to_string()], converted_input].concat(), None)??;

Ok(())
}
}

0 comments on commit 8638b78

Please sign in to comment.