Skip to content

Commit

Permalink
Merge pull request #1494 from multiversx/uegld-replacement
Browse files Browse the repository at this point in the history
Replacing direct transfers with unified syntax
  • Loading branch information
andrei-marinica authored Mar 22, 2024
2 parents e2dc435 + 6100308 commit 0c234ab
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 32 deletions.
12 changes: 8 additions & 4 deletions contracts/examples/crypto-kitties/kitty-auction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(_) => {
Expand Down
4 changes: 2 additions & 2 deletions contracts/examples/crypto-kitties/kitty-ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion contracts/examples/crypto-zombies/src/zombie_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/digital-cash/src/digital_cash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/digital-cash/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 8 additions & 4 deletions contracts/examples/digital-cash/src/signature_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions contracts/feature-tests/composability/forwarder/src/esdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ 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().to(&to).multi_esdt(all_token_payments).transfer();
}

#[payable("EGLD")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/modules/src/default_issue_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
7 changes: 4 additions & 3 deletions contracts/modules/src/esdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ pub trait EsdtModule {
// 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);
}
self.tx()
.to(&initial_caller)
.egld(&*egld_returned)
.transfer_if_not_empty();
},
}
}
Expand Down
5 changes: 4 additions & 1 deletion framework/base/src/contract_base/wrappers/send_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ where
///
/// If the amount is 0, it returns without error.
pub fn direct_non_zero_egld(&self, to: &ManagedAddress<A>, amount: &BigUint<A>) {
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,
Expand Down
4 changes: 2 additions & 2 deletions framework/base/src/types/interaction/tx_call_te.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 0c234ab

Please sign in to comment.