Writing e2e tests with ink-wrapper
Last updated
Was this helpful?
Was this helpful?
ink-wrapper -m test-project/psp22_contract/target/ink/psp22_contract.json \
| rustfmt --edition 2021 > ../psp22-tests/src/psp22_contract.rs# ...
[dependencies]
ink-wrapper-types = "0.4.0"
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
ink_primitives = "4.0.1"
aleph_client = "3.0.0"
async-trait = "0.1.68"
# These are a couple dependencies we will use to write our tests
tokio = { version = "1.25.0", features = ["macros"] }
rand = "0.8.5"
anyhow = "1.0.71"[toolchain]
channel = "nightly-2023-04-20"
components = ["rustfmt", "rust-src", "clippy"]mod psp22_contract;#[cfg(test)]
mod tests {
use crate::psp22_contract;
// The PSP22-specific methods of the contract are hidden behind a trait.
// This will only happen for contract methods with names like "PSP22::transfer".
// Other contract methods will just be available on the contract instance without
// any extra trait.
use crate::psp22_contract::PSP22 as _;
use aleph_client::keypair_from_string;
use aleph_client::Connection;
use aleph_client::SignedConnection;
use anyhow::Result;
// This is just a convenience helper for converting any AsRef<[u8; 32]> to
// ink_primitives::AccountId - the datatype used by the generated code to
// represent account ids.
use ink_wrapper_types::util::ToAccountId as _;
use rand::RngCore as _;
#[tokio::test]
async fn it_works() -> Result<()> {
// Connect to the node launched earlier.
let conn = Connection::new("ws://localhost:9944").await;
let conn = SignedConnection::from_connection(conn, keypair_from_string("//Alice"));
let bob = keypair_from_string("//Bob");
// We're using a random salt here so that each test run is independent.
let mut salt = vec![0; 32];
rand::thread_rng().fill_bytes(&mut salt);
let total_supply = 1000;
// Constructors take a connection, the salt, and any arguments
// the actual constructor requires afterwards.
let contract = psp22_contract::Instance::new(&conn, salt, total_supply).await?;
// A mutating method takes a signed connection and any arguments afterwards.
contract
.transfer(&conn, bob.account_id().to_account_id(), 100, vec![])
.await?;
// A reader method takes a connection (may be unsigned) and any arguments afterwards.
let balance = contract
.balance_of(&conn, bob.account_id().to_account_id())
.await??;
assert_eq!(balance, 100);
Ok(())
}
}