multiversx_sc_snippets/multi/
interactor_step.rs

1use crate::sdk::data::transaction::Transaction;
2use multiversx_sc_scenario::{
3    mandos_system::ScenarioRunner,
4    scenario::tx_to_step::StepWithResponse,
5    scenario_model::{AddressValue, ScCallStep, ScDeployStep, TxResponse},
6};
7use multiversx_sdk::gateway::GatewayAsyncService;
8
9use crate::InteractorBase;
10
11pub enum InteractorStepRef<'a> {
12    ScCall(&'a mut ScCallStep),
13    ScDeploy(&'a mut ScDeployStep),
14}
15
16impl InteractorStepRef<'_> {
17    pub fn to_transaction<GatewayProxy: GatewayAsyncService>(
18        &self,
19        interactor: &InteractorBase<GatewayProxy>,
20    ) -> Transaction {
21        match self {
22            InteractorStepRef::ScCall(sc_call) => interactor.tx_call_to_blockchain_tx(&sc_call.tx),
23            InteractorStepRef::ScDeploy(sc_deploy) => {
24                interactor.sc_deploy_to_blockchain_tx(sc_deploy)
25            },
26        }
27    }
28
29    pub fn sender_address(&self) -> &AddressValue {
30        match self {
31            InteractorStepRef::ScCall(sc_call) => &sc_call.tx.from,
32            InteractorStepRef::ScDeploy(sc_deploy) => &sc_deploy.tx.from,
33        }
34    }
35
36    pub fn run_step(&mut self, step_runner: &mut dyn ScenarioRunner) {
37        match self {
38            InteractorStepRef::ScCall(sc_call) => step_runner.run_sc_call_step(sc_call),
39            InteractorStepRef::ScDeploy(sc_deploy) => step_runner.run_sc_deploy_step(sc_deploy),
40        }
41    }
42
43    pub fn set_response(&mut self, tx_response: TxResponse) {
44        match self {
45            InteractorStepRef::ScCall(sc_call) => sc_call.save_response(tx_response),
46            InteractorStepRef::ScDeploy(sc_deploy) => sc_deploy.save_response(tx_response),
47        }
48    }
49}
50
51/// Describes a scenario step that can be executed in an interactor.
52pub trait InteractorStep: StepWithResponse {
53    fn as_interactor_step(&mut self) -> InteractorStepRef<'_>;
54}
55
56impl InteractorStep for ScCallStep {
57    fn as_interactor_step(&mut self) -> InteractorStepRef<'_> {
58        InteractorStepRef::ScCall(self)
59    }
60}
61
62impl InteractorStep for ScDeployStep {
63    fn as_interactor_step(&mut self) -> InteractorStepRef<'_> {
64        InteractorStepRef::ScDeploy(self)
65    }
66}