Skip to main content

balancer_maths_rust/common/
types.rs

1//! Core types for the Balancer maths library
2
3use crate::pools::buffer::BufferState;
4use alloy_primitives::U256;
5use serde::{Deserialize, Serialize};
6
7/// Kind of swap operation
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub enum SwapKind {
10    /// Given amount in, calculate amount out
11    GivenIn = 0,
12    /// Given amount out, calculate amount in
13    GivenOut = 1,
14}
15
16/// Kind of add liquidity operation
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub enum AddLiquidityKind {
19    /// Add liquidity with specific amounts (unbalanced)
20    Unbalanced = 0,
21    /// Add liquidity with exact BPT output for single token
22    SingleTokenExactOut = 1,
23}
24
25/// Kind of remove liquidity operation
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27pub enum RemoveLiquidityKind {
28    /// Remove liquidity proportionally
29    Proportional = 0,
30    /// Remove liquidity with exact BPT input for single token
31    SingleTokenExactIn = 1,
32    /// Remove liquidity with exact token output for single token
33    SingleTokenExactOut = 2,
34}
35
36/// Input for swap operations
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub struct SwapInput {
39    /// Amount to swap (raw, not scaled)
40    pub amount_raw: U256,
41    /// Kind of swap operation
42    pub swap_kind: SwapKind,
43    /// Token address to swap from
44    pub token_in: String,
45    /// Token address to swap to
46    pub token_out: String,
47}
48
49/// Input for add liquidity operations
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub struct AddLiquidityInput {
52    /// Pool address
53    pub pool: String,
54    /// Maximum amounts to add (raw, not scaled)
55    pub max_amounts_in_raw: Vec<U256>,
56    /// Minimum BPT amount to receive
57    pub min_bpt_amount_out_raw: U256,
58    /// Kind of add liquidity operation
59    pub kind: AddLiquidityKind,
60}
61
62/// Input for remove liquidity operations
63#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct RemoveLiquidityInput {
65    /// Pool address
66    pub pool: String,
67    /// Minimum amounts to receive (raw, not scaled)
68    pub min_amounts_out_raw: Vec<U256>,
69    /// Maximum BPT amount to burn
70    pub max_bpt_amount_in_raw: U256,
71    /// Kind of remove liquidity operation
72    pub kind: RemoveLiquidityKind,
73}
74
75/// Base pool state shared by all pool types
76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
77pub struct BasePoolState {
78    /// Pool address
79    pub pool_address: String,
80    /// Pool type (e.g., "WEIGHTED", "STABLE", etc.)
81    pub pool_type: String,
82    /// Token addresses
83    pub tokens: Vec<String>,
84    /// Scaling factors for each token
85    pub scaling_factors: Vec<U256>,
86    /// Token rates (scaled 18)
87    pub token_rates: Vec<U256>,
88    /// Balances (scaled 18)
89    pub balances_live_scaled_18: Vec<U256>,
90    /// Swap fee (scaled 18)
91    pub swap_fee: U256,
92    /// Aggregate swap fee (scaled 18)
93    pub aggregate_swap_fee: U256,
94    /// Total supply (scaled 18)
95    pub total_supply: U256,
96    /// Whether pool supports unbalanced liquidity
97    pub supports_unbalanced_liquidity: bool,
98    /// Optional hook type
99    pub hook_type: Option<String>,
100}
101
102/// Pool state - can be any specific pool type
103#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum PoolState {
106    /// Base pool state
107    Base(BasePoolState),
108    /// Weighted pool state
109    Weighted(crate::pools::weighted::WeightedState),
110    /// Stable pool state
111    Stable(crate::pools::stable::stable_data::StableState),
112
113    /// Gyro ECLP pool state
114    GyroECLP(crate::pools::gyro::gyro_eclp_data::GyroECLPState),
115    /// ReClamm pool state
116    ReClamm(crate::pools::reclamm::reclamm_data::ReClammState),
117    /// ReClammV2 pool state
118    ReClammV2(crate::pools::reclammv2::reclammv2_data::ReClammV2State),
119    /// QuantAMM pool state
120    QuantAmm(crate::pools::quantamm::quantamm_data::QuantAmmState),
121    /// Liquidity bootstrapping pool state
122    LiquidityBootstrapping(crate::pools::liquidity_bootstrapping::liquidity_bootstrapping_data::LiquidityBootstrappingState),
123    /// FixedPriceLBP pool state
124    FixedPriceLBP(crate::pools::fixed_price_lbp::fixed_price_lbp_data::FixedPriceLBPState),
125}
126
127/// Union type for pool states - can be either a normal pool or a buffer pool
128#[derive(Debug, Clone)]
129pub enum PoolStateOrBuffer {
130    Pool(Box<PoolState>),
131    Buffer(Box<BufferState>),
132}
133
134/// Result of a swap operation
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub struct SwapResult {
137    /// Amount out (raw, not scaled)
138    pub amount_out_raw: U256,
139    /// Fee amount (raw, not scaled)
140    pub fee_amount_raw: U256,
141}
142
143/// Result of an add liquidity operation
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub struct AddLiquidityResult {
146    /// BPT amount minted (raw, not scaled)
147    pub bpt_amount_out_raw: U256,
148    /// Amounts added (raw, not scaled)
149    pub amounts_in_raw: Vec<U256>,
150}
151
152/// Result of a remove liquidity operation
153#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
154pub struct RemoveLiquidityResult {
155    /// BPT amount burned (raw, not scaled)
156    pub bpt_amount_in_raw: U256,
157    /// Amounts removed (raw, not scaled)
158    pub amounts_out_raw: Vec<U256>,
159}
160
161/// Swap parameters
162#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
163pub struct SwapParams {
164    /// Swap kind
165    pub swap_kind: SwapKind,
166    /// Token in index
167    pub token_in_index: usize,
168    /// Token out index
169    pub token_out_index: usize,
170    /// Amount (scaled 18)
171    pub amount_scaled_18: U256,
172    /// Balances (scaled 18)
173    pub balances_live_scaled_18: Vec<U256>,
174}
175
176/// Base hook state trait
177pub trait HookStateBase {
178    fn hook_type(&self) -> &str;
179}
180
181/// Rounding direction for mathematical operations
182#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
183pub enum Rounding {
184    RoundDown = 0,
185    RoundUp = 1,
186}
187
188impl PoolState {
189    /// Get the base pool state
190    pub fn base(&self) -> &BasePoolState {
191        match self {
192            PoolState::Base(base) => base,
193            PoolState::Weighted(weighted) => weighted.base(),
194            PoolState::Stable(stable) => &stable.base,
195
196            PoolState::GyroECLP(gyro_eclp) => &gyro_eclp.base,
197            PoolState::ReClamm(re_clamm) => &re_clamm.base,
198            PoolState::ReClammV2(re_clamm_v2) => &re_clamm_v2.base,
199            PoolState::QuantAmm(quant_amm) => &quant_amm.base,
200            PoolState::LiquidityBootstrapping(liquidity_bootstrapping) => {
201                &liquidity_bootstrapping.base
202            }
203            PoolState::FixedPriceLBP(fixed_price_lbp) => &fixed_price_lbp.base,
204        }
205    }
206
207    /// Get the pool type
208    pub fn pool_type(&self) -> &str {
209        &self.base().pool_type
210    }
211
212    /// Get the pool address
213    pub fn pool_address(&self) -> &str {
214        &self.base().pool_address
215    }
216}