Skip to content

Commit

Permalink
feat: short circuit zero collections
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Sep 21, 2023
1 parent 2768b4b commit 077abd6
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pallets/foreign-investments/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,16 +1054,19 @@ impl<T: Config> Pallet<T> {
collected: CollectedAmount<T::Balance>,
) -> DispatchResult {
// Increment by previously stored amounts (via `CollectedInvestmentHook`)
CollectedInvestment::<T>::mutate(who, investment_id, |collected_before| {
collected_before
.amount_collected
let nothing_collect = CollectedInvestment::<T>::mutate(who, investment_id, |c| {
c.amount_collected
.ensure_add_assign(collected.amount_collected)?;
collected_before
.amount_payment
c.amount_payment
.ensure_add_assign(collected.amount_payment)?;
Ok::<(), DispatchError>(())
Ok::<bool, DispatchError>(c.amount_collected.is_zero() && c.amount_payment.is_zero())
})?;

// No need to transition if nothing was collected
if nothing_collect {
return Ok(());
}

// Update invest state to decrease the unprocessed investing amount
let investing_amount = T::Investment::investment(who, investment_id)?;
let pre_state = InvestmentState::<T>::get(who, investment_id);
Expand Down Expand Up @@ -1098,14 +1101,19 @@ impl<T: Config> Pallet<T> {
.expect("Impossible to collect redemption for non existing pool at this point");

// Increment by previously stored amounts (via `CollectedInvestmentHook`)
CollectedRedemption::<T>::mutate(who, investment_id, |old| {
old.amount_collected
let nothing_collect = CollectedRedemption::<T>::mutate(who, investment_id, |c| {
c.amount_collected
.ensure_add_assign(collected.amount_collected)?;
old.amount_payment
c.amount_payment
.ensure_add_assign(collected.amount_payment)?;
Ok::<(), DispatchError>(())
Ok::<bool, DispatchError>(c.amount_collected.is_zero() && c.amount_payment.is_zero())
})?;

// No need to transition if nothing was collected
if nothing_collect {
return Ok(());
}

// Transition state to initiate swap from pool to foreign currency
let pre_state = RedemptionState::<T>::get(who, investment_id);
let amount_unprocessed_redemption = T::Investment::redemption(who, investment_id)?;
Expand Down

0 comments on commit 077abd6

Please sign in to comment.