-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcall_value_init.rs
More file actions
101 lines (93 loc) · 3.02 KB
/
call_value_init.rs
File metadata and controls
101 lines (93 loc) · 3.02 KB
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
use crate::{
api::{
const_handles, use_raw_handle, CallValueApi, CallValueApiImpl, ErrorApi, ErrorApiImpl,
ManagedBufferApiImpl, ManagedTypeApi,
},
err_msg,
imports::ContractBase,
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,
{
if A::call_value_api_impl().esdt_num_transfers() > 0 {
A::error_api_impl().signal_error(err_msg::NON_PAYABLE_FUNC_ESDT.as_bytes());
}
}
/// 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, O>(obj: &O, expected_tokend_identifier: &str)
where
A: CallValueApi + ManagedTypeApi + ErrorApi,
O: ContractBase<Api = A>,
{
let transfers = obj.call_value().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, O>(obj: &O) -> BigUint<A>
where
A: CallValueApi + ManagedTypeApi,
O: ContractBase<Api = A>,
{
obj.call_value().egld_or_single_esdt().amount
}
/// Initializes an argument annotated with `#[payment_token]`.
pub fn arg_payment_token<A, O>(obj: &O) -> EgldOrEsdtTokenIdentifier<A>
where
A: CallValueApi + ManagedTypeApi,
O: ContractBase<Api = A>,
{
obj.call_value().egld_or_single_esdt().token_identifier
}
/// Initializes an argument annotated with `#[payment_nonce]`.
pub fn arg_payment_nonce<A, O>(obj: &O) -> u64
where
A: CallValueApi + ManagedTypeApi,
O: ContractBase<Api = A>,
{
obj.call_value().egld_or_single_esdt().token_nonce
}
/// Initializes an argument annotated with `#[payment_multi]`.
pub fn arg_payment_multi<A, O>(
obj: &O,
) -> ManagedRef<'static, A, ManagedVec<A, EsdtTokenPayment<A>>>
where
A: CallValueApi + ManagedTypeApi,
O: ContractBase<Api = A>,
{
obj.call_value().all_esdt_transfers()
}