Skip to content

Commit

Permalink
tx payment refactor - with_normalized moved back to TxPayment
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Mar 22, 2024
1 parent 2497d68 commit 1e95bad
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 207 deletions.
6 changes: 3 additions & 3 deletions framework/base/src/types/interaction/contract_call_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
function_call: self
.basic
.function_call
.convert_to_single_transfer_fungible_call(payment),
.convert_to_single_transfer_fungible_call(&payment),
explicit_gas_limit: self.basic.explicit_gas_limit,
_return_type: PhantomData,
},
Expand All @@ -55,7 +55,7 @@ where
function_call: self
.basic
.function_call
.convert_to_single_transfer_nft_call(&self.basic.to, payment),
.convert_to_single_transfer_nft_call(&self.basic.to, &payment),
explicit_gas_limit: self.basic.explicit_gas_limit,
_return_type: PhantomData,
},
Expand All @@ -78,7 +78,7 @@ where
function_call: self
.basic
.function_call
.convert_to_multi_transfer_esdt_call(&self.basic.to, payments),
.convert_to_multi_transfer_esdt_call(&self.basic.to, &payments),
explicit_gas_limit: self.basic.explicit_gas_limit,
_return_type: PhantomData,
},
Expand Down
8 changes: 4 additions & 4 deletions framework/base/src/types/interaction/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
/// Constructs `ESDTTransfer` builtin function call.
pub(super) fn convert_to_single_transfer_fungible_call(
self,
payment: EsdtTokenPayment<Api>,
payment: &EsdtTokenPayment<Api>,
) -> FunctionCall<Api> {
FunctionCall::new(ESDT_TRANSFER_FUNC_NAME)
.argument(&payment.token_identifier)
Expand All @@ -177,7 +177,7 @@ where
pub(super) fn convert_to_single_transfer_nft_call(
self,
to: &ManagedAddress<Api>,
payment: EsdtTokenPayment<Api>,
payment: &EsdtTokenPayment<Api>,
) -> FunctionCall<Api> {
FunctionCall::new(ESDT_NFT_TRANSFER_FUNC_NAME)
.argument(&payment.token_identifier)
Expand All @@ -191,13 +191,13 @@ where
pub(super) fn convert_to_multi_transfer_esdt_call(
self,
to: &ManagedAddress<Api>,
payments: ManagedVec<Api, EsdtTokenPayment<Api>>,
payments: &ManagedVec<Api, EsdtTokenPayment<Api>>,
) -> FunctionCall<Api> {
let mut result = FunctionCall::new(ESDT_MULTI_TRANSFER_FUNC_NAME)
.argument(&to)
.argument(&payments.len());

for payment in payments.into_iter() {
for payment in payments {
result = result
.argument(&payment.token_identifier)
.argument(&payment.token_nonce)
Expand Down
6 changes: 3 additions & 3 deletions framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::{
ContractCallNoPayment, ContractCallWithEgld, ContractDeploy, DeployCall, Egld, EgldPayment,
ExplicitGas, FromSource, FunctionCall, ManagedArgBuffer, OriginalResultMarker, RHList,
RHListAppendNoRet, RHListAppendRet, RHListItem, TxCodeSource, TxCodeValue, TxData, TxEgldValue,
TxEnv, TxFrom, TxFromSourceValue, TxGas, TxPayment, TxPaymentEgldOnly, TxPaymentNormalize,
TxProxyTrait, TxResultHandler, TxScEnv, TxTo, TxToSpecified,
TxEnv, TxFrom, TxFromSourceValue, TxGas, TxPayment, TxPaymentEgldOnly, TxProxyTrait,
TxResultHandler, TxScEnv, TxTo, TxToSpecified,
};

#[must_use]
Expand Down Expand Up @@ -565,7 +565,7 @@ impl<Api, To, Payment, OriginalResult> ContractCallBase<Api>
where
Api: CallTypeApi + 'static,
To: TxToSpecified<TxScEnv<Api>>,
Payment: TxPaymentNormalize<TxScEnv<Api>, (), To>,
Payment: TxPayment<TxScEnv<Api>>,
OriginalResult: TopEncodeMulti,
{
type OriginalResult = OriginalResult;
Expand Down
4 changes: 2 additions & 2 deletions framework/base/src/types/interaction/tx_call_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

use super::{
OriginalResultMarker, Tx, TxData, TxDataFunctionCall, TxEnv, TxFrom, TxGas, TxPayment,
TxPaymentNormalize, TxResultHandler, TxScEnv, TxTo, TxToSpecified,
TxResultHandler, TxScEnv, TxTo, TxToSpecified,
};

pub trait TxAsyncCallCallback<Api>: TxResultHandler<TxScEnv<Api>>
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<Api, To, Payment, FC, RH> Tx<TxScEnv<Api>, (), To, Payment, (), FC, RH>
where
Api: CallTypeApi,
To: TxToSpecified<TxScEnv<Api>>,
Payment: TxPaymentNormalize<TxScEnv<Api>, (), To>,
Payment: TxPayment<TxScEnv<Api>>,
FC: TxDataFunctionCall<TxScEnv<Api>>,
RH: TxAsyncCallCallback<Api>,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{

use super::{
callback_closure::CallbackClosureWithGas, ExplicitGas, FunctionCall, OriginalResultMarker, Tx,
TxGas, TxPayment, TxPaymentNormalize, TxResultHandler, TxScEnv, TxToSpecified,
TxGas, TxPayment, TxResultHandler, TxScEnv, TxToSpecified,
};

pub trait TxPromisesCallback<Api>: TxResultHandler<TxScEnv<Api>>
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<Api, To, Payment, Callback>
where
Api: CallTypeApi,
To: TxToSpecified<TxScEnv<Api>>,
Payment: TxPaymentNormalize<TxScEnv<Api>, (), To>,
Payment: TxPayment<TxScEnv<Api>>,
Callback: TxPromisesCallback<Api>,
{
pub fn register_promise(self) {
Expand Down
7 changes: 3 additions & 4 deletions framework/base/src/types/interaction/tx_call_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::{

use super::{
contract_call_exec::decode_result, BackTransfers, ConsNoRet, ConsRet, OriginalResultMarker,
RHList, RHListItem, Tx, TxDataFunctionCall, TxEnv, TxGas, TxPaymentNormalize, TxScEnv,
TxToSpecified,
RHList, RHListItem, Tx, TxDataFunctionCall, TxEnv, TxGas, TxPayment, TxScEnv, TxToSpecified,
};

pub trait RHListItemSync<Env, Original>: RHListItem<Env, Original>
Expand Down Expand Up @@ -90,7 +89,7 @@ impl<Api, To, Payment, Gas, FC, RH> Tx<TxScEnv<Api>, (), To, Payment, Gas, FC, R
where
Api: CallTypeApi,
To: TxToSpecified<TxScEnv<Api>>,
Payment: TxPaymentNormalize<TxScEnv<Api>, (), To>,
Payment: TxPayment<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
FC: TxDataFunctionCall<TxScEnv<Api>>,
RH: RHListSync<TxScEnv<Api>>,
Expand Down Expand Up @@ -133,7 +132,7 @@ impl<Api, To, Payment, Gas, FC, OriginalResult>
where
Api: CallTypeApi,
To: TxToSpecified<TxScEnv<Api>>,
Payment: TxPaymentNormalize<TxScEnv<Api>, (), To>,
Payment: TxPayment<TxScEnv<Api>>,
Gas: TxGas<TxScEnv<Api>>,
FC: TxDataFunctionCall<TxScEnv<Api>>,
{
Expand Down
22 changes: 19 additions & 3 deletions framework/base/src/types/interaction/tx_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ mod tx_payment_egld;
mod tx_payment_egld_value;
mod tx_payment_multi_esdt;
mod tx_payment_none;
mod tx_payment_normalize;
mod tx_payment_other;
mod tx_payment_single_esdt;

pub use tx_payment_egld::{Egld, EgldPayment};
pub use tx_payment_egld_value::TxEgldValue;
pub use tx_payment_normalize::TxPaymentNormalize;

use crate::{
api::ManagedTypeApi,
Expand All @@ -19,15 +17,18 @@ use crate::{
},
};

use super::{FunctionCall, TxEnv};
use super::{FunctionCall, TxEnv, TxFrom, TxToSpecified};

/// Describes a payment that is part of a transaction.
pub trait TxPayment<Env>
where
Env: TxEnv,
{
/// Returns true if payment indicates transfer of either non-zero EGLD or ESDT amounts.
fn is_no_payment(&self) -> bool;

/// Transfer-execute calls have different APIs for different payments types.
/// This method selects between them.
fn perform_transfer_execute(
self,
env: &Env,
Expand All @@ -36,6 +37,21 @@ where
fc: FunctionCall<Env::Api>,
);

/// Converts an ESDT call to a built-in function call, if necessary.
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;

/// Payment data to be used by the testing framework. Will be refactored.
fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api>;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
contract_base::SendRawWrapper,
types::{BigUint, ManagedAddress, ManagedVec},
types::{BigUint, ManagedAddress, ManagedVec, TxFrom, TxToSpecified},
};

use super::{
Expand Down Expand Up @@ -40,6 +40,25 @@ where
})
}

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,
{
to.with_address_ref(env, |to_addr| {
self.0
.with_egld_value(|egld_value| f(to_addr, egld_value, &fc))
})
}

fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api> {
FullPaymentData {
egld: Some(AnnotatedEgldPayment::new_egld(self.0.into_value(env))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
contract_base::SendRawWrapper,
types::{BigUint, ManagedAddress, ManagedVec, MultiEsdtPayment},
types::{BigUint, ManagedAddress, ManagedVec, MultiEsdtPayment, TxFrom, TxToSpecified},
};

use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment};
Expand Down Expand Up @@ -29,6 +29,29 @@ where
);
}

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.len() {
0 => ().with_normalized(env, from, to, fc, f),
1 => self.get(0).with_normalized(env, from, to, fc, f),
_ => to.with_address_ref(env, |to_addr| {
let fc_conv = fc.convert_to_multi_transfer_esdt_call(to_addr, self);
f(&from.resolve_address(env), &BigUint::zero(), &fc_conv)
}),
}
}

fn into_full_payment_data(self, _env: &Env) -> FullPaymentData<Env::Api> {
FullPaymentData {
egld: None,
Expand All @@ -55,6 +78,22 @@ where
(&self).perform_transfer_execute(env, to, gas_limit, fc);
}

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,
{
(&self).with_normalized(env, from, to, fc, f)
}

fn into_full_payment_data(self, _env: &Env) -> FullPaymentData<Env::Api> {
FullPaymentData {
egld: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
contract_base::SendRawWrapper,
types::{BigUint, ManagedAddress, ManagedVec},
types::{BigUint, ManagedAddress, ManagedVec, TxFrom, TxToSpecified},
};

use super::{
Expand All @@ -26,6 +26,22 @@ where
Egld(BigUint::zero()).perform_transfer_execute(env, to, gas_limit, fc);
}

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,
{
to.with_address_ref(env, |to_addr| f(to_addr, &BigUint::zero(), &fc))
}

fn into_full_payment_data(self, _env: &Env) -> FullPaymentData<Env::Api> {
FullPaymentData::default()
}
Expand Down
Loading

0 comments on commit 1e95bad

Please sign in to comment.