multiversx_sc_scenario/scenario/model/value/
address_value.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use multiversx_sc::{
    api::ManagedTypeApi,
    types::{AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified},
};
use std::fmt;

use crate::multiversx_sc::types::{Address, TestAddress, TestSCAddress};

use crate::{
    facade::expr::Bech32Address,
    scenario_format::{
        interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
        serde_raw::ValueSubTree,
        value_interpreter::{interpret_string, interpret_subtree},
    },
};

use super::AddressKey;

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct AddressValue {
    pub value: Address,
    pub original: ValueSubTree,
}

impl Default for AddressValue {
    fn default() -> Self {
        Self {
            value: Address::zero(),
            original: Default::default(),
        }
    }
}

impl AddressValue {
    pub fn to_address(&self) -> Address {
        self.value.clone()
    }
}

impl fmt::Display for AddressValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.original.fmt(f)
    }
}

pub(crate) fn value_from_slice(slice: &[u8]) -> Address {
    let mut value = [0u8; 32];
    if slice.len() == 32 {
        value.copy_from_slice(slice);
    } else {
        panic!("account address is not 32 bytes in length");
    }
    value.into()
}

impl InterpretableFrom<ValueSubTree> for AddressValue {
    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
        let bytes = interpret_subtree(&from, context);
        AddressValue {
            value: value_from_slice(bytes.as_slice()),
            original: from,
        }
    }
}

impl InterpretableFrom<&str> for AddressValue {
    fn interpret_from(from: &str, context: &InterpreterContext) -> Self {
        let bytes = interpret_string(from, context);
        AddressValue {
            value: value_from_slice(bytes.as_slice()),
            original: ValueSubTree::Str(from.to_string()),
        }
    }
}

impl IntoRaw<ValueSubTree> for AddressValue {
    fn into_raw(self) -> ValueSubTree {
        self.original
    }
}

impl From<&AddressValue> for AddressValue {
    fn from(from: &AddressValue) -> Self {
        from.clone()
    }
}

impl From<&AddressKey> for AddressValue {
    fn from(from: &AddressKey) -> Self {
        AddressValue {
            value: from.to_address(),
            original: ValueSubTree::Str(from.original.clone()),
        }
    }
}

impl From<AddressKey> for AddressValue {
    fn from(from: AddressKey) -> Self {
        AddressValue::from(&from)
    }
}

impl From<&Address> for AddressValue {
    fn from(from: &Address) -> Self {
        AddressValue {
            value: from.clone(),
            original: ValueSubTree::Str(format!("0x{}", hex::encode(from))),
        }
    }
}

impl From<&Bech32Address> for AddressValue {
    fn from(from: &Bech32Address) -> Self {
        AddressValue {
            value: from.to_address().clone(),
            original: ValueSubTree::Str(from.to_bech32_expr()),
        }
    }
}

impl From<Bech32Address> for AddressValue {
    fn from(from: Bech32Address) -> Self {
        AddressValue {
            original: ValueSubTree::Str(from.to_bech32_expr()),
            value: from.into_address(),
        }
    }
}

impl From<&str> for AddressValue {
    fn from(from: &str) -> Self {
        AddressValue::interpret_from(from, &InterpreterContext::default())
    }
}

impl From<TestAddress<'_>> for AddressValue {
    fn from(from: TestAddress) -> Self {
        AddressValue {
            value: from.eval_to_array().into(),
            original: ValueSubTree::Str(from.eval_to_expr()),
        }
    }
}

impl From<TestSCAddress<'_>> for AddressValue {
    fn from(from: TestSCAddress) -> Self {
        AddressValue {
            value: from.eval_to_array().into(),
            original: ValueSubTree::Str(from.eval_to_expr()),
        }
    }
}

impl<M> From<&AddressValue> for ManagedAddress<M>
where
    M: ManagedTypeApi,
{
    #[inline]
    fn from(address_value: &AddressValue) -> Self {
        ManagedAddress::from_address(&address_value.value)
    }
}

impl<Env> TxFrom<Env> for AddressValue
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        self.into()
    }
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for AddressValue
where
    Env: TxEnv,
{
    fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer<Env::Api> {
        ManagedBuffer::from(self.original.to_string())
    }

    fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(&self.value)
    }

    fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(&self.value)
    }

    fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
    where
        F: FnOnce(&ManagedAddress<Env::Api>) -> R,
    {
        f(&ManagedAddress::from_address(&self.value))
    }
}

impl<Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &AddressValue
where
    Env: TxEnv,
{
    fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer<Env::Api> {
        ManagedBuffer::from(self.original.to_string())
    }

    fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(&self.value)
    }

    fn into_value(self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(&self.value)
    }

    fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
    where
        F: FnOnce(&ManagedAddress<Env::Api>) -> R,
    {
        f(&ManagedAddress::from_address(&self.value))
    }
}

impl<Env> TxFromSpecified<Env> for AddressValue where Env: TxEnv {}

impl<Env> TxFrom<Env> for &AddressValue
where
    Env: TxEnv,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        ManagedAddress::from_address(&self.value)
    }
}

impl<Env> TxFromSpecified<Env> for &AddressValue where Env: TxEnv {}