multiversx_sc_scenario/facade/world_tx/
scenario_set_state.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
mod scenario_set_account;
mod scenario_set_block;
mod scenario_set_new_address;

use crate::{
    imports::StaticApi,
    scenario::{
        tx_to_step::{address_annotated, big_uint_annotated, u64_annotated},
        ScenarioRunner,
    },
    scenario_model::{AddressKey, BigUintValue, NewAddress, SetStateStep},
    ScenarioTxEnvData, ScenarioWorld,
};

use multiversx_chain_vm::world_mock::EsdtInstanceMetadata;
use multiversx_sc::{
    proxy_imports::TopEncode,
    types::{AnnotatedValue, BigUint, EsdtLocalRole, ManagedAddress},
};
use scenario_set_account::AccountItem;
use scenario_set_block::BlockItem;
use scenario_set_new_address::NewAddressItem;

impl ScenarioWorld {
    fn empty_builder(&mut self) -> SetStateBuilder<'_, ()> {
        SetStateBuilder {
            base: Some(SetStateBuilderBase::new(self)),
            item: (),
        }
    }

    pub fn account<A>(&mut self, address_expr: A) -> SetStateBuilder<'_, AccountItem>
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        self.empty_builder().account(address_expr)
    }

    pub fn new_address<CA, CN, NA>(
        &mut self,
        creator_address_expr: CA,
        creator_nonce_expr: CN,
        new_address_expr: NA,
    ) -> SetStateBuilder<'_, NewAddressItem>
    where
        CA: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        CN: AnnotatedValue<ScenarioTxEnvData, u64>,
        NA: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        self.empty_builder()
            .new_address(creator_address_expr, creator_nonce_expr, new_address_expr)
    }

    pub fn create_account_raw<A, V>(
        &mut self,
        address: A,
        egld_balance: V,
    ) -> SetStateBuilder<'_, AccountItem>
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        V: AnnotatedValue<ScenarioTxEnvData, BigUint<StaticApi>>,
    {
        self.empty_builder().account(address).balance(egld_balance)
    }

    pub fn set_egld_balance<A, V>(&mut self, address: A, balance: V)
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        V: AnnotatedValue<ScenarioTxEnvData, BigUint<StaticApi>>,
    {
        let env = self.new_env_data();
        let address_value = address_annotated(&env, &address);
        let balance_value = big_uint_annotated(&env, &balance);
        let accounts = &mut self.get_mut_state().accounts;
        for (vm_address_key, account) in accounts.iter_mut() {
            if vm_address_key == &address_value.to_address() {
                account.egld_balance.clone_from(&balance_value.value);
            }
        }
    }

    pub fn set_esdt_balance<A, V>(&mut self, address: A, token_id: &[u8], balance: V)
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        V: AnnotatedValue<ScenarioTxEnvData, BigUint<StaticApi>>,
    {
        let env = self.new_env_data();
        let address_value = address_annotated(&env, &address);
        let balance_value = big_uint_annotated(&env, &balance);
        let accounts = &mut self.get_mut_state().accounts;
        for (address, account) in accounts.iter_mut() {
            if address == &address_value.to_address() {
                account.esdt.set_esdt_balance(
                    token_id.to_vec(),
                    0,
                    &balance_value.value,
                    EsdtInstanceMetadata::default(),
                )
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn set_nft_balance_all_properties<A, B, N, T: TopEncode, R, C>(
        &mut self,
        address: A,
        token_id: &[u8],
        nonce: N,
        balance: B,
        attributes: T,
        royalties: R,
        creator: Option<C>,
        name: Option<&[u8]>,
        hash: Option<&[u8]>,
        uris: &[Vec<u8>],
    ) where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        B: AnnotatedValue<ScenarioTxEnvData, BigUint<StaticApi>>,
        N: AnnotatedValue<ScenarioTxEnvData, u64>,
        R: AnnotatedValue<ScenarioTxEnvData, u64>,
        C: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        let env = self.new_env_data();
        let address_value = address_annotated(&env, &address);
        let balance_value = big_uint_annotated(&env, &balance);
        let nonce_value = u64_annotated(&env, &nonce);
        let royalties_value = u64_annotated(&env, &royalties);

        let mut esdt_attributes = Vec::new();
        let _ = attributes.top_encode(&mut esdt_attributes);
        let accounts = &mut self.get_mut_state().accounts;
        for (account_address, account) in accounts.iter_mut() {
            if account_address == &address_value.to_address() {
                account.esdt.set_esdt_balance(
                    token_id.to_vec(),
                    nonce_value.value,
                    &balance_value.value,
                    EsdtInstanceMetadata {
                        creator: creator
                            .as_ref()
                            .map(|c| address_annotated(&env, c).to_address()),
                        attributes: esdt_attributes.clone(),
                        royalties: royalties_value.value,
                        name: name.unwrap_or_default().to_vec(),
                        hash: hash.map(|h| h.to_vec()),
                        uri: uris.to_vec(),
                    },
                )
            }
        }
    }

    pub fn set_developer_rewards<A: Copy, V: Copy>(&mut self, address: A, developer_rewards: V)
    where
        AddressKey: From<A>,
        BigUintValue: From<V>,
    {
        let accounts = &mut self.get_mut_state().accounts;
        for (account_address, account) in accounts.iter_mut() {
            if account_address == &AddressKey::from(address).to_address() {
                account
                    .developer_rewards
                    .clone_from(&BigUintValue::from(developer_rewards).value);
            }
        }
    }

    pub fn set_esdt_local_roles<A>(&mut self, address: A, token_id: &[u8], roles: &[EsdtLocalRole])
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        let env = self.new_env_data();
        let address_value = address_annotated(&env, &address);
        let accounts = &mut self.get_mut_state().accounts;
        for (account_address, account) in accounts.iter_mut() {
            if account_address == &address_value.to_address() {
                account.esdt.set_roles(
                    token_id.to_vec(),
                    roles
                        .iter()
                        .map(|role| role.as_role_name().to_vec())
                        .collect(),
                );
            }
        }
    }

    pub fn current_block(&mut self) -> SetStateBuilder<'_, BlockItem> {
        self.empty_builder().current_block()
    }

    pub fn previous_block(&mut self) -> SetStateBuilder<'_, BlockItem> {
        self.empty_builder().previous_block()
    }
}

