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
use crate::{
scenario::model::{
AddressValue, BigUintValue, BytesKey, BytesValue, Esdt, EsdtObject, U64Value,
},
scenario_format::{
interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
serde_raw::AccountRaw,
},
};
use std::collections::BTreeMap;
#[derive(Debug, Default)]
pub struct Account {
pub comment: Option<String>,
pub nonce: Option<U64Value>,
pub balance: Option<BigUintValue>,
pub esdt: BTreeMap<BytesKey, Esdt>,
pub username: Option<BytesValue>,
pub storage: BTreeMap<BytesKey, BytesValue>,
pub code: Option<BytesValue>,
pub owner: Option<AddressValue>,
pub developer_rewards: Option<BigUintValue>,
}
impl Account {
pub fn new() -> Self {
Self::default()
}
pub fn nonce<V>(mut self, nonce: V) -> Self
where
U64Value: From<V>,
{
self.nonce = Some(U64Value::from(nonce));
self
}
pub fn balance<V>(mut self, balance_expr: V) -> Self
where
BigUintValue: From<V>,
{
self.balance = Some(BigUintValue::from(balance_expr));
self
}
pub fn esdt_balance<K, V>(mut self, token_id_expr: K, balance_expr: V) -> Self
where
BytesKey: From<K>,
BigUintValue: From<V>,
{
let token_id = BytesKey::from(token_id_expr);
let esdt_data_ref = self.get_esdt_data_or_create(&token_id);
esdt_data_ref.set_balance(0u64, balance_expr);
self
}
pub fn esdt_nft_balance<K, N, V, T>(
mut self,
token_id_expr: K,
nonce_expr: N,
balance_expr: V,
opt_attributes_expr: Option<T>,
) -> Self
where
N: Clone,
BytesKey: From<K>,
U64Value: From<N>,
BigUintValue: From<V>,
BytesValue: From<T>,
{
let token_id = BytesKey::from(token_id_expr);
let esdt_obj_ref = self
.get_esdt_data_or_create(&token_id)
.get_mut_esdt_object();
esdt_obj_ref.set_balance(nonce_expr.clone(), balance_expr);
if let Some(attributes_expr) = opt_attributes_expr {
esdt_obj_ref.set_token_attributes(nonce_expr, attributes_expr);
}
self
}
pub fn esdt_nft_last_nonce<K, N>(mut self, token_id_expr: K, last_nonce_expr: N) -> Self
where
BytesKey: From<K>,
U64Value: From<N>,
{
let token_id = BytesKey::from(token_id_expr);
let esdt_obj_ref = self
.get_esdt_data_or_create(&token_id)
.get_mut_esdt_object();
esdt_obj_ref.set_last_nonce(last_nonce_expr);
self
}
pub fn esdt_roles<K>(mut self, token_id_expr: K, roles: Vec<String>) -> Self
where
BytesKey: From<K>,
{
let token_id = BytesKey::from(token_id_expr);
let esdt_obj_ref = self
.get_esdt_data_or_create(&token_id)
.get_mut_esdt_object();
esdt_obj_ref.set_roles(roles);
self
}
fn get_esdt_data_or_create(&mut self, token_id: &BytesKey) -> &mut Esdt {
if !self.esdt.contains_key(token_id) {
let _ = self
.esdt
.insert(token_id.clone(), Esdt::Full(EsdtObject::default()));
}
self.esdt.get_mut(token_id).unwrap()
}
}
impl InterpretableFrom<AccountRaw> for Account {
fn interpret_from(from: AccountRaw, context: &InterpreterContext) -> Self {
Account {
comment: from.comment,
nonce: from.nonce.map(|n| U64Value::interpret_from(n, context)),
balance: from
.balance
.map(|b| BigUintValue::interpret_from(b, context)),
esdt: from
.esdt
.into_iter()
.map(|(k, v)| {
(
BytesKey::interpret_from(k, context),
Esdt::interpret_from(v, context),
)
})
.collect(),
username: from
.username
.map(|c| BytesValue::interpret_from(c, context)),
storage: from
.storage
.into_iter()
.map(|(k, v)| {
(
BytesKey::interpret_from(k, context),
BytesValue::interpret_from(v, context),
)
})
.collect(),
code: from.code.map(|c| BytesValue::interpret_from(c, context)),
owner: from.owner.map(|v| AddressValue::interpret_from(v, context)),
developer_rewards: from
.developer_rewards
.map(|b| BigUintValue::interpret_from(b, context)),
}
}
}
impl IntoRaw<AccountRaw> for Account {
fn into_raw(self) -> AccountRaw {
AccountRaw {
comment: self.comment,
nonce: self.nonce.map(|n| n.original),
balance: self.balance.map(|n| n.original),
esdt: self
.esdt
.into_iter()
.map(|(k, v)| (k.original, v.into_raw()))
.collect(),
username: self.username.map(|n| n.original),
storage: self
.storage
.into_iter()
.map(|(key, value)| (key.original, value.original))
.collect(),
code: self.code.map(|n| n.original),
owner: self.owner.map(|n| n.original),
developer_rewards: self.developer_rewards.map(|n| n.original),
}
}
}