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    pub fn to<A>(mut self, address: A) -> Self
39    where
40        AddressValue: From<A>,
41    {
42        self.tx.to = AddressValue::from(address);
43        self
44    }
45
46    pub fn function(mut self, expr: &str) -> Self {
47        self.tx.function = expr.to_string();
48        self
49    }
50
51    pub fn argument(mut self, expr: &str) -> Self {
52        self.tx.arguments.push(BytesValue::from(expr));
53        self
54    }
55
56    /// Adds a custom expect section to the tx.
57    pub fn expect(mut self, expect: TxExpect) -> Self {
58        self.expect = Some(expect);
59        self
60    }
61
62    /// Explicitly states that no tx expect section should be added and no checks should be performed.
63    ///
64    /// Note: by default a basic `TxExpect::ok()` is added, which checks that status is 0 and nothing else.
65    pub fn no_expect(mut self) -> Self {
66        self.expect = None;
67        self
68    }
69
70    /// Unwraps the response, if available.
71    pub fn response(&self) -> &TxResponse {
72        self.response
73            .as_ref()
74            .expect("SC query response not yet available")
75    }
76
77    pub fn save_response(&mut self, tx_response: TxResponse) {
78        if let Some(expect) = &mut self.expect {
79            if expect.build_from_response {
80                expect.update_from_response(&tx_response)
81            }
82        }
83        self.response = Some(tx_response);
84    }
85}
86
87impl AsMut<ScQueryStep> for ScQueryStep {
88    fn as_mut(&mut self) -> &mut ScQueryStep {
89        self
90    }
91}