From 178fa2716e1339d2ca76d27806c9d9495512df5c Mon Sep 17 00:00:00 2001 From: Frederik Gartenmeister Date: Tue, 4 Jun 2024 12:05:27 +0200 Subject: [PATCH] fix: fix wrong ratio calculation --- pallets/pool-system/src/lib.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pallets/pool-system/src/lib.rs b/pallets/pool-system/src/lib.rs index cf4bba7567..a2ddb84015 100644 --- a/pallets/pool-system/src/lib.rs +++ b/pallets/pool-system/src/lib.rs @@ -1239,15 +1239,28 @@ pub mod pallet { let executed_amounts = epoch.tranches.fulfillment_cash_flows(solution)?; let total_assets = epoch.nav.total(pool.reserve.total)?; - let tranche_ratios = epoch.tranches.combine_with_residual_top( - &executed_amounts, - |tranche, &(invest, redeem)| { - Ok(Perquintill::from_rational( - tranche.supply.ensure_add(invest)?.ensure_sub(redeem)?, - total_assets, - )) - }, - )?; + let mut tranche_ratios = epoch + .tranches + .non_residual_tranches() + .map(|tranches| { + tranches + .iter() + .zip(&executed_amounts) + .map(|(tranche, &(invest, redeem))| { + Ok(Perquintill::from_rational( + tranche.supply.ensure_add(invest)?.ensure_sub(redeem)?, + total_assets, + )) + }) + .collect::, ArithmeticError>>() + }) + .unwrap_or(Ok(Vec::new()))?; + + let non_residual_tranche_ratio_sum = tranche_ratios + .iter() + .try_fold(Perquintill::zero(), |acc, ratio| acc.ensure_add(*ratio))?; + + tranche_ratios.push(Perquintill::one().ensure_sub(non_residual_tranche_ratio_sum)?); pool.tranches.rebalance_tranches( T::Time::now(),