Skip to content
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

Feature/e2e tests #53

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ members = [
"crates/multiasset",
"crates/nesting",
"crates/rmrk",
"crates/tests",
"examples/*",
]
]

[patch.crates-io]
ink = { git = "https://github.com/paritytech/ink" }
ink_e2e = { git = "https://github.com/paritytech/ink" }
34 changes: 34 additions & 0 deletions crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "rmrk_tests"
version = "0.5.0"
authors = ["Stake Technologies <[email protected]>"]
edition = "2021"

[dependencies]
ink = { version = "4.0.0", default-features = false }
ink_e2e = { version = "4.0.0" }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
openbrush = { tag = "3.0.0", git = "https://github.com/727-Ventures/openbrush-contracts", default-features = false, features = ["access_control", "reentrancy_guard", "psp34"] }
rmrk = { path = "../rmrk", default-features = false }
rmrk_example_equippable = { path = "../../examples/equippable", default-features = false, features = ["ink-as-dependency"]}


[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info",
"scale-info/std",
"openbrush/std",
"rmrk/std",
"rmrk_example_equippable/std",
]
e2e-tests = []


16 changes: 16 additions & 0 deletions crates/tests/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub mod psp34;
pub mod rmrk;

use ink::primitives::AccountId;

pub fn bob() -> AccountId {
ink_e2e::account_id(ink_e2e::AccountKeyring::Bob)
}

pub fn alice() -> AccountId {
ink_e2e::account_id(ink_e2e::AccountKeyring::Alice)
}

pub fn eve() -> AccountId {
ink_e2e::account_id(ink_e2e::AccountKeyring::Eve)
}
64 changes: 64 additions & 0 deletions crates/tests/src/helpers/psp34.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use rmrk_example_equippable::rmrk_example_equippable::RmrkRef;

use ink::{
env::DefaultEnvironment,
primitives::AccountId,
};
use ink_e2e::{
build_message,
AccountKeyring,
Client,
PairSigner,
};

use openbrush::contracts::psp34::{
extensions::enumerable::*,
psp34_external::PSP34,
};

pub async fn call_transfer(
client: &mut Client<ink_e2e::PolkadotConfig, DefaultEnvironment>,
contract: AccountId,
caller: AccountKeyring,
to: AccountId,
id: Id,
) {
let signer = PairSigner::new(caller.pair());
let approve = build_message::<RmrkRef>(contract.clone())
.call(|c| PSP34::transfer(c, to, id.clone(), Default::default()));
let _ = client
.call(&signer, approve, 0, None)
.await
.expect("Call transfer failed");
}

pub async fn call_approve(
client: &mut Client<ink_e2e::PolkadotConfig, DefaultEnvironment>,
contract: AccountId,
caller: AccountKeyring,
operator: AccountId,
id: Id,
) {
let signer = PairSigner::new(caller.pair());
let approve = build_message::<RmrkRef>(contract.clone())
.call(|c| PSP34::approve(c, operator, Some(id.clone()), true));
let _ = client
.call(&signer, approve, 0, None)
.await
.expect("Call approve failed");
}

pub async fn query_owner_of(
client: &mut Client<ink_e2e::PolkadotConfig, DefaultEnvironment>,
contract: AccountId,
caller: AccountKeyring,
id: Id,
) -> AccountId {
let signer = PairSigner::new(caller.pair());
let get = build_message::<RmrkRef>(contract.clone()).call(|c| PSP34::owner_of(c, id.clone()));
client
.call_dry_run(&signer, &get, 0, None)
.await
.return_value()
.expect("Query owner of failedf")
}
66 changes: 66 additions & 0 deletions crates/tests/src/helpers/rmrk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use ink::{
env::DefaultEnvironment,
primitives::AccountId,
};
use ink_e2e::{
build_message,
log_info,
Client,
PairSigner,
};
use rmrk::traits::{
minting_external,
minting_external::Minting,
};
use rmrk_example_equippable::rmrk_example_equippable::*;

use ink_e2e::AccountKeyring;
use openbrush::{
contracts::psp34::{
extensions::enumerable::*,
psp34_external::PSP34,
},
traits::String,
};

pub async fn init_contract(
client: &mut Client<ink_e2e::PolkadotConfig, DefaultEnvironment>,
caller: AccountKeyring,
name: &str,
symbol: &str,
base_uri: &str,
max_supply: u64,
collection_uri: &str,
) -> AccountId {
let signer = PairSigner::new(caller.pair());
let contract_constructor = RmrkRef::new(
String::from(name),
String::from(symbol),
String::from(base_uri),
max_supply,
String::from(collection_uri),
);
client
.instantiate(
"rmrk_example_equippable",
&signer,
contract_constructor,
0,
None,
)
.await
.expect("instantiate failed")
.account_id
}

pub async fn call_mint(
client: &mut Client<ink_e2e::PolkadotConfig, DefaultEnvironment>,
contract: AccountId,
caller: AccountKeyring,
to: AccountKeyring,
) {
let signer = PairSigner::new(caller.pair());
let to = ink_e2e::account_id(to);
let call = build_message::<RmrkRef>(contract).call(|c| c.mint(to.clone()));
client.call(&signer, call, 0, None).await.expect("Call mint failed");
}
8 changes: 8 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[cfg(feature = "e2e-tests")]
mod helpers;

#[cfg(feature = "e2e-tests")]
mod minting;
44 changes: 44 additions & 0 deletions crates/tests/src/minting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::helpers::{
alice,
bob,
eve,
psp34::{
call_approve,
call_transfer,
query_owner_of,
},
rmrk::{
call_mint,
init_contract,
},
};

use openbrush::contracts::psp34::extensions::enumerable::*;

use ink_e2e::AccountKeyring::{
Alice,
Bob,
Eve,
};

type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn can_mint(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let contract = init_contract(&mut client, Alice, "", "", "", 1000, "").await;
call_mint(&mut client, contract, Alice, Bob).await;
let owner = query_owner_of(&mut client, contract, Alice, Id::U64(1)).await;
assert!(owner == bob());
Ok(())
}

#[ink_e2e::test]
async fn can_approve_transfer(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let contract = init_contract(&mut client, Alice, "", "", "", 1000, "").await;
call_mint(&mut client, contract, Alice, Bob).await;
call_approve(&mut client, contract, Bob, eve(), Id::U64(1)).await;
call_transfer(&mut client, contract, Eve, alice(), Id::U64(1)).await;
let owner = query_owner_of(&mut client, contract, Alice, Id::U64(1)).await;
assert!(owner == alice());
Ok(())
}
1 change: 1 addition & 0 deletions examples/equippable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rmrk = { path = "../../crates/rmrk", default-features = false }
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"rlib",
"cdylib",
]

Expand Down