multiversx_sc/io/
call_value_init.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
use crate::{
    api::{
        const_handles, use_raw_handle, CallValueApi, CallValueApiImpl, ErrorApi, ErrorApiImpl,
        ManagedBufferApiImpl, ManagedTypeApi,
    },
    contract_base::CallValueWrapper,
    err_msg,
    types::{
        BigUint, EgldOrEsdtTokenIdentifier, EsdtTokenPayment, ManagedRef, ManagedType, ManagedVec,
    },
};

/// Called initially in the generated code whenever no payable annotation is provided.
pub fn not_payable<A>()
where
    A: CallValueApi,
{
    A::call_value_api_impl().check_not_payable();
}

/// Called initially in the generated code whenever `#[payable("*")]` annotation is provided.
pub fn payable_any<A>()
where
    A: CallValueApi,
{
}

/// Called initially in the generated code whenever `#[payable("EGLD")]` annotation is provided.
pub fn payable_egld<A>()
where
    A: CallValueApi + ErrorApi + ManagedTypeApi,
{
    // will crash if anything other than (single) EGLD was transferred
    let _ = CallValueWrapper::<A>::new().egld();
}

/// Called initially in the generated code whenever `#[payable("<token identifier>")]` annotation is provided.
///
/// Was never really used, expected to be deprecated/removed.
pub fn payable_single_specific_token<A>(expected_tokend_identifier: &str)
where
    A: CallValueApi + ManagedTypeApi + ErrorApi,
{
    let transfers = CallValueWrapper::<A>::new().all_esdt_transfers();
    if transfers.len() != 1 {
        A::error_api_impl().signal_error(err_msg::SINGLE_ESDT_EXPECTED.as_bytes());
    }
    let expected_token_handle: A::ManagedBufferHandle =
        use_raw_handle(const_handles::MBUF_TEMPORARY_1);
    A::managed_type_impl().mb_overwrite(
        expected_token_handle.clone(),
        expected_tokend_identifier.as_bytes(),
    );
    let transfer = transfers.get(0);
    if !A::managed_type_impl().mb_eq(
        transfer.token_identifier.get_handle(),
        expected_token_handle,
    ) {
        A::error_api_impl().signal_error(err_msg::BAD_TOKEN_PROVIDED.as_bytes());
    }
}

/// Initializes an argument annotated with `#[payment_amount]` or `#[payment]`.
pub fn arg_payment_amount<A>() -> BigUint<A>
where
    A: CallValueApi + ManagedTypeApi,
{
    CallValueWrapper::<A>::new().egld_or_single_esdt().amount
}

/// Initializes an argument annotated with `#[payment_token]`.
pub fn arg_payment_token<A>() -> EgldOrEsdtTokenIdentifier<A>
where
    A: CallValueApi + ManagedTypeApi,
{
    CallValueWrapper::<A>::new()
        .egld_or_single_esdt()
        .token_identifier
}

/// Initializes an argument annotated with `#[payment_nonce]`.
pub fn arg_payment_nonce<A>() -> u64
where
    A: CallValueApi + ManagedTypeApi,
{
    CallValueWrapper::<A>::new()
        .egld_or_single_esdt()
        .token_nonce
}

/// Initializes an argument annotated with `#[payment_multi]`.
pub fn arg_payment_multi<A>() -> ManagedRef<'static, A, ManagedVec<A, EsdtTokenPayment<A>>>
where
    A: CallValueApi + ManagedTypeApi,
{
    CallValueWrapper::<A>::new().all_esdt_transfers()
}