multiversx_sc_modules/
transfer_role_proxy.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
use multiversx_sc::codec::TopEncodeMulti;

multiversx_sc::imports!();

const CALLBACK_RESERVED_GAS_PER_TOKEN: u64 = 1_000_000;
static ERR_CALLBACK_MSG: &[u8] = b"Error received in callback:";

pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;

#[multiversx_sc::module]
pub trait TransferRoleProxyModule {
    fn transfer_to_user(
        &self,
        original_caller: ManagedAddress,
        dest: ManagedAddress,
        payments: &PaymentsVec<Self::Api>,
        data: ManagedBuffer,
    ) -> ! {
        let transaction = self.tx().to(&dest).raw_call(data).payment(payments);

        self.execute_async_call(original_caller, payments, transaction, None)
    }

    fn transfer_to_contract_typed_call<T>(
        &self,
        original_caller: ManagedAddress,
        transaction: Tx<
            TxScEnv<Self::Api>,
            (),
            &ManagedAddress,
            &ManagedVec<Self::Api, EsdtTokenPayment<Self::Api>>,
            (),
            FunctionCall<Self::Api>,
            (),
        >,
        opt_custom_callback: Option<CallbackClosure<Self::Api>>,
    ) -> !
    where
        T: TopEncodeMulti,
    {
        self.execute_async_call(
            original_caller,
            transaction.payment,
            transaction,
            opt_custom_callback,
        );
    }

    fn transfer_to_contract_raw(
        &self,
        original_caller: ManagedAddress,
        dest: ManagedAddress,
        payments: &PaymentsVec<Self::Api>,
        endpoint_name: ManagedBuffer,
        args: ManagedArgBuffer<Self::Api>,
        opt_custom_callback: Option<CallbackClosure<Self::Api>>,
    ) -> ! {
        let transaction = self
            .tx()
            .to(&dest)
            .raw_call(endpoint_name)
            .payment(payments)
            .arguments_raw(args);

        self.execute_async_call(original_caller, payments, transaction, opt_custom_callback)
    }

    fn execute_async_call(
        &self,
        original_caller: ManagedAddress,
        initial_payments: &PaymentsVec<Self::Api>,
        transaction: Tx<
            TxScEnv<Self::Api>,
            (),
            &ManagedAddress,
            &ManagedVec<Self::Api, EsdtTokenPayment<Self::Api>>,
            (),
            FunctionCall<Self::Api>,
            (),
        >,
        opt_custom_callback: Option<CallbackClosure<Self::Api>>,
    ) -> ! {
        require!(
            self.destination_whitelist().contains(transaction.to),
            "Destination address not whitelisted"
        );

        let remaining_gas = self.blockchain().get_gas_left();
        let cb_gas_needed = CALLBACK_RESERVED_GAS_PER_TOKEN * transaction.payment.len() as u64;
        require!(
            remaining_gas > cb_gas_needed,
            "Not enough gas to launch async call"
        );

        let cb = match opt_custom_callback {
            Some(custom_cb) => custom_cb,
            None => TransferRoleProxyModule::callbacks(self)
                .transfer_callback(original_caller, initial_payments.clone()),
        };

        transaction.callback(cb).async_call_and_exit()
    }

    #[callback]
    fn transfer_callback(
        &self,
        original_caller: ManagedAddress,
        initial_payments: ManagedVec<EsdtTokenPayment<Self::Api>>,
        #[call_result] result: ManagedAsyncCallResult<MultiValueEncoded<ManagedBuffer>>,
    ) -> MultiValueEncoded<ManagedBuffer> {
        match result {
            ManagedAsyncCallResult::Ok(return_values) => return_values,
            ManagedAsyncCallResult::Err(err) => {
                if !initial_payments.is_empty() {
                    self.tx()
                        .to(&original_caller)
                        .payment(initial_payments)
                        .transfer();
                }

                let mut err_result = MultiValueEncoded::new();
                err_result.push(ManagedBuffer::new_from_bytes(ERR_CALLBACK_MSG));
                err_result.push(err.err_msg.clone());

                sc_print!("{}", err.err_msg);

                err_result
            },
        }
    }

    #[storage_mapper("transfer_role_proxy:destination_whitelist")]
    fn destination_whitelist(&self) -> UnorderedSetMapper<ManagedAddress>;
}