Skip to content

Commit

Permalink
chain sim - adder interactor integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 14, 2024
1 parent dc2f0f8 commit a35096d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 55 deletions.
5 changes: 4 additions & 1 deletion contracts/examples/adder/interact/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
76 changes: 24 additions & 52 deletions contracts/examples/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -94,15 +95,15 @@ 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)
.await;
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

Expand All @@ -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;
Expand Down Expand Up @@ -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...");

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -220,7 +221,7 @@ impl AdderInteract {
println!("sum: {sum}");
}

async fn upgrade(
pub async fn upgrade(
&mut self,
new_value: u32,
sender: &Bech32Address,
Expand Down Expand Up @@ -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;
}
11 changes: 9 additions & 2 deletions contracts/examples/adder/interact/src/basic_interact_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions contracts/examples/adder/interact/src/basic_interact_lib.rs
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions contracts/examples/adder/interact/src/basic_interact_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern crate basic_interact;

#[tokio::main]
pub async fn main() {
basic_interact::adder_cli().await;
}
30 changes: 30 additions & 0 deletions contracts/examples/adder/interact/tests/basic_interact_cs_test.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a35096d

Please sign in to comment.