multiversx_sc_scenario/scenario/model/step/
set_state_step.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::collections::BTreeMap;

use crate::scenario::model::{
    Account, AddressKey, AddressValue, BlockInfo, BytesValue, NewAddress, U64Value,
};

#[derive(Debug, Default, Clone)]
pub struct SetStateStep {
    pub comment: Option<String>,
    pub accounts: BTreeMap<AddressKey, Account>,
    pub new_addresses: Vec<NewAddress>,
    pub new_token_identifiers: Vec<String>,
    pub block_hashes: Vec<BytesValue>,
    pub previous_block_info: Box<Option<BlockInfo>>,
    pub current_block_info: Box<Option<BlockInfo>>,
}

impl SetStateStep {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn put_account<A>(mut self, address_expr: A, account: Account) -> Self
    where
        AddressKey: From<A>,
    {
        let address_key = AddressKey::from(address_expr);
        self.accounts.insert(address_key, account);
        self
    }

    pub fn new_address<CA, NA>(
        mut self,
        creator_address_expr: CA,
        creator_nonce_expr: u64,
        new_address_expr: NA,
    ) -> Self
    where
        AddressValue: From<CA>,
        AddressValue: From<NA>,
    {
        self.new_addresses.push(NewAddress {
            creator_address: AddressValue::from(creator_address_expr),
            creator_nonce: U64Value::from(creator_nonce_expr),
            new_address: AddressValue::from(new_address_expr),
        });
        self
    }

    pub fn new_token_identifier<T>(mut self, token_identifier: T) -> Self
    where
        String: From<T>,
    {
        self.new_token_identifiers
            .push(String::from(token_identifier));
        self
    }

    pub fn block_epoch<N>(mut self, block_epoch_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_epoch = U64Value::from(block_epoch_expr);

        let mut block_info = self.current_block_info.unwrap_or_default();
        block_info.block_epoch = Some(block_epoch);
        self.current_block_info = Box::new(Some(block_info));
        self
    }

    pub fn block_nonce<N>(mut self, block_nonce_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_nonce = U64Value::from(block_nonce_expr);

        let mut block_info = self.current_block_info.unwrap_or_default();
        block_info.block_nonce = Some(block_nonce);
        self.current_block_info = Box::new(Some(block_info));
        self
    }

    pub fn block_round<N>(mut self, block_round_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_round = U64Value::from(block_round_expr);

        let mut block_info = self.current_block_info.unwrap_or_default();
        block_info.block_round = Some(block_round);
        self.current_block_info = Box::new(Some(block_info));
        self
    }

    pub fn block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_timestamp = U64Value::from(block_timestamp_expr);

        let mut block_info = self.current_block_info.unwrap_or_default();
        block_info.block_timestamp = Some(block_timestamp);
        self.current_block_info = Box::new(Some(block_info));
        self
    }

    pub fn block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
    where
        BytesValue: From<B>,
    {
        let block_random_seed = BytesValue::from(block_random_seed_expr);

        let mut block_info = self.current_block_info.unwrap_or_default();
        block_info.block_random_seed = Some(block_random_seed);
        self.current_block_info = Box::new(Some(block_info));
        self
    }

    pub fn prev_block_epoch<N>(mut self, block_epoch_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_epoch = U64Value::from(block_epoch_expr);

        let mut block_info = self.previous_block_info.unwrap_or_default();
        block_info.block_epoch = Some(block_epoch);
        self.previous_block_info = Box::new(Some(block_info));
        self
    }

    pub fn prev_block_nonce<N>(mut self, block_nonce_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_nonce = U64Value::from(block_nonce_expr);

        let mut block_info = self.previous_block_info.unwrap_or_default();
        block_info.block_nonce = Some(block_nonce);
        self.previous_block_info = Box::new(Some(block_info));
        self
    }

    pub fn prev_block_round<N>(mut self, block_round_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_round = U64Value::from(block_round_expr);

        let mut block_info = self.previous_block_info.unwrap_or_default();
        block_info.block_round = Some(block_round);
        self.previous_block_info = Box::new(Some(block_info));
        self
    }

    pub fn prev_block_timestamp<N>(mut self, block_timestamp_expr: N) -> Self
    where
        U64Value: From<N>,
    {
        let block_timestamp = U64Value::from(block_timestamp_expr);

        let mut block_info = self.previous_block_info.unwrap_or_default();
        block_info.block_timestamp = Some(block_timestamp);
        self.previous_block_info = Box::new(Some(block_info));
        self
    }

    pub fn prev_block_random_seed<B>(mut self, block_random_seed_expr: B) -> Self
    where
        BytesValue: From<B>,
    {
        let block_random_seed = BytesValue::from(block_random_seed_expr);

        let mut block_info = self.previous_block_info.unwrap_or_default();
        block_info.block_random_seed = Some(block_random_seed);
        self.previous_block_info = Box::new(Some(block_info));
        self
    }
}