From 647a1a1c3ff375b5765e40b1c784c4edb7eda34a Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Mar 2024 21:15:35 +0200 Subject: [PATCH] tx payment - file reorg --- framework/base/src/types/interaction/mod.rs | 6 - .../base/src/types/interaction/tx_payment.rs | 236 ++++-------------- .../{ => tx_payment}/tx_payment_egld.rs | 32 +-- .../{ => tx_payment}/tx_payment_egld_value.rs | 4 +- .../tx_payment/tx_payment_multi_esdt.rs | 64 +++++ .../interaction/tx_payment/tx_payment_none.rs | 48 ++++ .../{ => tx_payment}/tx_payment_normalize.rs | 6 +- .../tx_payment/tx_payment_other.rs | 76 ++++++ .../tx_payment/tx_payment_single_esdt.rs | 32 +++ 9 files changed, 268 insertions(+), 236 deletions(-) rename framework/base/src/types/interaction/{ => tx_payment}/tx_payment_egld.rs (71%) rename framework/base/src/types/interaction/{ => tx_payment}/tx_payment_egld_value.rs (92%) create mode 100644 framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs create mode 100644 framework/base/src/types/interaction/tx_payment/tx_payment_none.rs rename framework/base/src/types/interaction/{ => tx_payment}/tx_payment_normalize.rs (97%) create mode 100644 framework/base/src/types/interaction/tx_payment/tx_payment_other.rs create mode 100644 framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs diff --git a/framework/base/src/types/interaction/mod.rs b/framework/base/src/types/interaction/mod.rs index 44697cddb8..7b751e162b 100644 --- a/framework/base/src/types/interaction/mod.rs +++ b/framework/base/src/types/interaction/mod.rs @@ -32,9 +32,6 @@ mod tx_env_sc; mod tx_from; mod tx_gas; mod tx_payment; -mod tx_payment_egld; -mod tx_payment_egld_value; -mod tx_payment_normalize; mod tx_proxy; mod tx_result_handler; mod tx_rh_list; @@ -73,9 +70,6 @@ pub use tx_env_sc::*; pub use tx_from::*; pub use tx_gas::*; pub use tx_payment::*; -pub use tx_payment_egld::*; -pub use tx_payment_egld_value::TxEgldValue; -pub use tx_payment_normalize::TxPaymentNormalize; pub use tx_proxy::*; pub use tx_result_handler::*; pub use tx_rh_list::*; diff --git a/framework/base/src/types/interaction/tx_payment.rs b/framework/base/src/types/interaction/tx_payment.rs index 41d7b7f537..af40f79f56 100644 --- a/framework/base/src/types/interaction/tx_payment.rs +++ b/framework/base/src/types/interaction/tx_payment.rs @@ -1,54 +1,25 @@ +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, contract_base::SendRawWrapper, - imports::{BigUint, ManagedBuffer}, types::{ - EgldOrEsdtTokenPayment, EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedAddress, - MultiEsdtPayment, + BigUint, EgldOrEsdtTokenPayment, EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedAddress, + ManagedBuffer, MultiEsdtPayment, }, }; -use super::{Egld, FunctionCall, TxEnv}; - -#[derive(Clone)] -pub struct AnnotatedEgldPayment -where - Api: ManagedTypeApi, -{ - pub value: BigUint, - pub annotation: ManagedBuffer, -} - -impl AnnotatedEgldPayment -where - Api: ManagedTypeApi, -{ - pub fn new_egld(value: BigUint) -> Self { - let annotation = value.to_display(); - AnnotatedEgldPayment { value, annotation } - } -} - -#[derive(Clone)] -pub struct FullPaymentData -where - Api: ManagedTypeApi, -{ - pub egld: Option>, - pub multi_esdt: MultiEsdtPayment, -} - -impl Default for FullPaymentData -where - Api: ManagedTypeApi, -{ - fn default() -> Self { - Self { - egld: None, - multi_esdt: Default::default(), - } - } -} +use super::{FunctionCall, TxEnv}; /// Describes a payment that is part of a transaction. pub trait TxPayment @@ -68,177 +39,54 @@ where fn into_full_payment_data(self, env: &Env) -> FullPaymentData; } -impl TxPayment for () +/// Marks a payment object that only contains EGLD or nothing at all. +pub trait TxPaymentEgldOnly: TxPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { - true - } - - fn perform_transfer_execute( - self, - env: &Env, - to: &ManagedAddress, - gas_limit: u64, - fc: FunctionCall, - ) { - Egld(BigUint::zero()).perform_transfer_execute(env, to, gas_limit, fc); - } + fn with_egld_value(&self, f: F) -> R + where + F: FnOnce(&BigUint) -> R; - fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { - FullPaymentData::default() - } + fn into_egld_payment(self, env: &Env) -> BigUint; } -impl TxPayment for EsdtTokenPayment -where - Env: TxEnv, -{ - fn is_no_payment(&self) -> bool { - self.amount == 0u32 - } - - fn perform_transfer_execute( - self, - env: &Env, - to: &ManagedAddress, - gas_limit: u64, - fc: FunctionCall, - ) { - MultiEsdtPayment::from_single_item(self).perform_transfer_execute(env, to, gas_limit, fc); - } - - fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { - FullPaymentData { - egld: None, - multi_esdt: MultiEsdtPayment::from_single_item(self), - } - } -} - -impl TxPayment for &MultiEsdtPayment +#[derive(Clone)] +pub struct AnnotatedEgldPayment where - Env: TxEnv, + Api: ManagedTypeApi, { - fn is_no_payment(&self) -> bool { - self.is_empty() - } - - fn perform_transfer_execute( - self, - _env: &Env, - to: &ManagedAddress, - gas_limit: u64, - fc: FunctionCall, - ) { - let _ = SendRawWrapper::::new().multi_esdt_transfer_execute( - to, - self, - gas_limit, - &fc.function_name, - &fc.arg_buffer, - ); - } - - fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { - FullPaymentData { - egld: None, - multi_esdt: self.clone(), - } - } + pub value: BigUint, + pub annotation: ManagedBuffer, } -impl TxPayment for MultiEsdtPayment +impl AnnotatedEgldPayment where - Env: TxEnv, + Api: ManagedTypeApi, { - fn is_no_payment(&self) -> bool { - self.is_empty() - } - - fn perform_transfer_execute( - self, - env: &Env, - to: &ManagedAddress, - gas_limit: u64, - fc: FunctionCall, - ) { - (&self).perform_transfer_execute(env, to, gas_limit, fc); - } - - fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { - FullPaymentData { - egld: None, - multi_esdt: self, - } + pub fn new_egld(value: BigUint) -> Self { + let annotation = value.to_display(); + AnnotatedEgldPayment { value, annotation } } } -impl TxPayment for EgldOrEsdtTokenPayment +#[derive(Clone)] +pub struct FullPaymentData where - Env: TxEnv, + Api: ManagedTypeApi, { - fn is_no_payment(&self) -> bool { - self.amount == 0u32 - } - - fn perform_transfer_execute( - self, - env: &Env, - to: &ManagedAddress, - gas_limit: u64, - fc: FunctionCall, - ) { - self.map_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 into_full_payment_data(self, env: &Env) -> FullPaymentData { - self.map_egld_or_esdt( - (), - |(), amount| TxPayment::::into_full_payment_data(Egld(amount), env), - |(), esdt_payment| TxPayment::::into_full_payment_data(esdt_payment, env), - ) - } + pub egld: Option>, + pub multi_esdt: MultiEsdtPayment, } -impl TxPayment for EgldOrMultiEsdtPayment +impl Default for FullPaymentData where - Env: TxEnv, + Api: ManagedTypeApi, { - 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 { - EgldOrMultiEsdtPayment::Egld(egld_amount) => { - Egld(egld_amount).perform_transfer_execute(env, to, gas_limit, fc) - }, - EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => { - multi_esdt_payment.perform_transfer_execute(env, to, gas_limit, fc) - }, - } - } - - fn into_full_payment_data(self, env: &Env) -> FullPaymentData { - match self { - EgldOrMultiEsdtPayment::Egld(egld_amount) => { - TxPayment::::into_full_payment_data(Egld(egld_amount), env) - }, - EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => { - TxPayment::::into_full_payment_data(multi_esdt_payment, env) - }, + fn default() -> Self { + Self { + egld: None, + multi_esdt: Default::default(), } } } diff --git a/framework/base/src/types/interaction/tx_payment_egld.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs similarity index 71% rename from framework/base/src/types/interaction/tx_payment_egld.rs rename to framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs index fca770068c..f84d6afacb 100644 --- a/framework/base/src/types/interaction/tx_payment_egld.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs @@ -4,8 +4,8 @@ use crate::{ }; use super::{ - AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, - TxPayment, + AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment, + TxPaymentEgldOnly, }; /// Indicates the EGLD payment in a transaction. @@ -48,34 +48,6 @@ where } } -/// Marks a payment object that only contains EGLD or nothing at all. -pub trait TxPaymentEgldOnly: TxPayment -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R; - - fn into_egld_payment(self, env: &Env) -> BigUint; -} - -impl TxPaymentEgldOnly for () -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(&BigUint::zero()) - } - - fn into_egld_payment(self, _env: &Env) -> BigUint { - BigUint::zero() - } -} - impl TxPaymentEgldOnly for Egld where Env: TxEnv, diff --git a/framework/base/src/types/interaction/tx_payment_egld_value.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs similarity index 92% rename from framework/base/src/types/interaction/tx_payment_egld_value.rs rename to framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs index 94f424f2bc..561235eef2 100644 --- a/framework/base/src/types/interaction/tx_payment_egld_value.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs @@ -1,6 +1,6 @@ -use crate::types::BigUint; +use crate::types::{AnnotatedValue, BigUint}; -use super::{AnnotatedValue, TxEnv}; +use super::TxEnv; pub trait TxEgldValue: AnnotatedValue> where diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs new file mode 100644 index 0000000000..84a8de5e5b --- /dev/null +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs @@ -0,0 +1,64 @@ +use crate::{ + contract_base::SendRawWrapper, + types::{BigUint, ManagedAddress, ManagedVec, MultiEsdtPayment}, +}; + +use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment}; + +impl TxPayment for &MultiEsdtPayment +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, + ) { + let _ = SendRawWrapper::::new().multi_esdt_transfer_execute( + to, + self, + gas_limit, + &fc.function_name, + &fc.arg_buffer, + ); + } + + fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { + FullPaymentData { + egld: None, + multi_esdt: self.clone(), + } + } +} + +impl TxPayment for MultiEsdtPayment +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, + ) { + (&self).perform_transfer_execute(env, to, gas_limit, fc); + } + + fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { + FullPaymentData { + egld: None, + multi_esdt: self, + } + } +} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs new file mode 100644 index 0000000000..7b34ccfe9b --- /dev/null +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs @@ -0,0 +1,48 @@ +use crate::{ + contract_base::SendRawWrapper, + types::{BigUint, ManagedAddress, ManagedVec}, +}; + +use super::{ + AnnotatedEgldPayment, Egld, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment, + TxPaymentEgldOnly, +}; + +impl TxPayment for () +where + Env: TxEnv, +{ + fn is_no_payment(&self) -> bool { + true + } + + fn perform_transfer_execute( + self, + env: &Env, + to: &ManagedAddress, + gas_limit: u64, + fc: FunctionCall, + ) { + Egld(BigUint::zero()).perform_transfer_execute(env, to, gas_limit, fc); + } + + fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { + FullPaymentData::default() + } +} + +impl TxPaymentEgldOnly for () +where + Env: TxEnv, +{ + fn with_egld_value(&self, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(&BigUint::zero()) + } + + fn into_egld_payment(self, _env: &Env) -> BigUint { + BigUint::zero() + } +} diff --git a/framework/base/src/types/interaction/tx_payment_normalize.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_normalize.rs similarity index 97% rename from framework/base/src/types/interaction/tx_payment_normalize.rs rename to framework/base/src/types/interaction/tx_payment/tx_payment_normalize.rs index 9d64cbb4df..db263fa83f 100644 --- a/framework/base/src/types/interaction/tx_payment_normalize.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_normalize.rs @@ -1,11 +1,9 @@ use crate::types::{ BigUint, EgldOrEsdtTokenPayment, EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedAddress, - MultiEsdtPayment, + MultiEsdtPayment, TxFrom, TxToSpecified, }; -use super::{ - Egld, FunctionCall, TxEgldValue, TxEnv, TxFrom, TxPayment, TxToSpecified, -}; +use super::{Egld, FunctionCall, TxEgldValue, TxEnv, TxPayment}; /// Defines how a payment transforms a transaction, /// e.g. from ESDT transfer to ESDTTransfer builtin function. diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs new file mode 100644 index 0000000000..0c8bf7772b --- /dev/null +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs @@ -0,0 +1,76 @@ +use crate::{ + contract_base::SendRawWrapper, + types::{ + BigUint, Egld, EgldOrEsdtTokenPayment, EgldOrMultiEsdtPayment, ManagedAddress, ManagedVec, + }, +}; + +use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment}; + +impl TxPayment for EgldOrEsdtTokenPayment +where + Env: TxEnv, +{ + fn is_no_payment(&self) -> bool { + self.amount == 0u32 + } + + fn perform_transfer_execute( + self, + env: &Env, + to: &ManagedAddress, + gas_limit: u64, + fc: FunctionCall, + ) { + self.map_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 into_full_payment_data(self, env: &Env) -> FullPaymentData { + self.map_egld_or_esdt( + (), + |(), amount| TxPayment::::into_full_payment_data(Egld(amount), env), + |(), esdt_payment| TxPayment::::into_full_payment_data(esdt_payment, env), + ) + } +} + +impl TxPayment for EgldOrMultiEsdtPayment +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 { + EgldOrMultiEsdtPayment::Egld(egld_amount) => { + Egld(egld_amount).perform_transfer_execute(env, to, gas_limit, fc) + }, + EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => { + multi_esdt_payment.perform_transfer_execute(env, to, gas_limit, fc) + }, + } + } + + fn into_full_payment_data(self, env: &Env) -> FullPaymentData { + match self { + EgldOrMultiEsdtPayment::Egld(egld_amount) => { + TxPayment::::into_full_payment_data(Egld(egld_amount), env) + }, + EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => { + TxPayment::::into_full_payment_data(multi_esdt_payment, env) + }, + } + } +} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs new file mode 100644 index 0000000000..7651351db8 --- /dev/null +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs @@ -0,0 +1,32 @@ +use crate::{ + contract_base::SendRawWrapper, + types::{BigUint, EsdtTokenPayment, ManagedAddress, ManagedVec, MultiEsdtPayment}, +}; + +use super::{AnnotatedEgldPayment, FullPaymentData, FunctionCall, TxEgldValue, TxEnv, TxPayment}; + +impl TxPayment for EsdtTokenPayment +where + Env: TxEnv, +{ + fn is_no_payment(&self) -> bool { + self.amount == 0u32 + } + + fn perform_transfer_execute( + self, + env: &Env, + to: &ManagedAddress, + gas_limit: u64, + fc: FunctionCall, + ) { + MultiEsdtPayment::from_single_item(self).perform_transfer_execute(env, to, gas_limit, fc); + } + + fn into_full_payment_data(self, _env: &Env) -> FullPaymentData { + FullPaymentData { + egld: None, + multi_esdt: MultiEsdtPayment::from_single_item(self), + } + } +}