Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing framework & interactor syntax harmonization #1162

Merged
merged 10 commits into from
Jul 14, 2023
97 changes: 49 additions & 48 deletions contracts/examples/adder/interact/src/adder_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use multiversx_sc_snippets::{
mandos_system::ScenarioRunner,
num_bigint::BigUint,
scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext},
scenario_model::{IntoBlockchainCall, Scenario, TransferStep, TxExpect},
scenario_model::{ScCallStep, ScDeployStep, ScQueryStep, Scenario, TransferStep, TxExpect},
standalone::retrieve_account_as_scenario_set_state,
test_wallets, ContractInfo,
},
Expand Down Expand Up @@ -95,30 +95,27 @@ impl AdderInteract {
async fn deploy(&mut self) {
self.set_state().await;

let mut typed_sc_deploy = self
.state
.default_adder()
.init(BigUint::from(0u64))
.into_blockchain_call()
.from(&self.wallet_address)
.code_metadata(CodeMetadata::all())
.contract_code("file:../output/adder.wasm", &InterpreterContext::default())
.gas_limit("70,000,000")
.expect(TxExpect::ok());

self.interactor.sc_deploy(&mut typed_sc_deploy).await;

let result = typed_sc_deploy.response().new_deployed_address();
if result.is_err() {
println!("deploy failed: {}", result.err().unwrap());
return;
}

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

let new_address_expr = format!("bech32:{new_address_bech32}");
self.state.set_adder_address(&new_address_expr);
self.interactor
.sc_deploy_use_result(
ScDeployStep::new()
.call(self.state.default_adder().init(BigUint::from(0u64)))
.from(&self.wallet_address)
.code_metadata(CodeMetadata::all())
.contract_code("file:../output/adder.wasm", &InterpreterContext::default())
.gas_limit("5,000,000")
.expect(TxExpect::ok()),
|new_address, tr| {
tr.result
.unwrap_or_else(|err| panic!("deploy failed: {}", 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_address(&new_address_expr);
},
)
.await;
}

async fn multi_deploy(&mut self, count: &u8) {
Expand All @@ -132,11 +129,8 @@ impl AdderInteract {

let mut steps = Vec::new();
for _ in 0..*count {
let typed_sc_deploy = self
.state
.default_adder()
.init(BigUint::from(0u64))
.into_blockchain_call()
let typed_sc_deploy = ScDeployStep::new()
.call(self.state.default_adder().init(0u32))
.from(&self.wallet_address)
.code_metadata(CodeMetadata::all())
.contract_code("file:../output/adder.wasm", &InterpreterContext::default())
Expand Down Expand Up @@ -175,28 +169,35 @@ impl AdderInteract {
}

async fn add(&mut self, value: u64) {
let mut typed_sc_call = self
.state
.adder()
.add(BigUint::from(value))
.into_blockchain_call()
.from(&self.wallet_address)
.gas_limit("70,000,000")
.expect(TxExpect::ok());

self.interactor.sc_call(&mut typed_sc_call).await;

let result = typed_sc_call.response().handle_signal_error_event();
if result.is_err() {
println!("performing add failed with: {}", result.err().unwrap());
return;
}
self.interactor
.sc_call_use_result(
ScCallStep::new()
.call(self.state.adder().add(value))
.from(&self.wallet_address)
.gas_limit("5,000,000")
.expect(TxExpect::ok()),
|tr| {
tr.result.unwrap_or_else(|err| {
panic!("performing add failed with: {}", err.message)
});
},
)
.await;

println!("successfully performed add");
}

async fn print_sum(&mut self) {
let sum: SingleValue<BigUint> = self.interactor.vm_query(self.state.adder().sum()).await;
println!("sum: {}", sum.into());
self.interactor
.sc_query_use_result(
ScQueryStep::new()
.call(self.state.adder().sum())
.expect(TxExpect::ok()),
|tr| {
let sum: SingleValue<BigUint> = tr.result.unwrap();
println!("sum: {}", sum.into());
},
)
.await;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ impl State {

/// Returns the adder contract
pub fn adder(&self) -> AdderContract {
AdderContract::new(self.adder_address.clone().unwrap())
AdderContract::new(
self.adder_address
.clone()
.expect("no known adder contract, deploy first"),
)
}

/// Returns the adder contract with default address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ fn adder_scenario_constructed_raw() {
.put_account(owner_address, Account::new().nonce(1))
.new_address(owner_address, 1, "sc:adder"),
)
.sc_deploy_step(
.sc_deploy_use_new_address(
ScDeployStep::new()
.from(owner_address)
.contract_code("file:output/adder.wasm", &ic)
.call(adder_contract.init(5u32))
.gas_limit("5,000,000")
.expect(TxExpect::ok().no_result()),
|new_address| {
assert_eq!(new_address, adder_contract.to_address());
},
)
.sc_query_step(
ScQueryStep::new()
.to(&adder_contract)
.call_expect(adder_contract.sum(), SingleValue::from(BigUint::from(5u32))),
.call(adder_contract.sum())
.expect_value(SingleValue::from(BigUint::from(5u32))),
)
.sc_call_step(
ScCallStep::new()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ fn crowdfunding_scenario_rust_test() {
.put_account(owner_addr, Account::new())
.new_address(owner_addr, 0, &cf_sc),
);
let (_, ()) = cf_sc
.init(
2_000u32,
deadline,
EgldOrEsdtTokenIdentifier::esdt(cf_token_id_value),
)
.into_blockchain_call()
.from(owner_addr)
.contract_code("file:output/crowdfunding-esdt.wasm", &ctx)
.gas_limit("5,000,000")
.expect(TxExpect::ok().no_result())
.execute(&mut world);

world.sc_deploy_step(
ScDeployStep::new()
.from(owner_addr)
.contract_code("file:output/crowdfunding-esdt.wasm", &ctx)
.call(cf_sc.init(
2_000u32,
deadline,
EgldOrEsdtTokenIdentifier::esdt(cf_token_id_value),
))
.gas_limit("5,000,000")
.expect(TxExpect::ok().no_result()),
);

// setup user accounts
world
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ impl MultisigInteract {

async fn quorum_reached(&mut self, action_id: usize) -> bool {
self.interactor
.vm_query(self.state.multisig().quorum_reached(action_id))
.quick_query(self.state.multisig().quorum_reached(action_id))
.await
}

async fn signed(&mut self, signer: &Address, action_id: usize) -> bool {
self.interactor
.vm_query(self.state.multisig().signed(signer, action_id))
.quick_query(self.state.multisig().signed(signer, action_id))
.await
}

Expand Down Expand Up @@ -395,7 +395,7 @@ impl MultisigInteract {
async fn print_quorum(&mut self) {
let quorum: SingleValue<usize> = self
.interactor
.vm_query(self.state.multisig().quorum())
.quick_query(self.state.multisig().quorum())
.await;

println!("quorum: {}", quorum.into());
Expand All @@ -404,7 +404,7 @@ impl MultisigInteract {
async fn print_board(&mut self) {
let board: SingleValue<usize> = self
.interactor
.vm_query(self.state.multisig().num_board_members())
.quick_query(self.state.multisig().num_board_members())
.await;

println!("board: {}", board.into());
Expand Down
Loading
Loading