From 4ac234c569076c995883dad551858c1a305c86ad Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Mar 2024 19:16:17 +0200 Subject: [PATCH 1/2] send wrapper - send egld via unified syntax --- .../src/contract_base/wrappers/send_wrapper.rs | 14 ++++++++------ framework/base/src/types/interaction/tx_call_te.rs | 11 +++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/framework/base/src/contract_base/wrappers/send_wrapper.rs b/framework/base/src/contract_base/wrappers/send_wrapper.rs index 62f99b767c..ec8fd41c5b 100644 --- a/framework/base/src/contract_base/wrappers/send_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_wrapper.rs @@ -75,7 +75,10 @@ where /// Used especially for sending EGLD to regular accounts. #[inline] pub fn direct_egld(&self, to: &ManagedAddress, amount: &BigUint) { - self.send_raw_wrapper().direct_egld(to, amount, Empty) + Tx::new_tx_from_sc() + .to(to) + .egld(amount) + .transfer(); } /// Sends EGLD to a given address, directly. @@ -83,11 +86,10 @@ where /// /// If the amount is 0, it returns without error. pub fn direct_non_zero_egld(&self, to: &ManagedAddress, amount: &BigUint) { - if amount == &0 { - return; - } - - self.direct_egld(to, amount) + Tx::new_tx_from_sc() + .to(to) + .egld(amount) + .transfer_non_zero(); } /// Sends either EGLD, ESDT or NFT to the target address, diff --git a/framework/base/src/types/interaction/tx_call_te.rs b/framework/base/src/types/interaction/tx_call_te.rs index b7a97a93d1..792757eb07 100644 --- a/framework/base/src/types/interaction/tx_call_te.rs +++ b/framework/base/src/types/interaction/tx_call_te.rs @@ -49,4 +49,15 @@ where pub fn transfer(self) { self.transfer_execute_with_gas(0) } + + /// Transfers funds, amount is greater than zero. Does nothing otherwise. + /// + /// Can only used for simple transfers. + pub fn transfer_non_zero(self) { + if self.payment.is_no_payment() { + return; + } + + self.transfer(); + } } From 48e8f36c35bd9b38593f2e6f207a9f8e2d342eb7 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Mar 2024 19:44:22 +0200 Subject: [PATCH 2/2] send wrapper - send multi esdt via unified syntax --- .../contract_base/wrappers/send_wrapper.rs | 21 ++++--------- framework/base/src/types/interaction/tx.rs | 16 ++++++++++ .../base/src/types/interaction/tx_call_te.rs | 2 +- .../base/src/types/interaction/tx_payment.rs | 30 +++++++++++++++++-- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/framework/base/src/contract_base/wrappers/send_wrapper.rs b/framework/base/src/contract_base/wrappers/send_wrapper.rs index ec8fd41c5b..d73074f52d 100644 --- a/framework/base/src/contract_base/wrappers/send_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_wrapper.rs @@ -75,10 +75,7 @@ where /// Used especially for sending EGLD to regular accounts. #[inline] pub fn direct_egld(&self, to: &ManagedAddress, amount: &BigUint) { - Tx::new_tx_from_sc() - .to(to) - .egld(amount) - .transfer(); + Tx::new_tx_from_sc().to(to).egld(amount).transfer(); } /// Sends EGLD to a given address, directly. @@ -86,10 +83,7 @@ where /// /// If the amount is 0, it returns without error. pub fn direct_non_zero_egld(&self, to: &ManagedAddress, amount: &BigUint) { - Tx::new_tx_from_sc() - .to(to) - .egld(amount) - .transfer_non_zero(); + Tx::new_tx_from_sc().to(to).egld(amount).transfer_non_zero(); } /// Sends either EGLD, ESDT or NFT to the target address, @@ -293,13 +287,10 @@ where to: &ManagedAddress, payments: &ManagedVec>, ) { - let _ = self.send_raw_wrapper().multi_esdt_transfer_execute( - to, - payments, - 0, - &ManagedBuffer::new(), - &ManagedArgBuffer::new(), - ); + Tx::new_tx_from_sc() + .to(to) + .multi_esdt_ref(payments) + .transfer(); } /// Performs a simple ESDT/NFT transfer, but via async call. diff --git a/framework/base/src/types/interaction/tx.rs b/framework/base/src/types/interaction/tx.rs index 8fae065026..d7388dc250 100644 --- a/framework/base/src/types/interaction/tx.rs +++ b/framework/base/src/types/interaction/tx.rs @@ -220,6 +220,22 @@ where } } + /// Sets a reference to multiple ESDT payments. + pub fn multi_esdt_ref( + self, + payments: &MultiEsdtPayment, + ) -> Tx, Gas, Data, RH> { + Tx { + env: self.env, + from: self.from, + to: self.to, + payment: payments, + 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_call_te.rs b/framework/base/src/types/interaction/tx_call_te.rs index 792757eb07..938a6c2523 100644 --- a/framework/base/src/types/interaction/tx_call_te.rs +++ b/framework/base/src/types/interaction/tx_call_te.rs @@ -51,7 +51,7 @@ where } /// Transfers funds, amount is greater than zero. Does nothing otherwise. - /// + /// /// Can only used for simple transfers. pub fn transfer_non_zero(self) { if self.payment.is_no_payment() { diff --git a/framework/base/src/types/interaction/tx_payment.rs b/framework/base/src/types/interaction/tx_payment.rs index beca0172f0..c3cc29fa9d 100644 --- a/framework/base/src/types/interaction/tx_payment.rs +++ b/framework/base/src/types/interaction/tx_payment.rs @@ -118,7 +118,7 @@ where } } -impl TxPayment for MultiEsdtPayment +impl TxPayment for &MultiEsdtPayment where Env: TxEnv, { @@ -135,13 +135,39 @@ where ) { let _ = SendRawWrapper::::new().multi_esdt_transfer_execute( to, - &self, + 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,