Skip to content

Commit

Permalink
interactor multi-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 16, 2023
1 parent e463bb9 commit b022545
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
77 changes: 66 additions & 11 deletions interact/src/adder_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ mod adder_interact_cli;
mod adder_interact_config;
mod adder_interact_state;

use workshop::ProxyTrait;
use adder_interact_config::Config;
use adder_interact_state::State;
use clap::Parser;
use multiversx_sc_snippets::{
env_logger,
multiversx_sc::{storage::mappers::SingleValue, types::Address},
multiversx_sc::{
storage::mappers::SingleValue,
types::{Address, ContractDeploy},
},
multiversx_sc_scenario::{
api::StaticApi,
bech32,
Expand All @@ -23,6 +25,7 @@ use multiversx_sc_snippets::{
},
tokio, Interactor, StepBuffer,
};
use workshop::ProxyTrait;

const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";

Expand All @@ -36,20 +39,26 @@ async fn main() {
match &cli.command {
Some(adder_interact_cli::InteractCliCommand::Add(args)) => {
adder_interact.add(args.value).await;
},
}
Some(adder_interact_cli::InteractCliCommand::Deploy) => {
adder_interact.deploy().await;
},
}
Some(adder_interact_cli::InteractCliCommand::DeployEV) => {
adder_interact.deploy_ev().await;
}
Some(adder_interact_cli::InteractCliCommand::Feed) => {
adder_interact.feed_contract_egld().await;
},
}
Some(adder_interact_cli::InteractCliCommand::MultiDeploy(args)) => {
adder_interact.multi_deploy(&args.count).await;
},
}
Some(adder_interact_cli::InteractCliCommand::Sum) => {
adder_interact.print_sum().await;
},
None => {},
}
Some(adder_interact_cli::InteractCliCommand::SumSquared) => {
adder_interact.print_sum_squared().await;
}
None => {}
}
}

Expand All @@ -58,6 +67,7 @@ struct AdderInteract {
interactor: Interactor,
wallet_address: Address,
adder_code: BytesValue,
adder_ev_code: BytesValue,
state: State,
}

Expand All @@ -68,14 +78,21 @@ impl AdderInteract {
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;
let wallet_address = interactor.register_wallet(test_wallets::mike());
let adder_code =
BytesValue::interpret_from("file:../output/workshop.wasm", &InterpreterContext::default());
let wallet_address = interactor.register_wallet(test_wallets::grace());
let adder_code = BytesValue::interpret_from(
"file:../output/workshop.wasm",
&InterpreterContext::default(),
);
let adder_ev_code = BytesValue::interpret_from(
"file:../output/workshop-ev.wasm",
&InterpreterContext::default(),
);

Self {
interactor,
wallet_address,
adder_code,
adder_ev_code,
state: State::load_state(),
}
}
Expand Down Expand Up @@ -122,6 +139,32 @@ impl AdderInteract {
.await;
}

async fn deploy_ev(&mut self) {
self.interactor
.sc_deploy_use_result(
ScDeployStep::new()
.argument(self.state.adder_address.as_ref().unwrap().as_str())
.call(ContractDeploy::<StaticApi, ()>::new())
.from(&self.wallet_address)
.code(&self.adder_ev_code),
|new_address, tr| {
tr.result.unwrap_or_else(|err| {
panic!(
"deploy failed: status: {}, message: {}",
err.status, err.message
)
});

let new_address_bech32 = bech32::encode(&new_address);
println!("new address: {new_address_bech32}");

let new_address_expr = format!("bech32:{new_address_bech32}");
self.state.set_adder_ev_address(&new_address_expr);
},
)
.await;
}

async fn multi_deploy(&mut self, count: &u8) {
if *count == 0 {
println!("count must be greater than 0");
Expand Down Expand Up @@ -196,4 +239,16 @@ impl AdderInteract {
})
.await;
}

async fn print_sum_squared(&mut self) {
self.interactor
.sc_query_use_result(
ScQueryStep::new().call(self.state.adder_ev().sum_squared()),
|tr| {
let sum: BigUint = tr.result.unwrap();
println!("sum squared: {}", sum);
},
)
.await;
}
}
4 changes: 4 additions & 0 deletions interact/src/adder_interact_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ pub enum InteractCliCommand {
Add(AddArgs),
#[command(name = "deploy", about = "Deploy contract")]
Deploy,
#[command(name = "deploy-ev", about = "Deploy external view contract")]
DeployEV,
#[command(name = "feed", about = "Feed contract EGLD")]
Feed,
#[command(name = "multi-deploy", about = "Multiple deploy contracts")]
MultiDeploy(MultiDeployArgs),
#[command(name = "sum", about = "Print sum")]
Sum,
#[command(name = "sum-sq", about = "Print sum squared")]
SumSquared,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
Expand Down
16 changes: 15 additions & 1 deletion interact/src/adder_interact_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub type AdderContract = ContractInfo<workshop::Proxy<StaticApi>>;
/// Multisig Interact state
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct State {
adder_address: Option<String>,
pub adder_address: Option<String>,
pub adder_ev_address: Option<String>,
}

impl State {
Expand All @@ -38,6 +39,11 @@ impl State {
self.adder_address = Some(String::from(address));
}

/// Sets the adder-ev address
pub fn set_adder_ev_address(&mut self, address: &str) {
self.adder_ev_address = Some(String::from(address));
}

/// Returns the adder contract
pub fn adder(&self) -> AdderContract {
AdderContract::new(
Expand All @@ -47,6 +53,14 @@ impl State {
)
}

pub fn adder_ev(&self) -> AdderContract {
AdderContract::new(
self.adder_ev_address
.clone()
.expect("no known adder contract, deploy first"),
)
}

/// Returns the adder contract with default address
pub fn default_adder(&self) -> AdderContract {
AdderContract::new(DEFAULT_ADDER_ADDRESS)
Expand Down

0 comments on commit b022545

Please sign in to comment.