balancer_maths_rust/common/
types.rs

1//! Core types for the Balancer maths library
2
3use crate::pools::buffer::BufferState;
4use num_bigint::BigInt;
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: BigInt,
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<BigInt>,
56    /// Minimum BPT amount to receive
57    pub min_bpt_amount_out_raw: BigInt,
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<BigInt>,
69    /// Maximum BPT amount to burn
70    pub max_bpt_amount_in_raw: BigInt,
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<BigInt>,
86    /// Token rates (scaled 18)
87    pub token_rates: Vec<BigInt>,
88    /// Balances (scaled 18)
89    pub balances_live_scaled_18: Vec<BigInt>,
90    /// Swap fee (scaled 18)
91    pub swap_fee: BigInt,
92    /// Aggregate swap fee (scaled 18)
93    pub aggregate_swap_fee: BigInt,
94    /// Total supply (scaled 18)
95    pub total_supply: BigInt,
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}
124
125/// Union type for pool states - can be either a normal pool or a buffer pool
126#[derive(Debug, Clone)]
127pub enum PoolStateOrBuffer {
128    Pool(Box<PoolState>),
129    Buffer(Box<BufferState>),
130}
131
132/// Result of a swap operation
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct SwapResult {
135    /// Amount out (raw, not scaled)
136    pub amount_out_raw: BigInt,
137    /// Fee amount (raw, not scaled)
138    pub fee_amount_raw: BigInt,
139}
140
141/// Result of an add liquidity operation
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
143pub struct AddLiquidityResult {
144    /// BPT amount minted (raw, not scaled)
145    pub bpt_amount_out_raw: BigInt,
146    /// Amounts added (raw, not scaled)
147    pub amounts_in_raw: Vec<BigInt>,
148}
149
150/// Result of a remove liquidity operation
151#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
152pub struct RemoveLiquidityResult {
153    /// BPT amount burned (raw, not scaled)
154    pub bpt_amount_in_raw: BigInt,
155    /// Amounts removed (raw, not scaled)
156    pub amounts_out_raw: Vec<BigInt>,
157}
158
159/// Swap parameters
160#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161pub struct SwapParams {
162    /// Swap kind
163    pub swap_kind: SwapKind,
164    /// Token in index
165    pub token_in_index: usize,
166    /// Token out index
167    pub token_out_index: usize,
168    /// Amount (scaled 18)
169    pub amount_scaled_18: BigInt,
170    /// Balances (scaled 18)
171    pub balances_live_scaled_18: Vec<BigInt>,
172}
173
174/// Base hook state trait
175pub trait HookStateBase {
176    fn hook_type(&self) -> &str;
177}
178
179/// Rounding direction for mathematical operations
180#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
181pub enum Rounding {
182    RoundDown = 0,
183    RoundUp = 1,
184}
185
186impl From<crate::pools::weighted::weighted_data::WeightedState> for PoolState {
187    fn from(weighted_state: crate::pools::weighted::weighted_data::WeightedState) -> Self {
188        PoolState::Weighted(weighted_state)
189    }
190}
191
192impl PoolState {
193    /// Get the base pool state
194    pub fn base(&self) -> &BasePoolState {
195        match self {
196            PoolState::Base(base) => base,
197            PoolState::Weighted(weighted) => weighted.base(),
198            PoolState::Stable(stable) => &stable.base,
199
200            PoolState::GyroECLP(gyro_eclp) => &gyro_eclp.base,
201            PoolState::ReClamm(re_clamm) => &re_clamm.base,
202            PoolState::ReClammV2(re_clamm_v2) => &re_clamm_v2.base,
203            PoolState::QuantAmm(quant_amm) => &quant_amm.base,
204            PoolState::LiquidityBootstrapping(liquidity_bootstrapping) => {
205                &liquidity_bootstrapping.base
206            }
207        }
208    }
209
210    /// Get the pool type
211    pub fn pool_type(&self) -> &str {
212        &self.base().pool_type
213    }
214
215    /// Get the pool address
216    pub fn pool_address(&self) -> &str {
217        &self.base().pool_address
218    }
219}