From 17552ef45f1acee2f9815d38316b6be3c544b32a Mon Sep 17 00:00:00 2001 From: Jakub Date: Thu, 11 Jan 2024 13:40:37 +0100 Subject: [PATCH] Pool stable: Remove deprecated implementations --- contracts/pool_stable/src/contract.rs | 139 +------------------------- contracts/pool_stable/src/storage.rs | 89 ----------------- 2 files changed, 1 insertion(+), 227 deletions(-) diff --git a/contracts/pool_stable/src/contract.rs b/contracts/pool_stable/src/contract.rs index 9aa01386f..bb3272537 100644 --- a/contracts/pool_stable/src/contract.rs +++ b/contracts/pool_stable/src/contract.rs @@ -1,7 +1,5 @@ use soroban_sdk::{contract, contractimpl, contractmeta, log, Address, BytesN, Env, IntoVal}; -use num_integer::Roots; - use crate::storage::utils::{is_initialized, set_initialized}; use crate::storage::StableLiquidityPoolInfo; use crate::{ @@ -15,7 +13,7 @@ use crate::{ }; use decimal::Decimal; use phoenix::{ - utils::{assert_approx_ratio, StakeInitInfo, TokenInitInfo}, + utils::{StakeInitInfo, TokenInitInfo}, validate_int_parameters, }; @@ -660,141 +658,6 @@ fn do_swap( return_amount } -/// This function divides the deposit in such a way that when swapping it for the other token, -/// the resulting amounts of tokens maintain the current pool's ratio. -/// * `a_pool` - The current amount of Token A in the liquidity pool. -/// * `b_pool` - The current amount of Token B in the liquidity pool. -/// * `deposit` - The total amount of tokens that the user wants to deposit into the liquidity pool. -/// * `sell_a` - A boolean that indicates whether the deposit is in Token A (if true) or in Token B (if false). -/// # Returns -/// * A tuple `(final_offer_amount, final_ask_amount)`, where `final_offer_amount` is the amount of deposit tokens -/// to be swapped, and `final_ask_amount` is the amount of the other tokens that will be received in return. -fn split_deposit_based_on_pool_ratio( - env: &Env, - config: &Config, - a_pool: i128, - b_pool: i128, - deposit: i128, - offer_asset: &Address, -) -> (i128, i128) { - // Validate the inputs - if a_pool <= 0 || b_pool <= 0 || deposit <= 0 { - log!(env, "Both pools and deposit must be a positive!"); - panic!( - "Pool: split_deposit_based_on_pool_ratio: Both pools and deposit must be a positive!" - ); - } - - // Calculate the current ratio in the pool - let target_ratio = Decimal::from_ratio(b_pool, a_pool); - // Define boundaries for binary search algorithm - let mut low = 0; - let mut high = deposit; - - // Tolerance is the smallest difference in deposit that we care about - let tolerance = 500; - - let mut final_offer_amount = deposit; // amount of deposit tokens to be swapped - let mut final_ask_amount = 0; // amount of other tokens to be received - - while high - low > tolerance { - let mid = (low + high) / 2; // Calculate middle point - - // Simulate swap to get amount of other tokens to be received for `mid` amount of deposit tokens - let SimulateSwapResponse { - ask_amount, - spread_amount: _, - commission_amount: _, - total_return: _, - } = StableLiquidityPool::simulate_swap(env.clone(), offer_asset.clone(), mid); - - // Update final amounts - final_offer_amount = mid; - final_ask_amount = ask_amount; - - // Calculate the ratio that would result from swapping `mid` deposit tokens - let ratio = if offer_asset == &config.token_a { - Decimal::from_ratio(ask_amount, deposit - mid) - } else { - Decimal::from_ratio(deposit - mid, ask_amount) - }; - - // If the resulting ratio is approximately equal (1%) to the target ratio, break the loop - if assert_approx_ratio(ratio, target_ratio, Decimal::percent(1)) { - break; - } - // Update boundaries for the next iteration of the binary search - if ratio > target_ratio { - if offer_asset == &config.token_a { - high = mid; - } else { - low = mid; - } - } else if offer_asset == &config.token_a { - low = mid; - } else { - high = mid; - }; - } - (final_offer_amount, final_ask_amount) -} - -/// This function asserts that the slippage does not exceed the provided tolerance. -/// # Arguments -/// * `slippage_tolerance` - An optional user-provided slippage tolerance as basis points. -/// * `deposits` - The amounts of tokens that the user deposits into each of the two pools. -/// * `pools` - The amounts of tokens in each of the two pools before the deposit. -/// * `max_allowed_slippage` - The maximum allowed slippage as a decimal. -/// # Returns -/// * An error if the slippage exceeds the tolerance or if the tolerance itself exceeds the maximum allowed, -/// otherwise Ok. -#[allow(dead_code)] -fn assert_slippage_tolerance( - env: &Env, - slippage_tolerance: Option, - deposits: &[i128; 2], - pools: &[i128; 2], - max_allowed_slippage: Decimal, -) { - let default_slippage = Decimal::percent(1); // Representing 1% as the default slippage tolerance - - // If user provided a slippage tolerance, convert it from basis points to a decimal - // Otherwise, use the default slippage tolerance - let slippage_tolerance = if let Some(slippage_tolerance) = slippage_tolerance { - Decimal::bps(slippage_tolerance) - } else { - default_slippage - }; - if slippage_tolerance > max_allowed_slippage { - log!(env, "Slippage tolerance exceeds the maximum allowed value"); - panic!( - "Pool: Assert slippage tolerance: slippage tolerance exceeds the maximum allowed value" - ); - } - - // Calculate the limit below which the deposit-to-pool ratio must not fall for each token - let one_minus_slippage_tolerance = Decimal::one() - slippage_tolerance; - let deposits: [i128; 2] = [deposits[0], deposits[1]]; - let pools: [i128; 2] = [pools[0], pools[1]]; - - // Ensure each price does not change more than what the slippage tolerance allows - if deposits[0] * pools[1] * one_minus_slippage_tolerance - > deposits[1] * pools[0] * Decimal::one() - || deposits[1] * pools[0] * one_minus_slippage_tolerance - > deposits[0] * pools[1] * Decimal::one() - { - log!( - env, - "Slippage tolerance violated. Deposits: 0: {} 1: {}, Pools: 0: {} 1: {}", - deposits[0], - deposits[1], - pools[0], - pools[1] - ); - panic!("Pool: Assert slippage tolerance: slippage tolerance violated"); - } -} - /// This function asserts that the spread (slippage) does not exceed a given maximum. /// * `belief_price` - An optional user-provided belief price, i.e., the expected price per token. /// * `max_spread` - The maximum allowed spread (slippage) as a fraction of the return amount. diff --git a/contracts/pool_stable/src/storage.rs b/contracts/pool_stable/src/storage.rs index 44eabb10e..24f611523 100644 --- a/contracts/pool_stable/src/storage.rs +++ b/contracts/pool_stable/src/storage.rs @@ -228,95 +228,6 @@ pub mod utils { token_contract::Client::new(e, contract).balance(&e.current_contract_address()) } - #[allow(clippy::too_many_arguments)] - pub fn get_deposit_amounts( - env: &Env, - desired_a: i128, - min_a: Option, - desired_b: i128, - min_b: Option, - pool_balance_a: i128, - pool_balance_b: i128, - allowed_slippage: Decimal, - ) -> (i128, i128) { - if pool_balance_a == 0 && pool_balance_b == 0 { - return (desired_a, desired_b); - } - - if let Some(min_a) = min_a { - if min_a > desired_a { - panic!("Pool: Get deposit amounts: min_a > desired_a"); - } - } - if let Some(min_b) = min_b { - if min_b > desired_b { - panic!("Pool: Get deposit amounts: min_b > desired_b"); - } - } - - let amount_a = { - let mut amount_a = desired_b * pool_balance_a / pool_balance_b; - if amount_a > desired_a { - // If the amount is within the desired amount of slippage, we accept it - if Decimal::from_ratio(amount_a, desired_a) - Decimal::one() <= allowed_slippage { - amount_a = desired_a; - } else { - log!( - env, - "Deposit amount for asset A ({}) is invalid. It exceeds the desired amount ({})", - amount_a, - desired_a, - ); - panic!("Pool: Get deposit amounts: amount_a > desired_a"); - } - }; - if let Some(min_a) = min_a { - if amount_a < min_a { - log!( - env, - "Deposit amount for asset A ({}) is invalid. It falls below the minimum requirement ({})", - amount_a, - min_a - ); - panic!("Pool: Get deposit amounts: amount_a < min_a"); - } - } - amount_a - }; - - let amount_b = { - let mut amount_b = desired_a * pool_balance_b / pool_balance_a; - if amount_b > desired_b { - // If the amount is within 1% of the desired amount, we accept it - if Decimal::from_ratio(amount_b, desired_b) - Decimal::one() <= allowed_slippage { - amount_b = desired_b; - } else { - log!( - env, - "Deposit amount for asset B ({}) is invalid. It exceeds the desired amount ({})", - amount_b, - desired_b, - ); - panic!("Pool: Get deposit amounts: amount_b > desired_b"); - } - }; - if let Some(min_b) = min_b { - if amount_b < min_b { - log!( - env, - "Deposit amount for asset B ({}) is invalid. It falls below the minimum requirement ({})", - amount_b, - min_b - ); - panic!("Pool: Get deposit amounts: amount_b < min_b"); - } - } - amount_b - }; - - (amount_a, amount_b) - } - pub fn is_initialized(e: &Env) -> bool { e.storage() .persistent()