multiversx_chain_vm/world_mock/
esdt_instances.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
use super::{EsdtInstance, EsdtInstanceMetadata};
use crate::display_util::verbose_hex_list;
use num_bigint::BigUint;
use num_traits::Zero;
use std::{
    collections::BTreeMap,
    fmt::{self, Write},
};

#[derive(Clone, Debug, Default)]
pub struct EsdtInstances(BTreeMap<u64, EsdtInstance>);

impl EsdtInstances {
    pub fn new() -> Self {
        EsdtInstances(BTreeMap::new())
    }

    pub fn new_from_hash(hash: BTreeMap<u64, EsdtInstance>) -> Self {
        EsdtInstances(hash)
    }

    pub fn add(&mut self, nonce: u64, value: BigUint) {
        if self.0.contains_key(&nonce) {
            let esdt_balance = self.0.get_mut(&nonce).unwrap();
            esdt_balance.balance += value;
        } else {
            let mut instance = EsdtInstance::default(nonce);
            instance.balance = value;
            self.push_instance(instance)
        }
    }

    pub fn push_instance(&mut self, instance: EsdtInstance) {
        self.0.insert(instance.nonce, instance);
    }

    pub fn increase_balance(
        &mut self,
        nonce: u64,
        value: &BigUint,
        metadata: EsdtInstanceMetadata,
    ) {
        let instance = self.0.entry(nonce).or_insert_with(|| EsdtInstance {
            nonce,
            balance: BigUint::zero(),
            metadata: metadata.clone(),
        });
        if instance.balance.is_zero() {
            instance.metadata = metadata;
        }

        instance.balance += value;
    }

    pub fn set_balance(&mut self, nonce: u64, value: &BigUint, metadata: EsdtInstanceMetadata) {
        let _ = self
            .0
            .entry(nonce)
            .and_modify(|instance| {
                instance.balance.clone_from(value);
                instance.nonce = nonce;
                instance.metadata.clone_from(&metadata);
            })
            .or_insert_with(|| EsdtInstance {
                nonce,
                balance: value.clone(),
                metadata,
            });
    }

    pub fn get_by_nonce(&self, nonce: u64) -> Option<&EsdtInstance> {
        self.0.get(&nonce)
    }

    pub fn get_by_nonce_or_default(&self, nonce: u64) -> EsdtInstance {
        if let Some(instance) = self.0.get(&nonce) {
            instance.clone()
        } else {
            EsdtInstance::default(nonce)
        }
    }

    pub fn get_mut_by_nonce(&mut self, nonce: u64) -> Option<&mut EsdtInstance> {
        self.0.get_mut(&nonce)
    }

    pub fn get_instances(&self) -> &BTreeMap<u64, EsdtInstance> {
        &self.0
    }

    pub fn is_empty_esdt(&self) -> bool {
        self.0.values().all(EsdtInstance::is_empty_esdt)
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl fmt::Display for EsdtInstances {
    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut instance_buf = String::new();
        for (_, value) in self.0.iter() {
            let creator_encoded = if let Some(creator) = &value.metadata.creator {
                hex::encode(creator)
            } else {
                "".to_string()
            };
            write!(
                instance_buf,
                "{{
                    nonce: {},
                    balance: {},
                    creator: {},
                    royalties: {},
                    hash: {},
                    uri: [{} ],
                    attributes: {}
                }}",
                value.nonce,
                value.balance,
                creator_encoded,
                value.metadata.royalties,
                hex::encode(
                    value
                        .metadata
                        .hash
                        .as_ref()
                        .unwrap_or(&Vec::new())
                        .as_slice()
                ),
                verbose_hex_list(value.metadata.uri.as_slice()),
                hex::encode(value.metadata.attributes.as_slice())
            )?;
        }
        Ok(())
    }
}