use multiversx_sc::api::ManagedTypeApi;
use multiversx_sc::types::EsdtTokenPayment;
use multiversx_sc_scenario::api::StaticApi;
use crate::types::managed::ManagedConvertible;
use crate::types::native::NativeConvertible;
#[derive(Clone, PartialEq, Debug)]
pub struct Payment {
pub token_identifier: String,
pub token_nonce: u64,
pub amount: num_bigint::BigUint
}
impl<M: ManagedTypeApi> NativeConvertible for EsdtTokenPayment<M> {
type Native = Payment;
fn to_native(&self) -> Self::Native {
Payment {
token_identifier: self.token_identifier.to_native(),
token_nonce: self.token_nonce.to_native(),
amount: self.amount.to_native()
}
}
}
impl ManagedConvertible<EsdtTokenPayment<StaticApi>> for Payment {
fn to_managed(&self) -> EsdtTokenPayment<StaticApi> {
EsdtTokenPayment::new(
self.token_identifier.to_managed(),
self.token_nonce.to_managed(),
self.amount.to_managed()
)
}
}
#[cfg(test)]
mod tests {
use multiversx_sc::types::{BigUint, EsdtTokenPayment, TokenIdentifier};
use multiversx_sc_scenario::api::StaticApi;
use crate::Payment;
use crate::types::managed::ManagedConvertible;
#[test]
fn test_payment_to_managed_payment() {
let payment = Payment {
token_identifier: "WEGLD-abcdef".to_string(),
token_nonce: 14u64,
amount: num_bigint::BigUint::from(100u8),
};
let managed: EsdtTokenPayment<StaticApi> = payment.to_managed();
let expected = EsdtTokenPayment::new(
TokenIdentifier::from("WEGLD-abcdef"),
14u64,
BigUint::from(100u64)
);
assert_eq!(
managed,
expected
);
}
}