Skip to content

Commit

Permalink
Merge pull request #1710 from multiversx/wallet-import
Browse files Browse the repository at this point in the history
Interactor minor fixes & adder interactor improvmements
  • Loading branch information
andrei-marinica authored Jul 18, 2024
2 parents 8d2e7ca + 1d60807 commit 225e90c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions contracts/examples/adder/interact/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Pem files are used for interactions, but shouldn't be committed
*.pem
!adder-owner.pem

# Temporary storage of deployed contract address, so we can preserve the context between executions.
state.toml
Expand Down
5 changes: 5 additions & 0 deletions contracts/examples/adder/interact/adder-owner.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY for erd1s0a83vp2wmsvgphplkn8amkn8tnfd777zlut3e2lazgdzdu6z46qpgx3vz-----
MWIyYTQ3ZmIzMzcxODUxZTE3Njk5N2M0NTNmZDY1ODFkM2U5NjlhYmM1ODMzMTI0
MjNkYzAyNGI3YzZlNmVlNjgzZmE3OGIwMmE3NmUwYzQwNmUxZmRhNjdlZWVkMzNh
ZTY5NmZiZGUxN2Y4YjhlNTVmZTg5MGQxMzc5YTE1NzQ=
-----END PRIVATE KEY for erd1s0a83vp2wmsvgphplkn8amkn8tnfd777zlut3e2lazgdzdu6z46qpgx3vz-----
52 changes: 42 additions & 10 deletions contracts/examples/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ async fn main() {
let cli = basic_interact_cli::InteractCli::parse();
match &cli.command {
Some(basic_interact_cli::InteractCliCommand::Add(args)) => {
basic_interact.add(args.value).await;
if args.count == 1 {
basic_interact.add(args.value).await;
} else {
basic_interact.multi_add(args.value, args.count).await;
}
},
Some(basic_interact_cli::InteractCliCommand::Deploy) => {
basic_interact.deploy().await;
Expand All @@ -31,7 +35,7 @@ async fn main() {
basic_interact.feed_contract_egld().await;
},
Some(basic_interact_cli::InteractCliCommand::MultiDeploy(args)) => {
basic_interact.multi_deploy(&args.count).await;
basic_interact.multi_deploy(args.count).await;
},
Some(basic_interact_cli::InteractCliCommand::Sum) => {
basic_interact.print_sum().await;
Expand All @@ -46,6 +50,7 @@ async fn main() {
#[allow(unused)]
struct AdderInteract {
interactor: Interactor,
adder_owner_address: Bech32Address,
wallet_address: Bech32Address,
state: State,
}
Expand All @@ -57,17 +62,24 @@ impl AdderInteract {
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;

let adder_owner_address =
interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap());
let wallet_address = interactor.register_wallet(test_wallets::mike());

Self {
interactor,
adder_owner_address: adder_owner_address.into(),
wallet_address: wallet_address.into(),
state: State::load_state(),
}
}

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;
}

Expand All @@ -80,8 +92,8 @@ impl AdderInteract {
let new_address = self
.interactor
.tx()
.from(&self.wallet_address)
.gas(NumExpr("30,000,000"))
.from(&self.adder_owner_address)
.gas(3_000_000)
.typed(adder_proxy::AdderProxy)
.init(0u32)
.code(ADDER_CODE_PATH)
Expand All @@ -95,8 +107,8 @@ impl AdderInteract {
self.state.set_adder_address(new_address);
}

async fn multi_deploy(&mut self, count: &u8) {
if *count == 0 {
async fn multi_deploy(&mut self, count: usize) {
if count == 0 {
println!("count must be greater than 0");
return;
}
Expand All @@ -105,13 +117,13 @@ impl AdderInteract {
println!("deploying {count} contracts...");

let mut buffer = self.interactor.homogenous_call_buffer();
for _ in 0..*count {
for _ in 0..count {
buffer.push_tx(|tx| {
tx.from(&self.wallet_address)
.typed(adder_proxy::AdderProxy)
.init(0u32)
.code(ADDER_CODE_PATH)
.gas(NumExpr("70,000,000"))
.gas(3_000_000)
.returns(ReturnsNewBech32Address)
});
}
Expand All @@ -128,6 +140,26 @@ impl AdderInteract {
}
}

async fn multi_add(&mut self, value: u32, count: usize) {
self.set_state().await;
println!("calling contract {count} times...");

let mut buffer = self.interactor.homogenous_call_buffer();
for _ in 0..count {
buffer.push_tx(|tx| {
tx.from(&self.wallet_address)
.to(self.state.current_adder_address())
.typed(adder_proxy::AdderProxy)
.add(value)
.gas(3_000_000)
});
}

let _ = buffer.run().await;

println!("successfully performed add {count} times");
}

async fn feed_contract_egld(&mut self) {
self.interactor
.tx()
Expand All @@ -144,7 +176,7 @@ impl AdderInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(NumExpr("30,000,000"))
.gas(3_000_000)
.typed(adder_proxy::AdderProxy)
.add(value)
.prepare_async()
Expand Down Expand Up @@ -175,7 +207,7 @@ impl AdderInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(NumExpr("30,000,000"))
.gas(3_000_000)
.typed(adder_proxy::AdderProxy)
.upgrade(BigUint::from(new_value))
.code_metadata(CodeMetadata::UPGRADEABLE)
Expand Down
12 changes: 8 additions & 4 deletions contracts/examples/adder/interact/src/basic_interact_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ pub enum InteractCliCommand {
#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct AddArgs {
/// The value to add
#[arg(short = 'v', long = "value", verbatim_doc_comment)]
#[arg(short = 'v', long = "value")]
pub value: u32,

/// Repeat this number of times
#[arg(short = 'c', long = "count", default_value = "1")]
pub count: usize,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct UpgradeArgs {
/// The value to add
#[arg(short = 'v', long = "value", verbatim_doc_comment)]
#[arg(short = 'v', long = "value")]
pub value: u32,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct MultiDeployArgs {
/// The number of contracts to deploy
#[arg(short = 'c', long = "count", verbatim_doc_comment)]
pub count: u8,
#[arg(short = 'c', long = "count")]
pub count: usize,
}
2 changes: 2 additions & 0 deletions framework/snippets/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ pub use crate::{
dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer,
};

pub use multiversx_sdk::wallet::Wallet;

pub use env_logger;
pub use tokio;

0 comments on commit 225e90c

Please sign in to comment.