Skip to main content

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