From e8ce4666fe9602c9a4e15ef40c5be3e5cdd685c4 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 22 Mar 2024 15:58:06 +0200 Subject: [PATCH 1/4] unified sytax - remove direct_call usage in contracts --- .../examples/crypto-kitties/kitty-auction/src/lib.rs | 12 ++++++++---- .../crypto-kitties/kitty-ownership/src/lib.rs | 4 ++-- .../examples/crypto-zombies/src/zombie_helper.rs | 5 ++++- contracts/examples/digital-cash/src/digital_cash.rs | 2 +- contracts/examples/digital-cash/src/helpers.rs | 2 +- .../digital-cash/src/signature_operations.rs | 12 ++++++++---- .../nft-storage-prepay/src/nft_storage_prepay.rs | 4 ++-- .../src/bonding_curve/utils/owner_endpoints.rs | 2 +- contracts/modules/src/default_issue_callbacks.rs | 6 +++--- contracts/modules/src/esdt.rs | 6 +++--- 10 files changed, 33 insertions(+), 22 deletions(-) diff --git a/contracts/examples/crypto-kitties/kitty-auction/src/lib.rs b/contracts/examples/crypto-kitties/kitty-auction/src/lib.rs index af7c7c1736..a6bf8ae782 100644 --- a/contracts/examples/crypto-kitties/kitty-auction/src/lib.rs +++ b/contracts/examples/crypto-kitties/kitty-auction/src/lib.rs @@ -183,8 +183,10 @@ pub trait KittyAuction { // refund losing bid if !auction.current_winner.is_zero() { - self.send() - .direct_egld(&auction.current_winner, &auction.current_bid); + self.tx() + .to(&auction.current_winner) + .egld(&auction.current_bid) + .transfer(); } // update auction bid and winner @@ -356,8 +358,10 @@ pub trait KittyAuction { if auction.kitty_owner != self.blockchain().get_sc_address() && !auction.current_winner.is_zero() { - self.send() - .direct_egld(&auction.kitty_owner, &auction.current_bid); + self.tx() + .to(&auction.kitty_owner) + .egld(&auction.current_bid) + .transfer(); } }, ManagedAsyncCallResult::Err(_) => { diff --git a/contracts/examples/crypto-kitties/kitty-ownership/src/lib.rs b/contracts/examples/crypto-kitties/kitty-ownership/src/lib.rs index 2514592465..21eadff6d9 100644 --- a/contracts/examples/crypto-kitties/kitty-ownership/src/lib.rs +++ b/contracts/examples/crypto-kitties/kitty-ownership/src/lib.rs @@ -52,7 +52,7 @@ pub trait KittyOwnership { .blockchain() .get_sc_balance(&EgldOrEsdtTokenIdentifier::egld(), 0); - self.send().direct_egld(&caller, &egld_balance); + self.tx().to(&caller).egld(&egld_balance).transfer(); } // views/endpoints - ERC721 required @@ -569,7 +569,7 @@ pub trait KittyOwnership { // send birth fee to caller let fee = self.birth_fee().get(); - self.send().direct_egld(&original_caller, &fee); + self.tx().to(&original_caller).egld(&fee).transfer(); }, ManagedAsyncCallResult::Err(_) => { // this can only fail if the kitty_genes contract address is invalid diff --git a/contracts/examples/crypto-zombies/src/zombie_helper.rs b/contracts/examples/crypto-zombies/src/zombie_helper.rs index 000ffda770..c38edc9ea0 100644 --- a/contracts/examples/crypto-zombies/src/zombie_helper.rs +++ b/contracts/examples/crypto-zombies/src/zombie_helper.rs @@ -31,7 +31,10 @@ pub trait ZombieHelper: storage::Storage { fn withdraw(&self) { let caller_address = self.blockchain().get_caller(); let collected_fees = self.collected_fees().get(); - self.send().direct_egld(&caller_address, &collected_fees); + self.tx() + .to(&caller_address) + .egld(&collected_fees) + .transfer(); self.collected_fees().clear(); } diff --git a/contracts/examples/digital-cash/src/digital_cash.rs b/contracts/examples/digital-cash/src/digital_cash.rs index 052faca2ef..e1384d1742 100644 --- a/contracts/examples/digital-cash/src/digital_cash.rs +++ b/contracts/examples/digital-cash/src/digital_cash.rs @@ -55,7 +55,7 @@ pub trait DigitalCash: continue; } if token == EgldOrEsdtTokenIdentifier::egld() { - self.send().direct_egld(&caller_address, &fee); + self.tx().to(&caller_address).egld(&fee).transfer(); } else { let collected_fee = EsdtTokenPayment::new(token.unwrap_esdt(), 0, fee); collected_esdt_fees.push(collected_fee); diff --git a/contracts/examples/digital-cash/src/helpers.rs b/contracts/examples/digital-cash/src/helpers.rs index d300e35d08..eef0b641f1 100644 --- a/contracts/examples/digital-cash/src/helpers.rs +++ b/contracts/examples/digital-cash/src/helpers.rs @@ -9,7 +9,7 @@ use crate::{ pub trait HelpersModule: storage::StorageModule { fn send_fee_to_address(&self, fee: &EgldOrEsdtTokenPayment, address: &ManagedAddress) { if fee.token_identifier == EgldOrEsdtTokenIdentifier::egld() { - self.send().direct_egld(address, &fee.amount); + self.tx().to(address).egld(&fee.amount).transfer(); } else { let esdt_fee = fee.clone().unwrap_esdt(); self.send() diff --git a/contracts/examples/digital-cash/src/signature_operations.rs b/contracts/examples/digital-cash/src/signature_operations.rs index 3da7143f88..5a60851143 100644 --- a/contracts/examples/digital-cash/src/signature_operations.rs +++ b/contracts/examples/digital-cash/src/signature_operations.rs @@ -33,8 +33,10 @@ pub trait SignatureOperationsModule: storage::StorageModule + helpers::HelpersMo } if egld_funds > 0 { - self.send() - .direct_egld(&deposit.depositor_address, &egld_funds); + self.tx() + .to(&deposit.depositor_address) + .egld(&egld_funds) + .transfer(); } if !esdt_funds.is_empty() { @@ -71,8 +73,10 @@ pub trait SignatureOperationsModule: storage::StorageModule + helpers::HelpersMo .update(|collected_fees| *collected_fees += fee_cost); if deposit.egld_funds > 0 { - self.send() - .direct_egld(&caller_address, &deposit.egld_funds); + self.tx() + .to(&caller_address) + .egld(&deposit.egld_funds) + .transfer(); } if !deposit.esdt_funds.is_empty() { self.send() diff --git a/contracts/examples/nft-storage-prepay/src/nft_storage_prepay.rs b/contracts/examples/nft-storage-prepay/src/nft_storage_prepay.rs index f21dfcb61b..7e1c6115d3 100644 --- a/contracts/examples/nft-storage-prepay/src/nft_storage_prepay.rs +++ b/contracts/examples/nft-storage-prepay/src/nft_storage_prepay.rs @@ -42,7 +42,7 @@ pub trait NftStoragePrepay { self.total_reserved().clear(); let owner = self.blockchain().get_caller(); - self.send().direct_egld(&owner, &total_reserved); + self.tx().to(&owner).egld(&total_reserved).transfer(); } // endpoints @@ -71,7 +71,7 @@ pub trait NftStoragePrepay { user_deposit -= &amount; self.deposit(&caller).set(&user_deposit); - self.send().direct_egld(&caller, &amount); + self.tx().to(&caller).egld(&amount).transfer(); } // views diff --git a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs index 51f5eaaa35..007eda5fb7 100644 --- a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs +++ b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs @@ -181,7 +181,7 @@ pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { self.owned_tokens(&caller).clear(); self.send().direct_multi(&caller, &tokens_to_claim); if egld_to_claim > BigUint::zero() { - self.send().direct_egld(&caller, &egld_to_claim); + self.tx().to(&caller).egld(&egld_to_claim).transfer(); } } diff --git a/contracts/modules/src/default_issue_callbacks.rs b/contracts/modules/src/default_issue_callbacks.rs index 7c0d096b2e..6edd7abeec 100644 --- a/contracts/modules/src/default_issue_callbacks.rs +++ b/contracts/modules/src/default_issue_callbacks.rs @@ -46,9 +46,9 @@ pub trait DefaultIssueCallbacksModule { } fn return_failed_issue_funds(&self, initial_caller: ManagedAddress) { - let egld_returned = self.call_value().egld_value(); - if *egld_returned > 0u32 { - self.send().direct_egld(&initial_caller, &egld_returned); + let egld_returned = self.call_value().egld_value().to_u64().unwrap(); + if egld_returned > 0u64 { + self.tx().to(&initial_caller).egld(egld_returned).transfer(); } } } diff --git a/contracts/modules/src/esdt.rs b/contracts/modules/src/esdt.rs index 18f43b896f..a61ba67a3f 100644 --- a/contracts/modules/src/esdt.rs +++ b/contracts/modules/src/esdt.rs @@ -63,9 +63,9 @@ pub trait EsdtModule { ManagedAsyncCallResult::Err(_) => { // return payment to initial caller let initial_caller = self.blockchain().get_owner_address(); - let egld_returned = self.call_value().egld_value(); - if *egld_returned > 0u32 { - self.send().direct_egld(&initial_caller, &egld_returned); + let egld_returned = self.call_value().egld_value().to_u64().unwrap(); + if egld_returned > 0u64 { + self.tx().to(&initial_caller).egld(egld_returned).transfer(); } }, } From c374bfc9f6db1db5c106e73b096cfe848824c633 Mon Sep 17 00:00:00 2001 From: BiancaIalangi Date: Fri, 22 Mar 2024 16:54:50 +0200 Subject: [PATCH 2/4] unified syntax - add transfer_non_zero and replace multi_esdt_transfer_execute --- .../composability/forwarder/src/esdt.rs | 12 +++++------- framework/base/src/types/interaction/tx_call_te.rs | 8 ++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/contracts/feature-tests/composability/forwarder/src/esdt.rs b/contracts/feature-tests/composability/forwarder/src/esdt.rs index ae9e366720..c178749ba9 100644 --- a/contracts/feature-tests/composability/forwarder/src/esdt.rs +++ b/contracts/feature-tests/composability/forwarder/src/esdt.rs @@ -73,13 +73,11 @@ pub trait ForwarderEsdtModule: storage::ForwarderStorageModule { all_token_payments.push(payment); } - let _ = self.send_raw().multi_esdt_transfer_execute( - &to, - &all_token_payments, - self.blockchain().get_gas_left(), - &ManagedBuffer::new(), - &ManagedArgBuffer::new(), - ); + self.tx() + .with_gas_limit(self.blockchain().get_gas_left()) + .to(&to) + .multi_esdt(all_token_payments) + .transfer_execute(); } #[payable("EGLD")] diff --git a/framework/base/src/types/interaction/tx_call_te.rs b/framework/base/src/types/interaction/tx_call_te.rs index b7a97a93d1..96eb1a72fb 100644 --- a/framework/base/src/types/interaction/tx_call_te.rs +++ b/framework/base/src/types/interaction/tx_call_te.rs @@ -49,4 +49,12 @@ where pub fn transfer(self) { self.transfer_execute_with_gas(0) } + + pub fn transfer_non_zero(self) { + if self.payment.is_no_payment() { + return; + } + + self.transfer_execute_with_gas(0) + } } From f0b1bb03e2bf07003af45d3eee5b2b131883f8f0 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Mar 2024 23:01:33 +0200 Subject: [PATCH 3/4] transfer cleanup --- contracts/feature-tests/composability/forwarder/src/esdt.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/contracts/feature-tests/composability/forwarder/src/esdt.rs b/contracts/feature-tests/composability/forwarder/src/esdt.rs index c178749ba9..214b9dd9ea 100644 --- a/contracts/feature-tests/composability/forwarder/src/esdt.rs +++ b/contracts/feature-tests/composability/forwarder/src/esdt.rs @@ -73,11 +73,7 @@ pub trait ForwarderEsdtModule: storage::ForwarderStorageModule { all_token_payments.push(payment); } - self.tx() - .with_gas_limit(self.blockchain().get_gas_left()) - .to(&to) - .multi_esdt(all_token_payments) - .transfer_execute(); + self.tx().to(&to).multi_esdt(all_token_payments).transfer(); } #[payable("EGLD")] From 6100308e7f5b5c171f1a87967a853e6b62666e57 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Fri, 22 Mar 2024 23:07:34 +0200 Subject: [PATCH 4/4] transfer cleanup --- contracts/modules/src/esdt.rs | 9 +++++---- .../base/src/contract_base/wrappers/send_wrapper.rs | 5 ++++- framework/base/src/types/interaction/tx_call_te.rs | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/contracts/modules/src/esdt.rs b/contracts/modules/src/esdt.rs index a61ba67a3f..fa02cfd263 100644 --- a/contracts/modules/src/esdt.rs +++ b/contracts/modules/src/esdt.rs @@ -63,10 +63,11 @@ pub trait EsdtModule { ManagedAsyncCallResult::Err(_) => { // return payment to initial caller let initial_caller = self.blockchain().get_owner_address(); - let egld_returned = self.call_value().egld_value().to_u64().unwrap(); - if egld_returned > 0u64 { - self.tx().to(&initial_caller).egld(egld_returned).transfer(); - } + let egld_returned = self.call_value().egld_value(); + self.tx() + .to(&initial_caller) + .egld(&*egld_returned) + .transfer_if_not_empty(); }, } } diff --git a/framework/base/src/contract_base/wrappers/send_wrapper.rs b/framework/base/src/contract_base/wrappers/send_wrapper.rs index d73074f52d..aa0c837fde 100644 --- a/framework/base/src/contract_base/wrappers/send_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_wrapper.rs @@ -83,7 +83,10 @@ 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_if_not_empty(); } /// 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 938a6c2523..5cb81a6f0f 100644 --- a/framework/base/src/types/interaction/tx_call_te.rs +++ b/framework/base/src/types/interaction/tx_call_te.rs @@ -50,10 +50,10 @@ where self.transfer_execute_with_gas(0) } - /// Transfers funds, amount is greater than zero. Does nothing otherwise. + /// Transfers funds, if amount is greater than zero. Does nothing otherwise. /// /// Can only used for simple transfers. - pub fn transfer_non_zero(self) { + pub fn transfer_if_not_empty(self) { if self.payment.is_no_payment() { return; }