balancer_maths_rust/pools/reclammv2/
reclammv2_pool.rs

1use crate::common::errors::PoolError;
2use crate::common::pool_base::PoolBase;
3use crate::common::types::{Rounding, SwapKind, SwapParams};
4use crate::pools::reclammv2::reclammv2_data::ReClammV2State;
5use crate::pools::reclammv2::reclammv2_math::{
6    compute_current_virtual_balances, compute_in_given_out, compute_out_given_in,
7};
8use alloy_primitives::U256;
9
10/// ReClammV2 pool implementation
11pub struct ReClammV2Pool {
12    re_clamm_v2_state: ReClammV2State,
13}
14
15impl ReClammV2Pool {
16    /// Create a new ReClammV2 pool instance
17    pub fn new(pool_state: ReClammV2State) -> Self {
18        Self {
19            re_clamm_v2_state: pool_state,
20        }
21    }
22
23    /// Compute current virtual balances for the pool
24    fn compute_current_virtual_balances(&self, balances_scaled_18: &[U256]) -> (U256, U256, bool) {
25        compute_current_virtual_balances(
26            &self.re_clamm_v2_state.mutable.current_timestamp,
27            balances_scaled_18,
28            &self.re_clamm_v2_state.mutable.last_virtual_balances[0],
29            &self.re_clamm_v2_state.mutable.last_virtual_balances[1],
30            &self.re_clamm_v2_state.mutable.daily_price_shift_base,
31            &self.re_clamm_v2_state.mutable.last_timestamp,
32            &self.re_clamm_v2_state.mutable.centeredness_margin,
33            &self.re_clamm_v2_state.mutable.start_fourth_root_price_ratio,
34            &self.re_clamm_v2_state.mutable.end_fourth_root_price_ratio,
35            &self.re_clamm_v2_state.mutable.price_ratio_update_start_time,
36            &self.re_clamm_v2_state.mutable.price_ratio_update_end_time,
37        )
38    }
39}
40
41impl PoolBase for ReClammV2Pool {
42    fn get_maximum_invariant_ratio(&self) -> U256 {
43        // The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
44        // only be added or removed proportionally.
45        U256::ZERO
46    }
47
48    fn get_minimum_invariant_ratio(&self) -> U256 {
49        // The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
50        // only be added or removed proportionally.
51        U256::ZERO
52    }
53
54    fn on_swap(&self, swap_params: &SwapParams) -> Result<U256, PoolError> {
55        let compute_result =
56            self.compute_current_virtual_balances(&swap_params.balances_live_scaled_18);
57
58        match swap_params.swap_kind {
59            SwapKind::GivenIn => {
60                let amount_calculated_scaled_18 = compute_out_given_in(
61                    &swap_params.balances_live_scaled_18,
62                    &compute_result.0, // current_virtual_balance_a
63                    &compute_result.1, // current_virtual_balance_b
64                    swap_params.token_in_index,
65                    swap_params.token_out_index,
66                    &swap_params.amount_scaled_18,
67                )?;
68
69                Ok(amount_calculated_scaled_18)
70            }
71            SwapKind::GivenOut => {
72                let amount_calculated_scaled_18 = compute_in_given_out(
73                    &swap_params.balances_live_scaled_18,
74                    &compute_result.0, // current_virtual_balance_a
75                    &compute_result.1, // current_virtual_balance_b
76                    swap_params.token_in_index,
77                    swap_params.token_out_index,
78                    &swap_params.amount_scaled_18,
79                )?;
80
81                Ok(amount_calculated_scaled_18)
82            }
83        }
84    }
85
86    fn compute_invariant(
87        &self,
88        _balances_live_scaled_18: &[U256],
89        _rounding: Rounding,
90    ) -> Result<U256, PoolError> {
91        // Only needed for unbalanced liquidity and that's not possible in this pool
92        Ok(U256::ZERO)
93    }
94
95    fn compute_balance(
96        &self,
97        _balances_live_scaled_18: &[U256],
98        _token_in_index: usize,
99        _invariant_ratio: &U256,
100    ) -> Result<U256, PoolError> {
101        // Only needed for unbalanced liquidity and that's not possible in this pool
102        Ok(U256::ZERO)
103    }
104}