Skip to content

Commit

Permalink
multi-transfer: refactor batch_transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
CostinCarabas committed Dec 12, 2024
1 parent 7db4577 commit 247fb5b
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ pub trait MultiTransferEsdt:
.blockchain()
.get_esdt_local_roles(&eth_tx.token_id.clone());
if token_roles.has_role(&EsdtLocalRole::Transfer) {
let refund_tx = self.convert_to_refund_tx(eth_tx.clone());
refund_tx_list.push(refund_tx);
self.token_with_transfer_role(eth_tx.token_id);
// let refund_tx = self.convert_to_refund_tx(eth_tx.clone());
// refund_tx_list.push(refund_tx);
// self.token_with_transfer_role_event(eth_tx.token_id);

self.add_eth_tx_to_refund_tx_list(eth_tx.clone(), &mut refund_tx_list);
self.token_with_transfer_role_event(eth_tx.token_id);
continue;
}

//TODO simplify this

let is_success: bool = self
.tx()
.to(safe_address.clone())
Expand All @@ -76,27 +76,26 @@ pub trait MultiTransferEsdt:
.returns(ReturnsResult)
.sync_call();

//TODO: transform require to if
require!(is_success, "Invalid token or amount");
if !is_success {
self.add_eth_tx_to_refund_tx_list(eth_tx, &mut refund_tx_list);
continue;
}

let universal_token = self.get_universal_token(eth_tx.clone());

let mut must_refund = false;
if eth_tx.to.is_zero() {
self.transfer_failed_invalid_destination(batch_id, eth_tx.tx_nonce);
must_refund = true;
} else if self.is_above_max_amount(&eth_tx.token_id, &eth_tx.amount) {
self.transfer_over_max_amount(batch_id, eth_tx.tx_nonce);
must_refund = true;
} else if self.is_account_same_shard_frozen(sc_shard, &eth_tx.to, &universal_token) {
self.transfer_failed_frozen_destination_account(batch_id, eth_tx.tx_nonce);
must_refund = true;
self.add_eth_tx_to_refund_tx_list(eth_tx.clone(), &mut refund_tx_list);
self.transfer_failed_invalid_destination_event(batch_id, eth_tx.tx_nonce);
continue;
}

if must_refund {
let refund_tx = self.convert_to_refund_tx(eth_tx);
refund_tx_list.push(refund_tx);

if self.is_above_max_amount(&eth_tx.token_id, &eth_tx.amount) {
self.add_eth_tx_to_refund_tx_list(eth_tx.clone(), &mut refund_tx_list);
self.transfer_over_max_amount_event(batch_id, eth_tx.tx_nonce);
continue;
}
if self.is_account_same_shard_frozen(sc_shard, &eth_tx.to, &universal_token) {
self.add_eth_tx_to_refund_tx_list(eth_tx.clone(), &mut refund_tx_list);
self.transfer_failed_frozen_destination_account_event(batch_id, eth_tx.tx_nonce);
continue;
}

Expand Down Expand Up @@ -190,6 +189,15 @@ pub trait MultiTransferEsdt:

// private

fn add_eth_tx_to_refund_tx_list(
&self,
eth_tx: EthTransaction<Self::Api>,
refund_tx_list: &mut ManagedVec<Transaction<Self::Api>>,
) {
let refund_tx = self.convert_to_refund_tx(eth_tx);
refund_tx_list.push(refund_tx);
}

fn is_refund_valid(&self, token_id: &TokenIdentifier) -> bool {
let esdt_safe_addr = self.get_esdt_safe_address();
let own_sc_address = self.blockchain().get_sc_address();
Expand Down Expand Up @@ -327,23 +335,27 @@ pub trait MultiTransferEsdt:
);

#[event("transferFailedInvalidDestination")]
fn transfer_failed_invalid_destination(&self, #[indexed] batch_id: u64, #[indexed] tx_id: u64);
fn transfer_failed_invalid_destination_event(
&self,
#[indexed] batch_id: u64,
#[indexed] tx_id: u64,
);

#[event("tokenWithTransferRole")]
fn token_with_transfer_role(&self, #[indexed] token_id: TokenIdentifier);
fn token_with_transfer_role_event(&self, #[indexed] token_id: TokenIdentifier);

#[event("transferFailedInvalidToken")]
fn transfer_failed_invalid_token(&self, #[indexed] batch_id: u64, #[indexed] tx_id: u64);
fn transfer_failed_invalid_token_event(&self, #[indexed] batch_id: u64, #[indexed] tx_id: u64);

#[event("transferFailedFrozenDestinationAccount")]
fn transfer_failed_frozen_destination_account(
fn transfer_failed_frozen_destination_account_event(
&self,
#[indexed] batch_id: u64,
#[indexed] tx_id: u64,
);

#[event("transferOverMaxAmount")]
fn transfer_over_max_amount(&self, #[indexed] batch_id: u64, #[indexed] tx_id: u64);
fn transfer_over_max_amount_event(&self, #[indexed] batch_id: u64, #[indexed] tx_id: u64);

#[event("unprocessedRefundTxs")]
fn unprocessed_refund_txs_event(&self, #[indexed] tx_id: u64);
Expand Down

0 comments on commit 247fb5b

Please sign in to comment.