Skip to content

Commit

Permalink
de-nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Dec 11, 2024
1 parent 7c3ae25 commit 1e583c9
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions energy-integration/fees-collector/src/fees_accumulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,7 @@ pub trait FeesAccumulationModule:
);

if payment.token_nonce == 0 {
let opt_pair = self.get_pair(payment.token_identifier.clone());
if let Some(pair_address) = opt_pair {
let base_token_id = self.base_token_id().get();
payment = self.swap_to_common_token(pair_address, payment, base_token_id.clone());

// just a sanity check
require!(
payment.token_identifier == base_token_id,
"Wrong token received from pair"
);
}
self.try_swap_to_base_token(&mut payment);
} else {
self.burn_locked_token(&payment);
}
Expand All @@ -65,6 +55,24 @@ pub trait FeesAccumulationModule:
}
}

fn try_swap_to_base_token(&self, payment: &mut EsdtTokenPayment) {
let opt_pair = self.get_pair(payment.token_identifier.clone());
if opt_pair.is_none() {
return;
}

let pair_address = unsafe { opt_pair.unwrap_unchecked() };
let base_token_id = self.base_token_id().get();
*payment =
self.swap_to_common_token(pair_address, (*payment).clone(), base_token_id.clone());

// just a sanity check
require!(
payment.token_identifier == base_token_id,
"Wrong token received from pair"
);
}

fn burn_locked_token(&self, payment: &EsdtTokenPayment) {
require!(
payment.token_identifier == self.locked_token_id().get(),
Expand Down

0 comments on commit 1e583c9

Please sign in to comment.