multiversx_chain_vm/world_mock/
account_data.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
use num_bigint::BigUint;
use num_traits::Zero;

use super::AccountEsdt;
use crate::{
    display_util::key_hex,
    types::{VMAddress, VMCodeMetadata},
};
use std::{collections::HashMap, fmt, fmt::Write};

pub type AccountStorage = HashMap<Vec<u8>, Vec<u8>>;

#[derive(Clone, Debug)]
pub struct AccountData {
    pub address: VMAddress,
    pub nonce: u64,
    pub egld_balance: BigUint,
    pub esdt: AccountEsdt,
    pub storage: AccountStorage,
    pub username: Vec<u8>,
    pub contract_path: Option<Vec<u8>>,
    pub code_metadata: VMCodeMetadata,
    pub contract_owner: Option<VMAddress>,
    pub developer_rewards: BigUint,
}

impl AccountData {
    pub fn new_empty(address: VMAddress) -> Self {
        AccountData {
            address,
            nonce: 0,
            egld_balance: BigUint::zero(),
            esdt: AccountEsdt::default(),
            storage: AccountStorage::default(),
            username: vec![],
            contract_path: None,
            code_metadata: VMCodeMetadata::empty(),
            contract_owner: None,
            developer_rewards: BigUint::zero(),
        }
    }
}

impl fmt::Display for AccountData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut storage_buf = String::new();
        let mut storage_keys: Vec<Vec<u8>> = self.storage.keys().cloned().collect();
        storage_keys.sort();

        for key in &storage_keys {
            let value = self.storage.get(key).unwrap();
            write!(
                storage_buf,
                "\n\t\t\t{} -> 0x{}",
                key_hex(key.as_slice()),
                hex::encode(value.as_slice())
            )
            .unwrap();
        }

        write!(
            f,
            "AccountData {{
        nonce: {},
        balance: {},
        esdt: [{} ],
        username: {},
        storage: [{} ],
        developerRewards: {},
    }}",
            self.nonce,
            self.egld_balance,
            self.esdt,
            String::from_utf8(self.username.clone()).unwrap(),
            storage_buf,
            self.developer_rewards
        )
    }
}