multiversx_sc_scenario/scenario/model/esdt_data/
esdt_instance.rs1use crate::{
2 scenario::model::{BigUintValue, BytesValue, U64Value},
3 scenario_format::{
4 interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
5 serde_raw::EsdtInstanceRaw,
6 },
7 scenario_model::AddressValue,
8};
9
10#[derive(Debug, Default, Clone)]
11pub struct EsdtInstance {
12 pub nonce: Option<U64Value>,
13 pub balance: Option<BigUintValue>,
14 pub creator: Option<AddressValue>,
15 pub royalties: Option<U64Value>,
16 pub hash: Option<BytesValue>,
17 pub uri: Vec<BytesValue>,
18 pub attributes: Option<BytesValue>,
19}
20
21impl EsdtInstance {
22 pub fn is_simple_fungible(&self) -> bool {
23 let is_fungible = if let Some(nonce) = &self.nonce {
24 nonce.value == 0
25 } else {
26 true
27 };
28
29 is_fungible
30 && self.creator.is_none()
31 && self.royalties.is_none()
32 && self.hash.is_none()
33 && self.uri.is_empty()
34 && self.attributes.is_none()
35 }
36}
37
38impl InterpretableFrom<EsdtInstanceRaw> for EsdtInstance {
39 fn interpret_from(from: EsdtInstanceRaw, context: &InterpreterContext) -> Self {
40 EsdtInstance {
41 nonce: from.nonce.map(|n| U64Value::interpret_from(n, context)),
42 balance: from
43 .balance
44 .map(|b| BigUintValue::interpret_from(b, context)),
45 creator: from
46 .creator
47 .map(|b| AddressValue::interpret_from(b, context)),
48 royalties: from.royalties.map(|b| U64Value::interpret_from(b, context)),
49 hash: from.hash.map(|b| BytesValue::interpret_from(b, context)),
50 uri: from
51 .uri
52 .into_iter()
53 .map(|b| BytesValue::interpret_from(b, context))
54 .collect(),
55 attributes: from
56 .attributes
57 .map(|b| BytesValue::interpret_from(b, context)),
58 }
59 }
60}
61
62impl IntoRaw<EsdtInstanceRaw> for EsdtInstance {
63 fn into_raw(self) -> EsdtInstanceRaw {
64 EsdtInstanceRaw {
65 nonce: self.nonce.map(|n| n.original),
66 balance: self.balance.map(|n| n.original),
67 creator: self.creator.map(|n| n.original),
68 royalties: self.royalties.map(|n| n.original),
69 hash: self.hash.map(|n| n.original),
70 uri: self.uri.into_iter().map(|b| b.original).collect(),
71 attributes: self.attributes.map(|n| n.original),
72 }
73 }
74}