Skip to content

Commit

Permalink
unified syntax - egld of esdt payment refs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Apr 3, 2024
1 parent 0bd3e86 commit ea52e2c
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait DistributionModule {
self.tx()
.to(&distribution.address)
.raw_call(distribution.endpoint)
.egld_or_single_esdt((token_id.clone(), token_nonce, payment_amount))
.egld_or_single_esdt(token_id, token_nonce, &payment_amount)
.gas(distribution.gas_limit)
.transfer_execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ pub trait ForwarderRawAsync: super::forwarder_raw_common::ForwarderRawCommon {
.to(to)
.raw_call(endpoint_name)
.arguments_raw(args.to_arg_buffer())
.egld_or_single_esdt((payment_token, 0, payment_amount))
.payment(EgldOrEsdtTokenPayment::new(
payment_token,
0,
payment_amount,
))
}

#[endpoint]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait CallPromisesDirectModule {
self.tx()
.to(&to)
.raw_call(endpoint_name)
.egld_or_single_esdt(payment)
.payment(payment)
.arguments_raw(args.to_arg_buffer())
.gas(gas_limit)
.async_call_promise()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait RecursiveCaller {
.to(to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.egld_or_single_esdt((token_identifier.clone(), 0, amount.clone()))
.egld_or_single_esdt(token_identifier, 0, amount)
.async_call()
.with_callback(self.callbacks().recursive_send_funds_callback(
to,
Expand Down
2 changes: 1 addition & 1 deletion contracts/feature-tests/composability/vault/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub trait Vault {
self.tx()
.to(&caller)
.raw_call(endpoint_name.clone())
.egld_or_single_esdt(return_payment.clone())
.payment(&return_payment)
.gas(self.blockchain().get_gas_left() / 2)
.transfer_execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ where
#[inline]
pub fn get_sc_balance(&self, token: &EgldOrEsdtTokenIdentifier<A>, nonce: u64) -> BigUint<A> {
token.map_ref_or_else(
|| self.get_balance(&self.get_sc_address()),
|token_identifier| {
(),
|()| self.get_balance(&self.get_sc_address()),
|(), token_identifier| {
self.get_esdt_balance(&self.get_sc_address(), token_identifier, nonce)
},
)
Expand Down
24 changes: 16 additions & 8 deletions framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
api::CallTypeApi,
contract_base::BlockchainWrapper,
proxy_imports::{EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPaymentRefs},
types::{
BigUint, CodeMetadata, EgldOrEsdtTokenPayment, EgldOrMultiEsdtPayment,
EgldOrMultiEsdtPaymentRefs, EsdtTokenPayment, EsdtTokenPaymentRefs, ManagedAddress,
Expand Down Expand Up @@ -214,6 +215,20 @@ where
})
}

/// Syntactic sugar for `self.payment(EgldOrEsdtTokenPaymentRefs::new(...)`. Takes references.
pub fn egld_or_single_esdt<'a>(
self,
token_identifier: &'a EgldOrEsdtTokenIdentifier<Env::Api>,
token_nonce: u64,
amount: &'a BigUint<Env::Api>,
) -> Tx<Env, From, To, EgldOrEsdtTokenPaymentRefs<'a, Env::Api>, Gas, Data, RH> {
self.payment(EgldOrEsdtTokenPaymentRefs::new(
token_identifier,
token_nonce,
amount,
))
}

/// Sets a collection of ESDT transfers as the payment of the transaction.
///
/// Equivalend to just ``.payment(payments)`, but only accepts the multi-esdt types.
Expand All @@ -232,19 +247,12 @@ where
self.multi_esdt(payments)
}

pub fn egld_or_single_esdt<P: Into<EgldOrEsdtTokenPayment<Env::Api>>>(
self,
payment: P,
) -> Tx<Env, From, To, EgldOrEsdtTokenPayment<Env::Api>, Gas, Data, RH> {
self.payment(payment.into())
}

/// Backwards compatibility.
pub fn with_egld_or_single_esdt_transfer<P: Into<EgldOrEsdtTokenPayment<Env::Api>>>(
self,
payment: P,
) -> Tx<Env, From, To, EgldOrEsdtTokenPayment<Env::Api>, Gas, Data, RH> {
self.egld_or_single_esdt(payment)
self.payment(payment.into())
}

pub fn egld_or_multi_esdt<P: Into<EgldOrMultiEsdtPayment<Env::Api>>>(
Expand Down
3 changes: 1 addition & 2 deletions framework/base/src/types/interaction/tx_payment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod tx_payment_egld;
mod tx_payment_egld_or_esdt;
mod tx_payment_egld_or_esdt_ref;
mod tx_payment_egld_or_esdt_refs;
mod tx_payment_egld_or_multi_esdt;
mod tx_payment_egld_or_multi_esdt_ref;
mod tx_payment_egld_value;
Expand All @@ -10,7 +10,6 @@ mod tx_payment_single_esdt;
mod tx_payment_single_esdt_ref;

pub use tx_payment_egld::{Egld, EgldPayment};
pub use tx_payment_egld_or_esdt_ref::*;
pub use tx_payment_egld_value::TxEgldValue;
pub use tx_payment_multi_esdt::TxPaymentMultiEsdt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment};

impl<Env> TxPayment<Env> for EgldOrEsdtTokenPayment<Env::Api>
impl<Env> TxPayment<Env> for &EgldOrEsdtTokenPayment<Env::Api>
where
Env: TxEnv,
{
Expand All @@ -23,13 +23,60 @@ where
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
self.map_egld_or_esdt(
self.map_ref_egld_or_esdt(
(to, fc),
|(to, fc), amount| Egld(amount).perform_transfer_execute(env, to, gas_limit, fc),
|(to, fc), esdt_payment| esdt_payment.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.map_ref_egld_or_esdt(
(to, fc, f),
|(to, fc, f), amount| Egld(amount).with_normalized(env, from, to, fc, f),
|(to, fc, f), esdt_payment| esdt_payment.with_normalized(env, from, to, fc, f),
)
}

fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api> {
self.map_ref_egld_or_esdt(
(),
|(), amount| TxPayment::<Env>::into_full_payment_data(Egld(amount), env),
|(), esdt_payment| TxPayment::<Env>::into_full_payment_data(esdt_payment, env),
)
}
}

impl<Env> TxPayment<Env> for EgldOrEsdtTokenPayment<Env::Api>
where
Env: TxEnv,
{
fn is_no_payment(&self, env: &Env) -> bool {
(&self).is_no_payment(env)
}

fn perform_transfer_execute(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
(&self).perform_transfer_execute(env, to, gas_limit, fc)
}

fn with_normalized<From, To, F, R>(
self,
env: &Env,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use alloc::borrow::ToOwned;

use crate::{
api::ManagedTypeApi,
contract_base::SendRawWrapper,
types::{
AnnotatedValue, BigUint, EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPayment,
EgldOrEsdtTokenPaymentRefs, EgldOrMultiEsdtPayment, EsdtTokenPayment, EsdtTokenPaymentRefs,
ManagedAddress, ManagedType, ManagedVec, MultiEsdtPayment, TxFrom, TxToSpecified,
},
};

use super::{
AnnotatedEgldPayment, Egld, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment,
};

impl<'a, Env> TxPayment<Env> for EgldOrEsdtTokenPaymentRefs<'a, Env::Api>
where
Env: TxEnv,
{
fn is_no_payment(&self, _env: &Env) -> bool {
self.is_empty()
}

fn perform_transfer_execute(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
self.map_egld_or_esdt(
fc,
|fc, amount| Egld(amount).perform_transfer_execute(env, to, gas_limit, fc),
|fc, esdt_payment| esdt_payment.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.map_egld_or_esdt(
(to, fc, f),
|(to, fc, f), amount| Egld(amount).with_normalized(env, from, to, fc, f),
|(to, fc, f), esdt_payment| esdt_payment.with_normalized(env, from, to, fc, f),
)
}

fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api> {
self.map_egld_or_esdt(
(),
|(), amount| TxPayment::<Env>::into_full_payment_data(Egld(amount), env),
|(), esdt_payment| TxPayment::<Env>::into_full_payment_data(esdt_payment, env),
)
}
}
Loading

0 comments on commit ea52e2c

Please sign in to comment.