multiversx_sc_scenario/scenario/model/step/
sc_query_step.rs1use 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 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 pub fn expect(mut self, expect: TxExpect) -> Self {
64 self.expect = Some(expect);
65 self
66 }
67
68 pub fn no_expect(mut self) -> Self {
72 self.expect = None;
73 self
74 }
75
76 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}