novax_data/types/
payment.rs

1use multiversx_sc::api::ManagedTypeApi;
2use multiversx_sc::types::EsdtTokenPayment;
3use multiversx_sc_scenario::api::StaticApi;
4use crate::types::managed::ManagedConvertible;
5use crate::types::native::NativeConvertible;
6
7/// Represents a token payment on the blockchain.
8///
9/// This struct encapsulates the details of a payment made using a specific token.
10///
11/// # Example
12/// ```
13/// # use num_bigint::BigUint;
14/// # use novax_data::Payment;
15/// let payment = Payment {
16///     token_identifier: "WEGLD-d7c6bb".to_string(),
17///     token_nonce: 0,
18///     amount: BigUint::from(10u8).pow(18)
19/// };
20/// ```
21#[derive(Clone, PartialEq, Debug)]
22pub struct Payment {
23    /// A `String` representing the unique identifier of the token involved in the payment.
24    pub token_identifier: String,
25    /// A `u64` value representing the nonce associated with the token,
26    /// used to differentiate between different instances of the same token.
27    pub token_nonce: u64,
28    /// A `num_bigint::BigUint` representing the amount of tokens being transferred.
29    pub amount: num_bigint::BigUint
30}
31
32impl<M: ManagedTypeApi> NativeConvertible for EsdtTokenPayment<M> {
33    type Native = Payment;
34
35    fn to_native(&self) -> Self::Native {
36        Payment {
37            token_identifier: self.token_identifier.to_native(),
38            token_nonce: self.token_nonce.to_native(),
39            amount: self.amount.to_native()
40        }
41    }
42}
43
44impl ManagedConvertible<EsdtTokenPayment<StaticApi>> for Payment {
45    fn to_managed(&self) -> EsdtTokenPayment<StaticApi> {
46        EsdtTokenPayment::new(
47            self.token_identifier.to_managed(),
48            self.token_nonce.to_managed(),
49            self.amount.to_managed()
50        )
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use multiversx_sc::types::{BigUint, EsdtTokenPayment, TokenIdentifier};
57    use multiversx_sc_scenario::api::StaticApi;
58    use crate::Payment;
59    use crate::types::managed::ManagedConvertible;
60
61    #[test]
62    fn test_payment_to_managed_payment() {
63        let payment = Payment {
64            token_identifier: "WEGLD-abcdef".to_string(),
65            token_nonce: 14u64,
66            amount: num_bigint::BigUint::from(100u8),
67        };
68        let managed: EsdtTokenPayment<StaticApi> = payment.to_managed();
69
70        let expected = EsdtTokenPayment::new(
71            TokenIdentifier::from("WEGLD-abcdef"),
72            14u64,
73            BigUint::from(100u64)
74        );
75
76        assert_eq!(
77            managed,
78            expected
79        );
80    }
81}