Skip to main content

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