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    #[deprecated(since = "0.63.2", note = "Renamed to block_timestamp_seconds")]
98    pub fn block_timestamp<N>(self, block_timestamp_expr: N) -> Self
99    where
100        U64Value: From<N>,
101    {
102        self.block_timestamp_seconds(block_timestamp_expr)
103    }
104
105    pub fn block_timestamp_seconds<N>(mut self, block_timestamp_expr: N) -> Self
106    where
107        U64Value: From<N>,
108    {
109        let block_timestamp = U64Value::from(block_timestamp_expr);
110
111        let mut block_info = self.current_block_info.unwrap_or_default();
112        block_info.block_timestamp = Some(block_timestamp);
113        self.current_block_info = Box::new(Some(block_info));
114        self
115    }
116
117    #[deprecated(since = "0.63.2", note = "Renamed to block_timestamp_millis")]
118    pub fn block_timestamp_ms<N>(self, block_timestamp_ms_expr: N) -> Self
119    where
120        U64Value: From<N>,
121    {
122        self.block_timestamp_millis(block_timestamp_ms_expr)
123    }
124
125    pub fn block_timestamp_millis<N>(mut self, block_timestamp_ms_expr: N) -> Self
126    where
127        U64Value: From<N>,
128    {
129        let block_timestamp_ms = U64Value::from(block_timestamp_ms_expr);
130
131        let mut block_info = self.current_block_info.unwrap_or_default();
132        block_info.block_timestamp_ms = Some(block_timestamp_ms);
133        self.current_block_info = Box::new(Some(block_info));
134        self
135    }
136
137    pub fn block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
138    where
139        BytesValue: From<B>,
140    {
141        let block_random_seed = BytesValue::from(block_random_seed_expr);
142
143        let mut block_info = self.current_block_info.unwrap_or_default();
144        block_info.block_random_seed = Some(block_random_seed);
145        self.current_block_info = Box::new(Some(block_info));
146        self
147    }
148
149    pub fn prev_block_epoch<N>(mut self, block_epoch_expr: N) -> Self
150    where
151        U64Value: From<N>,
152    {
153        let block_epoch = U64Value::from(block_epoch_expr);
154
155        let mut block_info = self.previous_block_info.unwrap_or_default();
156        block_info.block_epoch = Some(block_epoch);
157        self.previous_block_info = Box::new(Some(block_info));
158        self
159    }
160
161    pub fn prev_block_nonce<N>(mut self, block_nonce_expr: N) -> Self
162    where
163        U64Value: From<N>,
164    {
165        let block_nonce = U64Value::from(block_nonce_expr);
166
167        let mut block_info = self.previous_block_info.unwrap_or_default();
168        block_info.block_nonce = Some(block_nonce);
169        self.previous_block_info = Box::new(Some(block_info));
170        self
171    }
172
173    pub fn prev_block_round<N>(mut self, block_round_expr: N) -> Self
174    where
175        U64Value: From<N>,
176    {
177        let block_round = U64Value::from(block_round_expr);
178
179        let mut block_info = self.previous_block_info.unwrap_or_default();
180        block_info.block_round = Some(block_round);
181        self.previous_block_info = Box::new(Some(block_info));
182        self
183    }
184
185    pub fn prev_block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
186    where
187        U64Value: From<N>,
188    {
189        let block_timestamp = U64Value::from(block_timestamp_expr);
190
191        let mut block_info = self.previous_block_info.unwrap_or_default();
192        block_info.block_timestamp = Some(block_timestamp);
193        self.previous_block_info = Box::new(Some(block_info));
194        self
195    }
196
197    pub fn prev_block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
198    where
199        BytesValue: From<B>,
200    {
201        let block_random_seed = BytesValue::from(block_random_seed_expr);
202
203        let mut block_info = self.previous_block_info.unwrap_or_default();
204        block_info.block_random_seed = Some(block_random_seed);
205        self.previous_block_info = Box::new(Some(block_info));
206        self
207    }
208}