multiversx_sc_scenario/scenario/model/step/
set_state_step.rs

1use std::collections::BTreeMap;
2
3use crate::scenario::model::{
4    Account, AddressKey, AddressValue, BlockInfo, BytesValue, NewAddress, U64Value,
5};
6
7#[derive(Debug, Default, Clone)]
8pub struct SetStateStep {
9    pub comment: Option<String>,
10    pub accounts: BTreeMap<AddressKey, Account>,
11    pub new_addresses: Vec<NewAddress>,
12    pub new_token_identifiers: Vec<String>,
13    pub block_hashes: Vec<BytesValue>,
14    pub previous_block_info: Box<Option<BlockInfo>>,
15    pub current_block_info: Box<Option<BlockInfo>>,
16    pub epoch_start_block_info: Box<Option<BlockInfo>>,
17    pub block_round_time_ms: Option<U64Value>,
18}
19
20impl SetStateStep {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn put_account<A>(mut self, address_expr: A, account: Account) -> Self
26    where
27        AddressKey: From<A>,
28    {
29        let address_key = AddressKey::from(address_expr);
30        self.accounts.insert(address_key, account);
31        self
32    }
33
34    pub fn new_address<CA, NA>(
35        mut self,
36        creator_address_expr: CA,
37        creator_nonce_expr: u64,
38        new_address_expr: NA,
39    ) -> Self
40    where
41        AddressValue: From<CA>,
42        AddressValue: From<NA>,
43    {
44        self.new_addresses.push(NewAddress {
45            creator_address: AddressValue::from(creator_address_expr),
46            creator_nonce: U64Value::from(creator_nonce_expr),
47            new_address: AddressValue::from(new_address_expr),
48        });
49        self
50    }
51
52    pub fn new_token_identifier<T>(mut self, token_identifier: T) -> Self
53    where
54        String: From<T>,
55    {
56        self.new_token_identifiers
57            .push(String::from(token_identifier));
58        self
59    }
60
61    pub fn block_epoch<N>(mut self, block_epoch_expr: N) -> Self
62    where
63        U64Value: From<N>,
64    {
65        let block_epoch = U64Value::from(block_epoch_expr);
66
67        let mut block_info = self.current_block_info.unwrap_or_default();
68        block_info.block_epoch = Some(block_epoch);
69        self.current_block_info = Box::new(Some(block_info));
70        self
71    }
72
73    pub fn block_nonce<N>(mut self, block_nonce_expr: N) -> Self
74    where
75        U64Value: From<N>,
76    {
77        let block_nonce = U64Value::from(block_nonce_expr);
78
79        let mut block_info = self.current_block_info.unwrap_or_default();
80        block_info.block_nonce = Some(block_nonce);
81        self.current_block_info = Box::new(Some(block_info));
82        self
83    }
84
85    pub fn block_round<N>(mut self, block_round_expr: N) -> Self
86    where
87        U64Value: From<N>,
88    {
89        let block_round = U64Value::from(block_round_expr);
90
91        let mut block_info = self.current_block_info.unwrap_or_default();
92        block_info.block_round = Some(block_round);
93        self.current_block_info = Box::new(Some(block_info));
94        self
95    }
96
97    pub fn block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
98    where
99        U64Value: From<N>,
100    {
101        let block_timestamp = U64Value::from(block_timestamp_expr);
102
103        let mut block_info = self.current_block_info.unwrap_or_default();
104        block_info.block_timestamp = Some(block_timestamp);
105        self.current_block_info = Box::new(Some(block_info));
106        self
107    }
108
109    pub fn block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
110    where
111        BytesValue: From<B>,
112    {
113        let block_random_seed = BytesValue::from(block_random_seed_expr);
114
115        let mut block_info = self.current_block_info.unwrap_or_default();
116        block_info.block_random_seed = Some(block_random_seed);
117        self.current_block_info = Box::new(Some(block_info));
118        self
119    }
120
121    pub fn prev_block_epoch<N>(mut self, block_epoch_expr: N) -> Self
122    where
123        U64Value: From<N>,
124    {
125        let block_epoch = U64Value::from(block_epoch_expr);
126
127        let mut block_info = self.previous_block_info.unwrap_or_default();
128        block_info.block_epoch = Some(block_epoch);
129        self.previous_block_info = Box::new(Some(block_info));
130        self
131    }
132
133    pub fn prev_block_nonce<N>(mut self, block_nonce_expr: N) -> Self
134    where
135        U64Value: From<N>,
136    {
137        let block_nonce = U64Value::from(block_nonce_expr);
138
139        let mut block_info = self.previous_block_info.unwrap_or_default();
140        block_info.block_nonce = Some(block_nonce);
141        self.previous_block_info = Box::new(Some(block_info));
142        self
143    }
144
145    pub fn prev_block_round<N>(mut self, block_round_expr: N) -> Self
146    where
147        U64Value: From<N>,
148    {
149        let block_round = U64Value::from(block_round_expr);
150
151        let mut block_info = self.previous_block_info.unwrap_or_default();
152        block_info.block_round = Some(block_round);
153        self.previous_block_info = Box::new(Some(block_info));
154        self
155    }
156
157    pub fn prev_block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
158    where
159        U64Value: From<N>,
160    {
161        let block_timestamp = U64Value::from(block_timestamp_expr);
162
163        let mut block_info = self.previous_block_info.unwrap_or_default();
164        block_info.block_timestamp = Some(block_timestamp);
165        self.previous_block_info = Box::new(Some(block_info));
166        self
167    }
168
169    pub fn prev_block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
170    where
171        BytesValue: From<B>,
172    {
173        let block_random_seed = BytesValue::from(block_random_seed_expr);
174
175        let mut block_info = self.previous_block_info.unwrap_or_default();
176        block_info.block_random_seed = Some(block_random_seed);
177        self.previous_block_info = Box::new(Some(block_info));
178        self
179    }
180}