multiversx_sc_scenario/scenario/model/transaction/
tx_call.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use multiversx_chain_vm::types::{top_encode_big_uint, top_encode_u64};
use multiversx_sc::api::{
    ESDT_MULTI_TRANSFER_FUNC_NAME, ESDT_NFT_TRANSFER_FUNC_NAME, ESDT_TRANSFER_FUNC_NAME,
};

use crate::{
    api::StaticApi,
    multiversx_sc::types::{ContractCall, EsdtTokenPayment},
    scenario::model::{AddressValue, BigUintValue, BytesValue, U64Value},
    scenario_format::{
        interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
        serde_raw::TxCallRaw,
    },
};

use super::{tx_interpret_util::interpret_egld_value, TxESDT};

pub const DEFAULT_GAS_EXPR: &str = "5,000,000";

#[derive(Debug, Clone)]
pub struct TxCall {
    pub from: AddressValue,
    pub to: AddressValue,
    pub egld_value: BigUintValue,
    pub esdt_value: Vec<TxESDT>,
    pub function: String,
    pub arguments: Vec<BytesValue>,
    pub gas_limit: U64Value,
    pub gas_price: U64Value,
}

impl Default for TxCall {
    fn default() -> Self {
        Self {
            from: Default::default(),
            to: Default::default(),
            egld_value: Default::default(),
            esdt_value: Default::default(),
            function: Default::default(),
            arguments: Default::default(),
            gas_limit: U64Value::from(DEFAULT_GAS_EXPR),
            gas_price: Default::default(),
        }
    }
}

impl InterpretableFrom<TxCallRaw> for TxCall {
    fn interpret_from(from: TxCallRaw, context: &InterpreterContext) -> Self {
        TxCall {
            from: AddressValue::interpret_from(from.from, context),
            to: AddressValue::interpret_from(from.to, context),
            egld_value: interpret_egld_value(from.value, from.egld_value, context),
            esdt_value: from
                .esdt_value
                .into_iter()
                .map(|esdt_value| TxESDT::interpret_from(esdt_value, context))
                .collect(),
            function: from.function,
            arguments: from
                .arguments
                .into_iter()
                .map(|t| BytesValue::interpret_from(t, context))
                .collect(),
            gas_limit: U64Value::interpret_from(from.gas_limit, context),
            gas_price: U64Value::interpret_from(from.gas_price.unwrap_or_default(), context),
        }
    }
}

impl IntoRaw<TxCallRaw> for TxCall {
    fn into_raw(self) -> TxCallRaw {
        TxCallRaw {
            from: self.from.into_raw(),
            to: self.to.into_raw(),
            value: None,
            egld_value: self.egld_value.into_raw_opt(),
            esdt_value: self
                .esdt_value
                .into_iter()
                .map(|esdt_value| esdt_value.into_raw())
                .collect(),
            function: self.function,
            arguments: self
                .arguments
                .into_iter()
                .map(|arg| arg.into_raw())
                .collect(),
            gas_limit: self.gas_limit.into_raw(),
            gas_price: self.gas_price.into_raw_opt(),
        }
    }
}

