multiversx_sc_scenario/scenario/model/step/
sc_query_step.rs

1use multiversx_sc::types::H256;
2
3use crate::{
4    scenario::model::{AddressValue, BytesValue, TxExpect, TxQuery},
5    scenario_model::TxResponse,
6};
7
8#[derive(Debug, Clone)]
9pub struct ScQueryStep {
10    pub id: String,
11    pub tx_id: Option<String>,
12    pub explicit_tx_hash: Option<H256>,
13    pub comment: Option<String>,
14    pub tx: Box<TxQuery>,
15    pub expect: Option<TxExpect>,
16    pub response: Option<TxResponse>,
17}
18
19impl Default for ScQueryStep {
20    fn default() -> Self {
21        Self {
22            id: Default::default(),
23            tx_id: Default::default(),
24            explicit_tx_hash: Default::default(),
25            comment: Default::default(),
26            tx: Default::default(),
27            expect: Some(TxExpect::ok()),
28            response: Default::default(),
29        }
30    }
31}
32
33impl ScQueryStep {
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Sets the tx id for mandos.
39    pub fn id(mut self, id: impl ToString) -> Self {
40        self.id = id.to_string();
41        self
42    }
43
44    pub fn to<A>(mut self, address: A) -> Self
45    where
46        AddressValue: From<A>,
47    {
48        self.tx.to = AddressValue::from(address);
49        self
50    }
51
52    pub fn function(mut self, expr: &str) -> Self {
53        self.tx.function = expr.to_string();
54        self
55    }
56
57    pub fn argument(mut self, expr: &str) -> Self {
58        self.tx.arguments.push(BytesValue::from(expr));
59        self
60    }
61
62    /// Adds a custom expect section to the tx.
63    pub fn expect(mut self, expect: TxExpect) -> Self {
64        self.expect = Some(expect);
65        self
66    }
67
68    /// Explicitly states that no tx expect section should be added and no checks should be performed.
69    ///
70    /// Note: by default a basic `TxExpect::ok()` is added, which checks that status is 0 and nothing else.
71    pub fn no_expect(mut self) -> Self {
72        self.expect = None;
73        self
74    }
75
76    /// Unwraps the response, if available.
77    pub fn response(&self) -> &TxResponse {
78        self.response
79            .as_ref()
80            .expect("SC query response not yet available")
81    }
82
83    pub fn save_response(&mut self, tx_response: TxResponse) {
84        if let Some(expect) = &mut self.expect {
85            if expect.build_from_response {
86                expect.update_from_response(&tx_response)
87            }
88        }
89        self.response = Some(tx_response);
90    }
91}
92
93impl AsMut<ScQueryStep> for ScQueryStep {
94    fn as_mut(&mut self) -> &mut ScQueryStep {
95        self
96    }
97}