multiversx_chain_vm/host/context/
tx_back_transfers.rs

1use num_bigint::BigUint;
2
3use crate::{builtin_functions::BuiltinFunctionContainer, types::VMAddress};
4
5use super::{async_call_tx_input, CallType, TxResult, TxTokenTransfer};
6
7#[derive(Default, Debug)]
8pub struct BackTransfers {
9    pub call_value: BigUint,
10    pub esdt_transfers: Vec<TxTokenTransfer>,
11}
12
13impl BackTransfers {
14    pub fn empty() -> Self {
15        BackTransfers::default()
16    }
17
18    pub fn new_from_result(
19        &mut self,
20        own_address: &VMAddress,
21        result: &TxResult,
22        builtin_functions: &BuiltinFunctionContainer,
23    ) {
24        let mut bt = BackTransfers::default();
25
26        for call in &result.all_calls {
27            // TODO: refactor, check type
28
29            if call.endpoint_name.is_empty() {
30                bt.call_value += &call.call_value;
31                continue;
32            }
33
34            let tx_input = async_call_tx_input(call, CallType::BackTransfer);
35            let mut token_transfers = builtin_functions.extract_token_transfers(&tx_input);
36            if &token_transfers.real_recipient == own_address {
37                bt.esdt_transfers.append(&mut token_transfers.transfers);
38            }
39        }
40
41        self.call_value = bt.call_value;
42        self.esdt_transfers = bt.esdt_transfers;
43    }
44}