Skip to content

Commit

Permalink
unified sytax - integrate in Tx
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed Mar 26, 2024
1 parent 5a1a184 commit 38e6794
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
32 changes: 31 additions & 1 deletion framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -260,6 +260,36 @@ where
}
}

pub fn egld_ref(
self,
amount: &BigUint<Env::Api>,
) -> Tx<Env, From, To, EgldOrMultiEsdtPaymentRefs<'_, Env::Api>, 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<Env::Api, EsdtTokenPayment<Env::Api>>,
) -> Tx<Env, From, To, EgldOrMultiEsdtPaymentRefs<'_, Env::Api>, 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,
Expand Down
1 change: 1 addition & 0 deletions framework/base/src/types/interaction/tx_payment.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Env> 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<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
match self {
EgldOrMultiEsdtPaymentRefs::Egld(egld_amount) => {
let _ = SendRawWrapper::<Env::Api>::new().direct_egld_execute(
to,
egld_amount,
gas_limit,
&fc.function_name,
&fc.arg_buffer,
);
},
EgldOrMultiEsdtPaymentRefs::MultiEsdt(multi_esdt_payment) => {
let _ = SendRawWrapper::<Env::Api>::new().multi_esdt_transfer_execute(
to,
multi_esdt_payment,
gas_limit,
&fc.function_name,
&fc.arg_buffer,
);
},
}
}

fn with_normalized<From, To, F, R>(
self,
env: &Env,
from: &From,
to: To,
fc: FunctionCall<Env::Api>,
f: F,
) -> R
where
From: TxFrom<Env>,
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, &FunctionCall<Env::Api>) -> 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<Env::Api> {
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(),
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<M: ManagedTypeApi> EgldOrMultiEsdtPayment<M> {
}
}

/// The version of `EgldOrMultiEsdtPayment` that contains referrences instead of owned fields.
pub enum EgldOrMultiEsdtPaymentRefs<'a, M: ManagedTypeApi> {
Egld(&'a BigUint<M>),
MultiEsdt(&'a ManagedVec<M, EsdtTokenPayment<M>>),
Expand All @@ -51,3 +52,23 @@ impl<M: ManagedTypeApi> EgldOrMultiEsdtPayment<M> {
}
}
}

impl<'a, M: ManagedTypeApi> EgldOrMultiEsdtPaymentRefs<'a, M> {
pub fn to_owned_payment(&self) -> EgldOrMultiEsdtPayment<M> {
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(),
}
}
}
2 changes: 1 addition & 1 deletion framework/base/src/types/managed/wrapped/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 38e6794

Please sign in to comment.