From a35096d284e7c4d2ba823050153dcacd66e99fa8 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 14 Oct 2024 23:25:41 +0300 Subject: [PATCH] chain sim - adder interactor integration test --- contracts/examples/adder/interact/Cargo.toml | 5 +- .../adder/interact/src/basic_interact.rs | 76 ++++++------------- .../interact/src/basic_interact_config.rs | 11 ++- .../adder/interact/src/basic_interact_lib.rs | 7 ++ .../adder/interact/src/basic_interact_main.rs | 6 ++ .../interact/tests/basic_interact_cs_test.rs | 30 ++++++++ 6 files changed, 80 insertions(+), 55 deletions(-) create mode 100644 contracts/examples/adder/interact/src/basic_interact_lib.rs create mode 100644 contracts/examples/adder/interact/src/basic_interact_main.rs create mode 100644 contracts/examples/adder/interact/tests/basic_interact_cs_test.rs diff --git a/contracts/examples/adder/interact/Cargo.toml b/contracts/examples/adder/interact/Cargo.toml index bba6300e03..8005539f97 100644 --- a/contracts/examples/adder/interact/Cargo.toml +++ b/contracts/examples/adder/interact/Cargo.toml @@ -7,7 +7,10 @@ publish = false [[bin]] name = "basic-interact" -path = "src/basic_interact.rs" +path = "src/basic_interact_main.rs" + +[lib] +path = "src/basic_interact_lib.rs" [dependencies] clap = { version = "4.4.7", features = ["derive"] } diff --git a/contracts/examples/adder/interact/src/basic_interact.rs b/contracts/examples/adder/interact/src/basic_interact.rs index f83ca84a26..668c8b7d77 100644 --- a/contracts/examples/adder/interact/src/basic_interact.rs +++ b/contracts/examples/adder/interact/src/basic_interact.rs @@ -1,12 +1,13 @@ -mod basic_interact_cli; -mod basic_interact_config; -mod basic_interact_state; +// mod basic_interact_cli; +// mod basic_interact_config; +// mod basic_interact_state; use core::str; +use crate::basic_interact_cli; +use crate::basic_interact_config::Config; +use crate::basic_interact_state::State; use adder::adder_proxy; -use basic_interact_config::Config; -use basic_interact_state::State; use clap::Parser; use multiversx_sc_snippets::imports::*; @@ -15,11 +16,12 @@ const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json"; const ADDER_CODE_PATH: MxscPath = MxscPath::new("../output/adder.mxsc.json"); -#[tokio::main] -async fn main() { +pub async fn adder_cli() { env_logger::init(); - let mut basic_interact = AdderInteract::init().await; + let config = Config::load_config(); + + let mut basic_interact = AdderInteract::init(config).await; let cli = basic_interact_cli::InteractCli::parse(); match &cli.command { @@ -53,16 +55,15 @@ async fn main() { } #[allow(unused)] -struct AdderInteract { - interactor: Interactor, - adder_owner_address: Bech32Address, - wallet_address: Bech32Address, - state: State, +pub struct AdderInteract { + pub interactor: Interactor, + pub adder_owner_address: Bech32Address, + pub wallet_address: Bech32Address, + pub state: State, } impl AdderInteract { - async fn init() -> Self { - let config = Config::load_config(); + pub async fn init(config: Config) -> Self { let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) .await .with_tracer(INTERACTOR_SCENARIO_TRACE_PATH) @@ -94,7 +95,7 @@ impl AdderInteract { } } - async fn set_state(&mut self) { + pub async fn set_state(&mut self) { println!("wallet address: {}", self.wallet_address); self.interactor .retrieve_account(&self.adder_owner_address) @@ -102,7 +103,7 @@ impl AdderInteract { self.interactor.retrieve_account(&self.wallet_address).await; } - async fn deploy(&mut self) { + pub async fn deploy(&mut self) { // warning: multi deploy not yet fully supported // only works with last deployed address @@ -126,7 +127,7 @@ impl AdderInteract { self.state.set_adder_address(new_address); } - async fn multi_deploy(&mut self, count: usize) { + pub async fn multi_deploy(&mut self, count: usize) { if count == 0 { println!("count must be greater than 0"); return; @@ -159,7 +160,7 @@ impl AdderInteract { } } - async fn multi_add(&mut self, value: u32, count: usize) { + pub async fn multi_add(&mut self, value: u32, count: usize) { self.set_state().await; println!("calling contract {count} times..."); @@ -179,7 +180,7 @@ impl AdderInteract { println!("successfully performed add {count} times"); } - async fn feed_contract_egld(&mut self) { + pub async fn feed_contract_egld(&mut self) { self.interactor .tx() .from(&self.wallet_address) @@ -190,7 +191,7 @@ impl AdderInteract { .await; } - async fn add(&mut self, value: u32) { + pub async fn add(&mut self, value: u32) { self.interactor .tx() .from(&self.wallet_address) @@ -205,7 +206,7 @@ impl AdderInteract { println!("successfully performed add"); } - async fn print_sum(&mut self) { + pub async fn print_sum(&mut self) { let sum = self .interactor .query() @@ -220,7 +221,7 @@ impl AdderInteract { println!("sum: {sum}"); } - async fn upgrade( + pub async fn upgrade( &mut self, new_value: u32, sender: &Bech32Address, @@ -275,32 +276,3 @@ impl AdderInteract { } } } - -#[cfg(feature = "chain_simulator")] -#[tokio::test] -async fn simulator_upgrade_test() { - let mut basic_interact = AdderInteract::init().await; - // let wallet_address = basic_interact.wallet_address.clone(); - let adder_owner_address = basic_interact.adder_owner_address.clone(); - // let error_not_owner = (4, "upgrade is allowed only for owner"); - - basic_interact.deploy().await; - basic_interact.add(1u32).await; - - // Sum will be 1 - basic_interact.print_sum().await; - - basic_interact - .upgrade(7u32, &adder_owner_address, None) - .await; - - // Sum will be the updated value of 7 - basic_interact.print_sum().await; - - // basic_interact - // .upgrade(10u32, &wallet_address, Some(error_not_owner)) - // .await; - - // // Sum will remain 7 - // basic_interact.print_sum().await; -} diff --git a/contracts/examples/adder/interact/src/basic_interact_config.rs b/contracts/examples/adder/interact/src/basic_interact_config.rs index edd4ba6b1e..bd19da629d 100644 --- a/contracts/examples/adder/interact/src/basic_interact_config.rs +++ b/contracts/examples/adder/interact/src/basic_interact_config.rs @@ -14,8 +14,8 @@ pub enum ChainType { /// Adder Interact configuration #[derive(Debug, Deserialize)] pub struct Config { - gateway_uri: String, - chain_type: ChainType, + pub gateway_uri: String, + pub chain_type: ChainType, } impl Config { @@ -27,6 +27,13 @@ impl Config { toml::from_str(&content).unwrap() } + pub fn chain_simulator_config() -> Self { + Config { + gateway_uri: "http://localhost:8085".to_owned(), + chain_type: ChainType::Simulator, + } + } + // Returns the gateway URI pub fn gateway_uri(&self) -> &str { &self.gateway_uri diff --git a/contracts/examples/adder/interact/src/basic_interact_lib.rs b/contracts/examples/adder/interact/src/basic_interact_lib.rs new file mode 100644 index 0000000000..937d2ccaf2 --- /dev/null +++ b/contracts/examples/adder/interact/src/basic_interact_lib.rs @@ -0,0 +1,7 @@ +pub mod basic_interact_cli; +pub mod basic_interact_config; +pub mod basic_interact_state; +pub mod basic_interact; + +pub use basic_interact::{AdderInteract, adder_cli}; +pub use basic_interact_config::Config; \ No newline at end of file diff --git a/contracts/examples/adder/interact/src/basic_interact_main.rs b/contracts/examples/adder/interact/src/basic_interact_main.rs new file mode 100644 index 0000000000..ee111351fe --- /dev/null +++ b/contracts/examples/adder/interact/src/basic_interact_main.rs @@ -0,0 +1,6 @@ +extern crate basic_interact; + +#[tokio::main] +pub async fn main() { + basic_interact::adder_cli().await; +} \ No newline at end of file diff --git a/contracts/examples/adder/interact/tests/basic_interact_cs_test.rs b/contracts/examples/adder/interact/tests/basic_interact_cs_test.rs new file mode 100644 index 0000000000..4fce1acb7e --- /dev/null +++ b/contracts/examples/adder/interact/tests/basic_interact_cs_test.rs @@ -0,0 +1,30 @@ +use basic_interact::{AdderInteract, Config}; + +#[tokio::test] +#[cfg_attr(not(feature = "chain_simulator"), ignore)] +async fn simulator_upgrade_test() { + let mut basic_interact = AdderInteract::init(Config::chain_simulator_config()).await; + // let wallet_address = basic_interact.wallet_address.clone(); + let adder_owner_address = basic_interact.adder_owner_address.clone(); + // let error_not_owner = (4, "upgrade is allowed only for owner"); + + basic_interact.deploy().await; + basic_interact.add(1u32).await; + + // Sum will be 1 + basic_interact.print_sum().await; + + basic_interact + .upgrade(7u32, &adder_owner_address, None) + .await; + + // Sum will be the updated value of 7 + basic_interact.print_sum().await; + + // basic_interact + // .upgrade(10u32, &wallet_address, Some(error_not_owner)) + // .await; + + // // Sum will remain 7 + // basic_interact.print_sum().await; +}