From eab7ea1d239cfe2473d0d482bc1f3a3e22af7044 Mon Sep 17 00:00:00 2001 From: nahem Date: Tue, 28 May 2024 15:05:32 +0200 Subject: [PATCH] fix(smart-contracts): update slippage tolerance to work with N coins -> WIP --- .../liquidity_hub/pool-manager/src/helpers.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/contracts/liquidity_hub/pool-manager/src/helpers.rs b/contracts/liquidity_hub/pool-manager/src/helpers.rs index c41565e8..0eef6f5a 100644 --- a/contracts/liquidity_hub/pool-manager/src/helpers.rs +++ b/contracts/liquidity_hub/pool-manager/src/helpers.rs @@ -489,10 +489,11 @@ pub struct OfferAmountComputation { } // TODO: make this work with n_coins being dynamic + pub fn assert_slippage_tolerance( slippage_tolerance: &Option, - deposits: &[Coin], - pools: &[Coin], + deposits: &Vec, + pools: &Vec, pool_type: PoolType, amount: Uint128, pool_token_supply: Uint128, @@ -504,14 +505,19 @@ pub fn assert_slippage_tolerance( } let one_minus_slippage_tolerance = Decimal256::one() - slippage_tolerance; - let deposits: [Uint256; 2] = [deposits[0].amount.into(), deposits[1].amount.into()]; - let pools: [Uint256; 2] = [pools[0].amount.into(), pools[1].amount.into()]; + let deposits: Vec = deposits.iter().map(|coin| coin.amount.into()).collect(); + let pools: Vec = pools.iter().map(|coin| coin.amount.into()).collect(); // Ensure each prices are not dropped as much as slippage tolerance rate match pool_type { PoolType::StableSwap { .. } => { - let pools_total = pools[0].checked_add(pools[1])?; - let deposits_total = deposits[0].checked_add(deposits[1])?; + // TODO: shouldn't be necessary to handle unwraps properly as they come from Uint128, but doublecheck! + let pools_total: Uint256 = pools + .into_iter() + .fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap()); + let deposits_total: Uint256 = deposits + .into_iter() + .fold(Uint256::zero(), |acc, x| acc.checked_add(x).unwrap()); let pool_ratio = Decimal256::from_ratio(pools_total, pool_token_supply); let deposit_ratio = Decimal256::from_ratio(deposits_total, amount);