balancer_maths_rust/pools/gyro/
gyro_eclp_pool.rs1use crate::common::errors::PoolError;
2use crate::common::pool_base::PoolBase;
3use crate::common::types::{Rounding, SwapKind, SwapParams};
4use crate::pools::gyro::gyro_eclp_data::GyroECLPImmutable;
5use crate::pools::gyro::gyro_eclp_math::{
6 calc_in_given_out, calc_out_given_in, calculate_invariant_with_error, compute_balance,
7 DerivedEclpParams, EclpParams, Vector2,
8};
9use alloy_primitives::{I256, U256};
10
11pub struct GyroECLPPool {
13 pub params: EclpParams,
14 pub derived: DerivedEclpParams,
15}
16
17impl GyroECLPPool {
18 pub fn new(immutable: GyroECLPImmutable) -> Self {
20 let params = EclpParams {
21 alpha: immutable.alpha,
22 beta: immutable.beta,
23 c: immutable.c,
24 s: immutable.s,
25 lambda: immutable.lambda,
26 };
27
28 let derived = DerivedEclpParams {
29 tau_alpha: Vector2 {
30 x: immutable.tau_alpha_x,
31 y: immutable.tau_alpha_y,
32 },
33 tau_beta: Vector2 {
34 x: immutable.tau_beta_x,
35 y: immutable.tau_beta_y,
36 },
37 u: immutable.u,
38 v: immutable.v,
39 w: immutable.w,
40 z: immutable.z,
41 d_sq: immutable.d_sq,
42 };
43
44 Self { params, derived }
45 }
46}
47
48impl PoolBase for GyroECLPPool {
49 fn get_maximum_invariant_ratio(&self) -> U256 {
50 use crate::pools::gyro::gyro_eclp_math::MAX_INVARIANT_RATIO;
51 MAX_INVARIANT_RATIO
52 }
53
54 fn get_minimum_invariant_ratio(&self) -> U256 {
55 use crate::pools::gyro::gyro_eclp_math::MIN_INVARIANT_RATIO;
56 MIN_INVARIANT_RATIO
57 }
58
59 fn on_swap(&self, swap_params: &SwapParams) -> Result<U256, PoolError> {
60 let (current_invariant, inv_err) = calculate_invariant_with_error(
62 &swap_params.balances_live_scaled_18,
63 &self.params,
64 &self.derived,
65 )?;
66
67 let invariant_x_result = current_invariant + inv_err * I256::try_from(2).unwrap();
68 let invariant = Vector2 {
69 x: invariant_x_result,
70 y: current_invariant,
71 };
72
73 let token_in_is_token0 = swap_params.token_in_index == 0;
74
75 match swap_params.swap_kind {
76 SwapKind::GivenIn => calc_out_given_in(
77 &swap_params.balances_live_scaled_18,
78 &swap_params.amount_scaled_18,
79 token_in_is_token0,
80 &self.params,
81 &self.derived,
82 &invariant,
83 ),
84 SwapKind::GivenOut => calc_in_given_out(
85 &swap_params.balances_live_scaled_18,
86 &swap_params.amount_scaled_18,
87 token_in_is_token0,
88 &self.params,
89 &self.derived,
90 &invariant,
91 ),
92 }
93 }
94
95 fn compute_invariant(
96 &self,
97 balances_live_scaled18: &[U256],
98 rounding: Rounding,
99 ) -> Result<U256, PoolError> {
100 let (current_invariant, inv_err) =
101 calculate_invariant_with_error(balances_live_scaled18, &self.params, &self.derived)?;
102 match rounding {
103 Rounding::RoundDown => Ok((current_invariant - inv_err).into_raw()),
104 Rounding::RoundUp => Ok((current_invariant + inv_err).into_raw()),
105 }
106 }
107
108 fn compute_balance(
109 &self,
110 balances_live_scaled18: &[U256],
111 token_in_index: usize,
112 invariant_ratio: &U256,
113 ) -> Result<U256, PoolError> {
114 compute_balance(
115 balances_live_scaled18,
116 token_in_index,
117 invariant_ratio,
118 &self.params,
119 &self.derived,
120 )
121 }
122}