Skip to main content

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(
25        &self,
26        balances_scaled_18: &[U256],
27    ) -> Result<(U256, U256, bool), PoolError> {
28        compute_current_virtual_balances(
29            &self.re_clamm_v2_state.mutable.current_timestamp,
30            balances_scaled_18,
31            &self.re_clamm_v2_state.mutable.last_virtual_balances[0],
32            &self.re_clamm_v2_state.mutable.last_virtual_balances[1],
33            &self.re_clamm_v2_state.mutable.daily_price_shift_base,
34            &self.re_clamm_v2_state.mutable.last_timestamp,
35            &self.re_clamm_v2_state.mutable.centeredness_margin,
36            &self.re_clamm_v2_state.mutable.start_fourth_root_price_ratio,
37            &self.re_clamm_v2_state.mutable.end_fourth_root_price_ratio,
38            &self.re_clamm_v2_state.mutable.price_ratio_update_start_time,
39            &self.re_clamm_v2_state.mutable.price_ratio_update_end_time,
40        )
41    }
42}
43
44impl PoolBase for ReClammV2Pool {
45    fn get_maximum_invariant_ratio(&self) -> U256 {
46        // The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
47        // only be added or removed proportionally.
48        U256::ZERO
49    }
50
51    fn get_minimum_invariant_ratio(&self) -> U256 {
52        // The invariant ratio bounds are required by `IBasePool`, but are unused in this pool type, as liquidity can
53        // only be added or removed proportionally.
54        U256::ZERO
55    }
56
57    fn on_swap(&self, swap_params: &SwapParams) -> Result<U256, PoolError> {
58        let compute_result =
59            self.compute_current_virtual_balances(&swap_params.balances_live_scaled_18)?;
60
61        match swap_params.swap_kind {
62            SwapKind::GivenIn => {
63                let amount_calculated_scaled_18 = compute_out_given_in(
64                    &swap_params.balances_live_scaled_18,
65                    &compute_result.0, // current_virtual_balance_a
66                    &compute_result.1, // current_virtual_balance_b
67                    swap_params.token_in_index,
68                    swap_params.token_out_index,
69                    &swap_params.amount_scaled_18,
70                )?;
71
72                Ok(amount_calculated_scaled_18)
73            }
74            SwapKind::GivenOut => {
75                let amount_calculated_scaled_18 = compute_in_given_out(
76                    &swap_params.balances_live_scaled_18,
77                    &compute_result.0, // current_virtual_balance_a
78                    &compute_result.1, // current_virtual_balance_b
79                    swap_params.token_in_index,
80                    swap_params.token_out_index,
81                    &swap_params.amount_scaled_18,
82                )?;
83
84                Ok(amount_calculated_scaled_18)
85            }
86        }
87    }
88
89    fn compute_invariant(
90        &self,
91        _balances_live_scaled_18: &[U256],
92        _rounding: Rounding,
93    ) -> Result<U256, PoolError> {
94        // Only needed for unbalanced liquidity and that's not possible in this pool
95        Ok(U256::ZERO)
96    }
97
98    fn compute_balance(
99        &self,
100        _balances_live_scaled_18: &[U256],
101        _token_in_index: usize,
102        _invariant_ratio: &U256,
103    ) -> Result<U256, PoolError> {
104        // Only needed for unbalanced liquidity and that's not possible in this pool
105        Ok(U256::ZERO)
106    }
107}