multiversx_chain_vm/blockchain/state/
account_data.rs

1use num_bigint::BigUint;
2use num_traits::Zero;
3
4use super::AccountEsdt;
5use crate::{
6    display_util::key_hex,
7    types::{VMAddress, VMCodeMetadata},
8};
9use std::{collections::HashMap, fmt, fmt::Write};
10
11pub type AccountStorage = HashMap<Vec<u8>, Vec<u8>>;
12
13#[derive(Clone, Debug)]
14pub struct AccountData {
15    pub address: VMAddress,
16    pub nonce: u64,
17    pub egld_balance: BigUint,
18    pub esdt: AccountEsdt,
19    pub storage: AccountStorage,
20    pub username: Vec<u8>,
21    pub contract_path: Option<Vec<u8>>,
22    pub code_metadata: VMCodeMetadata,
23    pub contract_owner: Option<VMAddress>,
24    pub developer_rewards: BigUint,
25}
26
27impl AccountData {
28    pub fn new_empty(address: VMAddress) -> Self {
29        AccountData {
30            address,
31            nonce: 0,
32            egld_balance: BigUint::zero(),
33            esdt: AccountEsdt::default(),
34            storage: AccountStorage::default(),
35            username: vec![],
36            contract_path: None,
37            code_metadata: VMCodeMetadata::empty(),
38            contract_owner: None,
39            developer_rewards: BigUint::zero(),
40        }
41    }
42}
43
44impl fmt::Display for AccountData {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        let mut storage_buf = String::new();
47        let mut storage_keys: Vec<Vec<u8>> = self.storage.keys().cloned().collect();
48        storage_keys.sort();
49
50        for key in &storage_keys {
51            let value = self.storage.get(key).unwrap();
52            write!(
53                storage_buf,
54                "\n\t\t\t{} -> 0x{}",
55                key_hex(key.as_slice()),
56                hex::encode(value.as_slice())
57            )
58            .unwrap();
59        }
60
61        write!(
62            f,
63            "AccountData {{
64        nonce: {},
65        balance: {},
66        esdt: [{} ],
67        username: {},
68        storage: [{} ],
69        developerRewards: {},
70    }}",
71            self.nonce,
72            self.egld_balance,
73            self.esdt,
74            String::from_utf8(self.username.clone()).unwrap(),
75            storage_buf,
76            self.developer_rewards
77        )
78    }
79}