multiversx_sc_scenario/scenario/model/step/
sc_deploy_step.rs

1use multiversx_sc::types::H256;
2
3use crate::{
4    scenario::model::{AddressValue, BigUintValue, BytesValue, TxDeploy, TxExpect, U64Value},
5    scenario_model::TxResponse,
6};
7
8use crate::multiversx_sc::types::CodeMetadata;
9
10#[derive(Debug, Clone)]
11pub struct ScDeployStep {
12    pub id: String,
13    pub tx_id: Option<String>,
14    pub explicit_tx_hash: Option<H256>,
15    pub comment: Option<String>,
16    pub tx: Box<TxDeploy>,
17    pub expect: Option<TxExpect>,
18    pub response: Option<TxResponse>,
19}
20
21impl Default for ScDeployStep {
22    fn default() -> Self {
23        Self {
24            id: Default::default(),
25            tx_id: Default::default(),
26            explicit_tx_hash: Default::default(),
27            comment: Default::default(),
28            tx: Default::default(),
29            expect: Some(TxExpect::ok()),
30            response: Default::default(),
31        }
32    }
33}
34
35impl ScDeployStep {
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    pub fn from<V>(mut self, expr: V) -> Self
41    where
42        AddressValue: From<V>,
43    {
44        self.tx.from = AddressValue::from(expr);
45        self
46    }
47
48    pub fn egld_value<V>(mut self, expr: V) -> Self
49    where
50        BigUintValue: From<V>,
51    {
52        self.tx.egld_value = BigUintValue::from(expr);
53        self
54    }
55
56    pub fn code_metadata(mut self, code_metadata: CodeMetadata) -> Self {
57        self.tx.code_metadata = code_metadata;
58        self
59    }
60
61    pub fn code<V>(mut self, expr: V) -> Self
62    where
63        BytesValue: From<V>,
64    {
65        self.tx.contract_code = BytesValue::from(expr);
66        self
67    }
68
69    pub fn argument(mut self, expr: &str) -> Self {
70        self.tx.arguments.push(BytesValue::from(expr));
71        self
72    }
73
74    pub fn gas_limit<V>(mut self, value: V) -> Self
75    where
76        U64Value: From<V>,
77    {
78        self.tx.gas_limit = U64Value::from(value);
79        self
80    }
81
82    /// Adds a custom expect section to the tx.
83    pub fn expect(mut self, expect: TxExpect) -> Self {
84        self.expect = Some(expect);
85        self
86    }
87
88    /// Explicitly states that no tx expect section should be added and no checks should be performed.
89    ///
90    /// Note: by default a basic `TxExpect::ok()` is added, which checks that status is 0 and nothing else.
91    pub fn no_expect(mut self) -> Self {
92        self.expect = None;
93        self
94    }
95
96    /// Unwraps the response, if available.
97    pub fn response(&self) -> &TxResponse {
98        self.response
99            .as_ref()
100            .expect("SC deploy response not yet available")
101    }
102
103    pub fn save_response(&mut self, mut tx_response: TxResponse) {
104        if let Some(expect) = &mut self.expect {
105            if expect.build_from_response {
106                expect.update_from_response(&tx_response)
107            }
108        }
109        if tx_response.tx_hash.is_none() {
110            tx_response.tx_hash = self
111                .explicit_tx_hash
112                .as_ref()
113                .map(|vm_hash| vm_hash.as_array().into());
114        }
115        self.response = Some(tx_response);
116    }
117}
118
119impl AsMut<ScDeployStep> for ScDeployStep {
120    fn as_mut(&mut self) -> &mut ScDeployStep {
121        self
122    }
123}