impl TxCall {
    #[deprecated(
        since = "0.49.0",
        note = "Please use the unified transaction syntax instead."
    )]
    #[allow(deprecated)]
    pub fn to_contract_call(&self) -> multiversx_sc::types::ContractCallWithEgld<StaticApi, ()> {
        let mut contract_call = multiversx_sc::types::ContractCallWithEgld::new(
            (&self.to.value).into(),
            self.function.as_bytes(),
            (&self.egld_value.value).into(),
        );

        contract_call.basic.explicit_gas_limit = self.gas_limit.value;

        contract_call = contract_call.convert_to_esdt_transfer_call(
            self.esdt_value
                .iter()
                .map(|esdt| {
                    EsdtTokenPayment::new(
                        esdt.esdt_token_identifier.value.as_slice().into(),
                        esdt.nonce.value,
                        (&esdt.esdt_value.value).into(),
                    )
                })
                .collect(),
        );

        // For some contract calls from == to.
        // The contract call objects have no "from" field, since that is always part of the execution context.
        // On the static API there is no execution context, but a placeholder value is provided.
        // Here we already know the sender, so we can replace the placeholder with the actual value.
        if StaticApi::is_current_address_placeholder(&contract_call.basic.to.to_address()) {
            contract_call.basic.to = self.from.value.clone().into();
        }

        for argument in &self.arguments {
            contract_call.push_raw_argument(argument.value.as_slice());
        }
        contract_call
    }

    /// Converts call to builtin function ESDT transfer call, if necessary.
    pub fn normalize(&self) -> TxCall {
        let (function, arguments, to_self) = self.process_payments();
        TxCall {
            from: self.from.clone(),
            to: if to_self {
                self.from.clone()
            } else {
                self.to.clone()
            },
            egld_value: self.egld_value.clone(),
            esdt_value: Vec::new(),
            function,
            arguments,
            gas_limit: self.gas_limit.clone(),
            gas_price: self.gas_price.clone(),
        }
    }

    fn process_payments(&self) -> (String, Vec<BytesValue>, bool) {
        assert!(
            self.egld_value.is_zero() || self.esdt_value.is_empty(),
            "Cannot have both EGLD and ESDT fields filled. To transfer EGLD and ESDT in the same transaction, represent EGLD as EGLD-000000 in the ESDTs.");

        match self.esdt_value.len() {
            0 => (self.function.clone(), self.arguments.clone(), false),
            1 => {
                let payment = self.esdt_value.first().unwrap();
                if payment.is_egld() {
                    self.construct_multi_transfer_esdt_call()
                } else if payment.nonce.value == 0 {
                    self.construct_single_transfer_fungible_call(payment)
                } else {
                    self.construct_single_transfer_nft_call(payment)
                }
            },
            _ => self.construct_multi_transfer_esdt_call(),
        }
    }

    fn append_function_call_to_arguments(&self, arguments: &mut Vec<BytesValue>) {
        if !self.function.is_empty() {
            arguments.push(BytesValue::from_str_expr(&self.function));
        }
        for regular_arg in &self.arguments {
            arguments.push(regular_arg.clone())
        }
    }

    /// Constructs `ESDTTransfer` builtin function call.
    pub(crate) fn construct_single_transfer_fungible_call(
        &self,
        payment: &TxESDT,
    ) -> (String, Vec<BytesValue>, bool) {
        let mut arguments = Vec::new();
        arguments.push(payment.esdt_token_identifier.value.as_slice().into());
        arguments.push(top_encode_big_uint(&payment.esdt_value.value).into());
        self.append_function_call_to_arguments(&mut arguments);

        (ESDT_TRANSFER_FUNC_NAME.to_owned(), arguments, false)
    }

    /// Constructs `ESDTNFTTransfer` builtin function call.
    ///
    /// `ESDTNFTTransfer` takes 4 arguments:
    /// arg0 - token identifier
    /// arg1 - nonce
    /// arg2 - quantity to transfer
    /// arg3 - destination address
    pub(crate) fn construct_single_transfer_nft_call(
        &self,
        payment: &TxESDT,
    ) -> (String, Vec<BytesValue>, bool) {
        let mut arguments = vec![
            payment.esdt_token_identifier.value.as_slice().into(),
            top_encode_u64(payment.nonce.value).into(),
            top_encode_big_uint(&payment.esdt_value.value).into(),
            self.to.value.as_bytes().into(), // TODO: preserve representation
        ];

        self.append_function_call_to_arguments(&mut arguments);

        (ESDT_NFT_TRANSFER_FUNC_NAME.to_owned(), arguments, true)
    }

    /// Constructs `MultiESDTNFTTransfer` builtin function call.
    pub(crate) fn construct_multi_transfer_esdt_call(&self) -> (String, Vec<BytesValue>, bool) {
        let mut arguments = Vec::new();
        arguments.push(self.to.value.as_bytes().into()); // TODO: preserve representation
        arguments.push(top_encode_u64(self.esdt_value.len() as u64).into());

        for payment in &self.esdt_value {
            arguments.push(payment.esdt_token_identifier.value.as_slice().into());
            arguments.push(top_encode_u64(payment.nonce.value).into());
            arguments.push(top_encode_big_uint(&payment.esdt_value.value).into());
        }

        self.append_function_call_to_arguments(&mut arguments);

        (ESDT_MULTI_TRANSFER_FUNC_NAME.to_owned(), arguments, true)
    }

    /// Creates the data field of the transaction represented by object.
    pub fn compute_data_field(&self) -> String {
        let mut result = self.function.clone();
        for argument in &self.arguments {
            result.push('@');
            result.push_str(hex::encode(argument.value.as_slice()).as_str());
        }
        result
    }
}