pub trait SetStateBuilderItem {
    fn commit_to_step(&mut self, step: &mut SetStateStep);
}

impl SetStateBuilderItem for () {
    fn commit_to_step(&mut self, _step: &mut SetStateStep) {}
}

struct SetStateBuilderBase<'w> {
    world: &'w mut ScenarioWorld,
    set_state_step: SetStateStep,
}

pub struct SetStateBuilder<'w, Current>
where
    Current: SetStateBuilderItem,
{
    base: Option<SetStateBuilderBase<'w>>,
    item: Current,
}

impl<'w> SetStateBuilderBase<'w> {
    fn new(world: &'w mut ScenarioWorld) -> Self {
        SetStateBuilderBase {
            world,
            set_state_step: SetStateStep::new(),
        }
    }

    fn start_account(&self, address: AddressKey) -> AccountItem {
        assert!(
            !self
                .world
                .get_debugger_backend()
                .vm_runner
                .blockchain_mock
                .state
                .account_exists(&address.to_address()),
            "updating existing accounts currently not supported"
        );

        AccountItem::new(address)
    }
}

impl SetStateBuilder<'_, ()> {}

impl<'w, Item> SetStateBuilder<'w, Item>
where
    Item: SetStateBuilderItem,
{
    fn new_env_data(&self) -> ScenarioTxEnvData {
        self.base.as_ref().unwrap().world.new_env_data()
    }

    /// Starts building of a new account.
    pub fn account<A>(mut self, address_expr: A) -> SetStateBuilder<'w, AccountItem>
    where
        A: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        let mut base = core::mem::take(&mut self.base).unwrap();
        let env = base.world.new_env_data();
        let address_value = address_annotated(&env, &address_expr);
        self.item.commit_to_step(&mut base.set_state_step);
        let item = base.start_account(address_value.into());
        SetStateBuilder {
            base: Some(base),
            item,
        }
    }

    pub fn new_address<CA, CN, NA>(
        mut self,
        creator_address_expr: CA,
        creator_nonce_expr: CN,
        new_address_expr: NA,
    ) -> SetStateBuilder<'w, NewAddressItem>
    where
        CA: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
        CN: AnnotatedValue<ScenarioTxEnvData, u64>,
        NA: AnnotatedValue<ScenarioTxEnvData, ManagedAddress<StaticApi>>,
    {
        let mut base = core::mem::take(&mut self.base).unwrap();
        self.item.commit_to_step(&mut base.set_state_step);
        let env = base.world.new_env_data();
        SetStateBuilder {
            base: Some(base),
            item: NewAddressItem {
                new_address: NewAddress {
                    creator_address: address_annotated(&env, &creator_address_expr),
                    creator_nonce: u64_annotated(&env, &creator_nonce_expr),
                    new_address: address_annotated(&env, &new_address_expr),
                },
            },
        }
    }

    pub fn current_block(&mut self) -> SetStateBuilder<'w, BlockItem> {
        let mut base = core::mem::take(&mut self.base).unwrap();
        self.item.commit_to_step(&mut base.set_state_step);
        SetStateBuilder {
            base: Some(base),
            item: BlockItem::new_current(),
        }
    }

    pub fn previous_block(&mut self) -> SetStateBuilder<'w, BlockItem> {
        let mut base = core::mem::take(&mut self.base).unwrap();
        self.item.commit_to_step(&mut base.set_state_step);
        SetStateBuilder {
            base: Some(base),
            item: BlockItem::new_prev(),
        }
    }

    /// Forces value drop and commit accounts.
    pub fn commit(self) {}
}

impl<Current> Drop for SetStateBuilder<'_, Current>
where
    Current: SetStateBuilderItem,
{
    fn drop(&mut self) {
        if let Some(base) = &mut self.base {
            self.item.commit_to_step(&mut base.set_state_step);
            base.world.run_set_state_step(&base.set_state_step);
        }
    }
}