From 38e6794bfb4c5273f1d0cb4e9d33055390bedecc Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Tue, 26 Mar 2024 19:24:10 +0200 Subject: [PATCH] unified sytax - integrate in Tx --- framework/base/src/types/interaction/tx.rs | 32 ++++++- .../base/src/types/interaction/tx_payment.rs | 1 + .../tx_payment_egld_or_multi_esdt.rs | 93 +++++++++++++++++++ .../tx_payment/tx_payment_single_esdt_ref.rs | 4 +- .../wrapped/egld_or_multi_esdt_payment.rs | 21 +++++ .../base/src/types/managed/wrapped/mod.rs | 2 +- 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt.rs diff --git a/framework/base/src/types/interaction/tx.rs b/framework/base/src/types/interaction/tx.rs index 583e038625..1251dc656c 100644 --- a/framework/base/src/types/interaction/tx.rs +++ b/framework/base/src/types/interaction/tx.rs @@ -1,7 +1,7 @@ use crate::{ api::CallTypeApi, contract_base::BlockchainWrapper, - proxy_imports::{EsdtTokenPaymentRefs, TokenIdentifier}, + proxy_imports::{EgldOrMultiEsdtPaymentRefs, EsdtTokenPaymentRefs, TokenIdentifier}, types::{ BigUint, CodeMetadata, EgldOrEsdtTokenPayment, EsdtTokenPayment, ManagedAddress, ManagedBuffer, ManagedOption, ManagedVec, MultiEsdtPayment, @@ -260,6 +260,36 @@ where } } + pub fn egld_ref( + self, + amount: &BigUint, + ) -> Tx, Gas, Data, RH> { + Tx { + env: self.env, + from: self.from, + to: self.to, + payment: EgldOrMultiEsdtPaymentRefs::Egld(amount), + gas: self.gas, + data: self.data, + result_handler: self.result_handler, + } + } + + pub fn mult_esdt_payment_ref( + self, + amount: &ManagedVec>, + ) -> Tx, Gas, Data, RH> { + Tx { + env: self.env, + from: self.from, + to: self.to, + payment: EgldOrMultiEsdtPaymentRefs::MultiEsdt(amount), + gas: self.gas, + data: self.data, + result_handler: self.result_handler, + } + } + /// Backwards compatibility. pub fn with_multi_token_transfer( self, diff --git a/framework/base/src/types/interaction/tx_payment.rs b/framework/base/src/types/interaction/tx_payment.rs index 4b2f54e29f..d70fc79349 100644 --- a/framework/base/src/types/interaction/tx_payment.rs +++ b/framework/base/src/types/interaction/tx_payment.rs @@ -1,4 +1,5 @@ mod tx_payment_egld; +mod tx_payment_egld_or_multi_esdt; mod tx_payment_egld_value; mod tx_payment_multi_esdt; mod tx_payment_none; diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt.rs new file mode 100644 index 0000000000..7ebfe42806 --- /dev/null +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_or_multi_esdt.rs @@ -0,0 +1,93 @@ +use alloc::borrow::ToOwned; + +use crate::{ + api::ManagedTypeApi, + contract_base::SendRawWrapper, + proxy_imports::{ + AnnotatedValue, EgldOrMultiEsdtPayment, EgldOrMultiEsdtPaymentRefs, ManagedType, + }, + types::{ + BigUint, EsdtTokenPayment, EsdtTokenPaymentRefs, ManagedAddress, ManagedVec, + MultiEsdtPayment, TxFrom, TxToSpecified, + }, +}; + +use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment}; + +impl<'a, Env> TxPayment for EgldOrMultiEsdtPaymentRefs<'a, Env::Api> +where + Env: TxEnv, +{ + fn is_no_payment(&self) -> bool { + self.is_empty() + } + + fn perform_transfer_execute( + self, + env: &Env, + to: &ManagedAddress, + gas_limit: u64, + fc: FunctionCall, + ) { + match self { + EgldOrMultiEsdtPaymentRefs::Egld(egld_amount) => { + let _ = SendRawWrapper::::new().direct_egld_execute( + to, + egld_amount, + gas_limit, + &fc.function_name, + &fc.arg_buffer, + ); + }, + EgldOrMultiEsdtPaymentRefs::MultiEsdt(multi_esdt_payment) => { + let _ = SendRawWrapper::::new().multi_esdt_transfer_execute( + to, + multi_esdt_payment, + gas_limit, + &fc.function_name, + &fc.arg_buffer, + ); + }, + } + } + + fn with_normalized( + self, + env: &Env, + from: &From, + to: To, + fc: FunctionCall, + f: F, + ) -> R + where + From: TxFrom, + To: TxToSpecified, + F: FnOnce(&ManagedAddress, &BigUint, &FunctionCall) -> R, + { + match self { + EgldOrMultiEsdtPaymentRefs::Egld(egld_amount) => { + to.with_address_ref(env, |to_addr| f(to_addr, &BigUint::zero(), &fc)) + }, + EgldOrMultiEsdtPaymentRefs::MultiEsdt(multi_esdt_payment) => { + to.with_address_ref(env, |to_addr| { + let fc_conv = + fc.convert_to_multi_transfer_esdt_call(to_addr, multi_esdt_payment); + f(to_addr, &BigUint::zero(), &fc_conv) + }) + }, + } + } + + fn into_full_payment_data(self, env: &Env) -> FullPaymentData { + match self { + EgldOrMultiEsdtPaymentRefs::Egld(egld_amount) => FullPaymentData { + egld: Some(AnnotatedEgldPayment::new_egld(egld_amount.into_value(env))), + multi_esdt: ManagedVec::new(), + }, + EgldOrMultiEsdtPaymentRefs::MultiEsdt(multi_esdt_payment) => FullPaymentData { + egld: None, + multi_esdt: multi_esdt_payment.clone(), + }, + } + } +} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs index c0d568e11b..fd976e14d4 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs @@ -2,8 +2,8 @@ use crate::{ api::ManagedTypeApi, contract_base::SendRawWrapper, types::{ - BigUint, EsdtTokenPayment, EsdtTokenPaymentRefs, ManagedAddress, ManagedVec, - MultiEsdtPayment, TokenIdentifier, TxFrom, TxToSpecified, + BigUint, EsdtTokenPaymentRefs, ManagedAddress, ManagedVec, MultiEsdtPayment, + TokenIdentifier, TxFrom, TxToSpecified, }, }; diff --git a/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs b/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs index 11a8d7ea83..838fc278a4 100644 --- a/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs +++ b/framework/base/src/types/managed/wrapped/egld_or_multi_esdt_payment.rs @@ -34,6 +34,7 @@ impl EgldOrMultiEsdtPayment { } } +/// The version of `EgldOrMultiEsdtPayment` that contains referrences instead of owned fields. pub enum EgldOrMultiEsdtPaymentRefs<'a, M: ManagedTypeApi> { Egld(&'a BigUint), MultiEsdt(&'a ManagedVec>), @@ -51,3 +52,23 @@ impl EgldOrMultiEsdtPayment { } } } + +impl<'a, M: ManagedTypeApi> EgldOrMultiEsdtPaymentRefs<'a, M> { + pub fn to_owned_payment(&self) -> EgldOrMultiEsdtPayment { + match self { + EgldOrMultiEsdtPaymentRefs::Egld(egld_value) => { + EgldOrMultiEsdtPayment::Egld((*egld_value).clone()) + }, + EgldOrMultiEsdtPaymentRefs::MultiEsdt(esdt_payments) => { + EgldOrMultiEsdtPayment::MultiEsdt((*esdt_payments).clone()) + }, + } + } + + pub fn is_empty(&self) -> bool { + match self { + EgldOrMultiEsdtPaymentRefs::Egld(egld_value) => *egld_value == &0u32, + EgldOrMultiEsdtPaymentRefs::MultiEsdt(esdt_payments) => esdt_payments.is_empty(), + } + } +} diff --git a/framework/base/src/types/managed/wrapped/mod.rs b/framework/base/src/types/managed/wrapped/mod.rs index a2dcd6008a..5a5e907403 100644 --- a/framework/base/src/types/managed/wrapped/mod.rs +++ b/framework/base/src/types/managed/wrapped/mod.rs @@ -20,7 +20,7 @@ mod token_identifier; pub use egld_or_esdt_token_identifier::EgldOrEsdtTokenIdentifier; pub use egld_or_esdt_token_payment::EgldOrEsdtTokenPayment; -pub use egld_or_multi_esdt_payment::EgldOrMultiEsdtPayment; +pub use egld_or_multi_esdt_payment::{EgldOrMultiEsdtPayment, EgldOrMultiEsdtPaymentRefs}; pub(crate) use encoded_managed_vec_item::EncodedManagedVecItem; pub use esdt_token_data::EsdtTokenData; pub use esdt_token_payment::{EsdtTokenPayment, EsdtTokenPaymentRefs, MultiEsdtPayment};