multiversx_sc/types/managed/multi_value/
esdt_token_payment_multi_value.rs

1use crate::{
2    abi::TypeAbiFrom,
3    codec::{
4        multi_types::MultiValue3, DecodeErrorHandler, EncodeErrorHandler, MultiValueConstLength,
5        TopDecodeMulti, TopDecodeMultiInput, TopEncodeMulti, TopEncodeMultiOutput,
6    },
7    types::ManagedVecRef,
8};
9
10use crate::{
11    abi::{TypeAbi, TypeName},
12    api::ManagedTypeApi,
13    types::{BigUint, EsdtTokenPayment, ManagedVecItem, TokenIdentifier},
14};
15
16/// Thin wrapper around EsdtTokenPayment, which has different I/O behaviour:
17/// - as input, is built from 3 arguments instead of 1: token identifier, nonce, value
18/// - as output, it becomes 3 results instead of 1: token identifier, nonce, value
19#[derive(Clone, PartialEq, Eq, Debug)]
20pub struct EsdtTokenPaymentMultiValue<M: ManagedTypeApi> {
21    obj: EsdtTokenPayment<M>,
22}
23
24#[deprecated(
25    since = "0.29.3",
26    note = "Alias kept for backwards compatibility. Replace with `EsdtTokenPaymentMultiValue`"
27)]
28pub type EsdtTokenPaymentMultiArg<M> = EsdtTokenPaymentMultiValue<M>;
29
30impl<M: ManagedTypeApi> From<EsdtTokenPayment<M>> for EsdtTokenPaymentMultiValue<M> {
31    #[inline]
32    fn from(obj: EsdtTokenPayment<M>) -> Self {
33        EsdtTokenPaymentMultiValue { obj }
34    }
35}
36
37impl<M: ManagedTypeApi> EsdtTokenPaymentMultiValue<M> {
38    pub fn into_inner(self) -> EsdtTokenPayment<M> {
39        self.obj
40    }
41}
42
43impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPaymentMultiValue<M> {
44    type PAYLOAD = <EsdtTokenPayment<M> as ManagedVecItem>::PAYLOAD;
45    const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::<M>::SKIPS_RESERIALIZATION;
46    type Ref<'a> = ManagedVecRef<'a, Self>;
47
48    fn read_from_payload(payload: &Self::PAYLOAD) -> Self {
49        EsdtTokenPayment::read_from_payload(payload).into()
50    }
51
52    unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> {
53        ManagedVecRef::new(Self::read_from_payload(payload))
54    }
55
56    fn save_to_payload(self, payload: &mut Self::PAYLOAD) {
57        self.obj.save_to_payload(payload);
58    }
59}
60
61impl<M> TopEncodeMulti for EsdtTokenPaymentMultiValue<M>
62where
63    M: ManagedTypeApi,
64{
65    fn multi_encode_or_handle_err<O, H>(&self, output: &mut O, h: H) -> Result<(), H::HandledErr>
66    where
67        O: TopEncodeMultiOutput,
68        H: EncodeErrorHandler,
69    {
70        output.push_single_value(&self.obj.token_identifier, h)?;
71        output.push_single_value(&self.obj.token_nonce, h)?;
72        output.push_single_value(&self.obj.amount, h)?;
73        Ok(())
74    }
75}
76
77impl<M> TopDecodeMulti for EsdtTokenPaymentMultiValue<M>
78where
79    M: ManagedTypeApi,
80{
81    fn multi_decode_or_handle_err<I, H>(input: &mut I, h: H) -> Result<Self, H::HandledErr>
82    where
83        I: TopDecodeMultiInput,
84        H: DecodeErrorHandler,
85    {
86        let token_identifier = TokenIdentifier::multi_decode_or_handle_err(input, h)?;
87        let token_nonce = u64::multi_decode_or_handle_err(input, h)?;
88        let amount = BigUint::multi_decode_or_handle_err(input, h)?;
89        Ok(EsdtTokenPayment::new(token_identifier, token_nonce, amount).into())
90    }
91}
92
93impl<M> MultiValueConstLength for EsdtTokenPaymentMultiValue<M>
94where
95    M: ManagedTypeApi,
96{
97    const MULTI_VALUE_CONST_LEN: usize = 3;
98}
99
100impl<M> TypeAbiFrom<Self> for EsdtTokenPaymentMultiValue<M> where M: ManagedTypeApi {}
101
102impl<M> TypeAbi for EsdtTokenPaymentMultiValue<M>
103where
104    M: ManagedTypeApi,
105{
106    type Unmanaged = Self;
107
108    fn type_name() -> TypeName {
109        MultiValue3::<TokenIdentifier<M>, u64, BigUint<M>>::type_name()
110    }
111
112    fn type_name_rust() -> TypeName {
113        "EsdtTokenPaymentMultiValue<$API>".into()
114    }
115
116    fn is_variadic() -> bool {
117        true
118    }
119}