Skip to content

Commit

Permalink
merge feat/unified
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Apr 8, 2024
2 parents 16ff8d1 + 3710414 commit 11db738
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 108 deletions.
125 changes: 66 additions & 59 deletions contracts/examples/multisig/interact/src/multisig_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ mod multisig_interact_state;
mod multisig_interact_wegld;

use clap::Parser;
use multisig::{
multisig_perform::ProxyTrait as _, multisig_propose::ProxyTrait as _, multisig_proxy,
ProxyTrait as _,
};
use multisig::multisig_proxy;
use multisig_interact_config::Config;
use multisig_interact_state::State;
use multiversx_sc_scenario::{
Expand All @@ -25,7 +22,7 @@ use multiversx_sc_snippets::{
api::StaticApi, bech32, scenario_format::interpret_trait::InterpreterContext,
scenario_model::*, ContractInfo,
},
tokio, Interactor, InteractorPrepareAsync, StepBuffer,
tokio, Interactor, InteractorPrepareAsync,
};

const SYSTEM_SC_BECH32: &str = "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u";
Expand Down Expand Up @@ -238,8 +235,8 @@ impl MultisigInteract {
}

async fn perform_action(&mut self, action_id: usize, gas_expr: u64) {
if !self.quorum_reached(action_id).await && !self.sign(action_id).await {
return;
if !self.quorum_reached(action_id).await {
self.sign(&[action_id]).await
}
println!("quorum reached for action `{action_id}`");

Expand All @@ -257,36 +254,43 @@ impl MultisigInteract {
println!("successfully performed action `{action_id}`");
}

async fn perform_actions(&mut self, actions: Vec<usize>, gas_expr: &str) {
let mut steps = Vec::new();
for action_id in actions.iter() {
if !self.quorum_reached(*action_id).await && !self.sign(*action_id).await {
continue;
async fn perform_actions(&mut self, action_ids: Vec<usize>, gas_expr: u64) {
let multisig_address = self.state.multisig().to_address();

let mut actions_no_quorum_reached = Vec::new();
for &action_id in &action_ids {
if self.quorum_reached(action_id).await {
println!("quorum reached for action `{action_id}`");
} else {
actions_no_quorum_reached.push(action_id)
}
println!("quorum reached for action `{action_id}`");
}

let typed_sc_call = ScCallStep::new()
.call(self.state.multisig().perform_action_endpoint(action_id))
.from(&self.wallet_address)
.gas_limit(gas_expr);
self.sign(&actions_no_quorum_reached).await;

steps.push(typed_sc_call);
let from = &self.wallet_address;
let mut buffer = self.interactor.homogenous_call_buffer();
for action_id in action_ids {
buffer.push_tx(|tx| {
tx.from(from)
.to(&multisig_address)
.gas(gas_expr)
.typed(multisig_proxy::MultisigProxy)
.perform_action_endpoint(action_id)
.returns(ReturnsResult)
});
}

self.interactor
.multi_sc_exec(StepBuffer::from_sc_call_vec(&mut steps))
.await;
let deployed_addresses = buffer.run().await;

for (i, action_id) in actions.iter().enumerate() {
if !steps[i].response().is_success() {
for (action_id, address) in deployed_addresses.iter().enumerate() {
println!("successfully performed action `{action_id}`");
if address.is_some() {
println!(
"perform action `{action_id}` failed with: {}",
steps[i].response().tx_error
);
continue;
"new deployed address for action `{action_id}: {:#?}`",
address.clone().into_option().unwrap()
)
}

println!("successfully performed action `{action_id}`");
}
}

Expand Down Expand Up @@ -314,42 +318,45 @@ impl MultisigInteract {
.await
}

async fn sign(&mut self, action_id: usize) -> bool {
println!("signing action `{action_id}`...");
let mut steps = Vec::new();
for signer in self.board().iter() {
if self.signed(signer, action_id).await {
println!(
"{} - already signed action `{action_id}`",
bech32::encode(signer)
);
continue;
async fn sign(&mut self, action_ids: &[usize]) {
println!("signing actions `{action_ids:?}`...");
let multisig_address = self.state.multisig().to_address();

let mut pending_signers = Vec::<(Address, usize)>::new();
for &action_id in action_ids {
for signer in self.board().iter() {
if self.signed(signer, action_id).await {
println!(
"{} - already signed action `{action_id}`",
bech32::encode(signer)
);
} else {
pending_signers.push((signer.clone(), action_id));
}
}
}

let typed_sc_call = ScCallStep::new()
.call(self.state.multisig().sign(action_id))
.from(signer)
.gas_limit("15,000,000");

steps.push(typed_sc_call);
let mut buffer = self.interactor.homogenous_call_buffer();
for (signer, action_id) in pending_signers {
buffer.push_tx(|tx| {
tx.from(signer)
.to(&multisig_address)
.gas(15_000_000u64)
.typed(multisig_proxy::MultisigProxy)
.sign(action_id)
});
}

self.interactor
.multi_sc_exec(StepBuffer::from_sc_call_vec(&mut steps))
.await;
buffer.run().await;

for step in steps.iter() {
if !step.response().is_success() {
println!(
"perform sign `{action_id}` failed with: {}",
step.response().tx_error
);
return false;
}
}
println!("successfully performed sign action `{action_ids:?}`");
}

println!("successfully performed sign action `{action_id}`");
true
async fn sign_if_quorum_not_reached(&mut self, action_id: usize) {
if !self.quorum_reached(action_id).await {
self.sign(&[action_id]).await;
}
println!("quorum reached for action `{action_id}`");
}

async fn dns_register(&mut self, name: &str) {
Expand Down
52 changes: 16 additions & 36 deletions contracts/examples/multisig/interact/src/multisig_interact_nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ impl MultisigInteract {

println!("perfoming issue collection with all roles action `{action_id}`...");

if !self.quorum_reached(action_id).await && !self.sign(action_id).await {
return;
}
println!("quorum reached for action `{action_id}`");
self.sign_if_quorum_not_reached(action_id).await;

let new_token_id = self
.interactor
Expand Down Expand Up @@ -127,10 +124,8 @@ impl MultisigInteract {

println!("perfoming issue collection action `{action_id}`...");

if !self.quorum_reached(action_id).await && !self.sign(action_id).await {
return;
}
println!("quorum reached for action `{action_id}`");
self.sign_if_quorum_not_reached(action_id).await;

let new_token_id = self
.interactor
.tx()
Expand Down Expand Up @@ -189,17 +184,20 @@ impl MultisigInteract {
println!("creating items...");

let multisig_address = self.state.multisig().to_address();
let mut steps = Vec::new();

let mut buffer = self.interactor.homogenous_call_buffer();
for item_index in 0..NUM_ITEMS {
let item_name = format!("Test collection item #{item_index}");
let image_cid = format!(
"https://ipfs.io/ipfs/QmYyAaEf1phJS5mN6wfou5de5GbpUddBxTY1VekKcjd5PC/nft{item_index:02}.png"
);

let typed_sc_call = ScCallStep::new()
.call(
self.state.multisig().propose_async_call(
buffer.push_tx(|tx| {
tx.from(&self.wallet_address)
.to(&multisig_address)
.gas(10_000_000u64)
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
&multisig_address,
0u64,
FunctionCall::new("ESDTNFTCreate")
Expand All @@ -210,34 +208,16 @@ impl MultisigInteract {
.argument(&Empty)
.argument(&METADATA)
.argument(&image_cid),
),
)
.from(&self.wallet_address)
.gas_limit("10,000,000");

steps.push(typed_sc_call);
)
.returns(ReturnsResult)
});
}

self.interactor
.multi_sc_exec(StepBuffer::from_sc_call_vec(&mut steps))
.await;

let mut actions = Vec::new();
for step in steps.iter() {
let result = step.result();
if result.is_err() {
println!(
"propose ESDTNFTCreate failed with: {}",
result.err().unwrap()
);
return;
}

let action_id = result.unwrap();
let action_ids = buffer.run().await;
for action_id in action_ids.iter() {
println!("successfully proposed ESDTNFTCreate action `{action_id}`");
actions.push(action_id);
}

self.perform_actions(actions, "30,000,000").await;
self.perform_actions(action_ids, 30_000_000u64).await;
}
}
22 changes: 9 additions & 13 deletions contracts/examples/multisig/src/multisig_perform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,13 @@ pub trait MultisigPerformModule:
&call_data.endpoint_name,
call_data.arguments.as_multi(),
);

self.tx()
.to(&call_data.to)
.to(call_data.to)
.egld(call_data.egld_amount)
.gas(gas)
.raw_call(call_data.endpoint_name)
.arguments_raw(call_data.arguments.into())
.gas(gas)
.egld(&call_data.egld_amount)
.transfer_execute();

OptionalValue::None
},
Action::SendAsyncCall(call_data) => {
Expand Down Expand Up @@ -214,15 +212,14 @@ pub trait MultisigPerformModule:
);
let new_address = self
.tx()
.egld(amount)
.gas(gas_left)
.egld(&amount)
.raw_deploy()
.arguments_raw(arguments.into())
.from_source(source)
.code_metadata(code_metadata)
.arguments_raw(arguments.into())
.returns(ReturnsNewManagedAddress)
.sync_call();

OptionalValue::Some(new_address)
},
Action::SCUpgradeFromSource {
Expand All @@ -242,16 +239,15 @@ pub trait MultisigPerformModule:
gas_left,
arguments.as_multi(),
);

self.tx()
.to(&sc_address)
.to(sc_address)
.egld(amount)
.gas(gas_left)
.raw_upgrade()
.arguments_raw(arguments.into())
.egld(&amount)
.from_source(source)
.code_metadata(code_metadata)
.arguments_raw(arguments.into())
.upgrade_async_call_and_exit();

OptionalValue::None
},
}
Expand Down
Loading

0 comments on commit 11db738

Please sign in to comment.