balancer_maths_rust/common/
types.rs1use crate::pools::buffer::BufferState;
4use alloy_primitives::U256;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub enum SwapKind {
10 GivenIn = 0,
12 GivenOut = 1,
14}
15
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub enum AddLiquidityKind {
19 Unbalanced = 0,
21 SingleTokenExactOut = 1,
23}
24
25#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27pub enum RemoveLiquidityKind {
28 Proportional = 0,
30 SingleTokenExactIn = 1,
32 SingleTokenExactOut = 2,
34}
35
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub struct SwapInput {
39 pub amount_raw: U256,
41 pub swap_kind: SwapKind,
43 pub token_in: String,
45 pub token_out: String,
47}
48
49#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51pub struct AddLiquidityInput {
52 pub pool: String,
54 pub max_amounts_in_raw: Vec<U256>,
56 pub min_bpt_amount_out_raw: U256,
58 pub kind: AddLiquidityKind,
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct RemoveLiquidityInput {
65 pub pool: String,
67 pub min_amounts_out_raw: Vec<U256>,
69 pub max_bpt_amount_in_raw: U256,
71 pub kind: RemoveLiquidityKind,
73}
74
75#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
77pub struct BasePoolState {
78 pub pool_address: String,
80 pub pool_type: String,
82 pub tokens: Vec<String>,
84 pub scaling_factors: Vec<U256>,
86 pub token_rates: Vec<U256>,
88 pub balances_live_scaled_18: Vec<U256>,
90 pub swap_fee: U256,
92 pub aggregate_swap_fee: U256,
94 pub total_supply: U256,
96 pub supports_unbalanced_liquidity: bool,
98 pub hook_type: Option<String>,
100}
101
102#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
104#[serde(untagged)]
105pub enum PoolState {
106 Base(BasePoolState),
108 Weighted(crate::pools::weighted::WeightedState),
110 Stable(crate::pools::stable::stable_data::StableState),
112
113 GyroECLP(crate::pools::gyro::gyro_eclp_data::GyroECLPState),
115 ReClamm(crate::pools::reclamm::reclamm_data::ReClammState),
117 ReClammV2(crate::pools::reclammv2::reclammv2_data::ReClammV2State),
119 QuantAmm(crate::pools::quantamm::quantamm_data::QuantAmmState),
121 LiquidityBootstrapping(crate::pools::liquidity_bootstrapping::liquidity_bootstrapping_data::LiquidityBootstrappingState),
123 FixedPriceLBP(crate::pools::fixed_price_lbp::fixed_price_lbp_data::FixedPriceLBPState),
125}
126
127#[derive(Debug, Clone)]
129pub enum PoolStateOrBuffer {
130 Pool(Box<PoolState>),
131 Buffer(Box<BufferState>),
132}
133
134#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
136pub struct SwapResult {
137 pub amount_out_raw: U256,
139 pub fee_amount_raw: U256,
141}
142
143#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
145pub struct AddLiquidityResult {
146 pub bpt_amount_out_raw: U256,
148 pub amounts_in_raw: Vec<U256>,
150}
151
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
154pub struct RemoveLiquidityResult {
155 pub bpt_amount_in_raw: U256,
157 pub amounts_out_raw: Vec<U256>,
159}
160
161#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
163pub struct SwapParams {
164 pub swap_kind: SwapKind,
166 pub token_in_index: usize,
168 pub token_out_index: usize,
170 pub amount_scaled_18: U256,
172 pub balances_live_scaled_18: Vec<U256>,
174}
175
176pub trait HookStateBase {
178 fn hook_type(&self) -> &str;
179}
180
181#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
183pub enum Rounding {
184 RoundDown = 0,
185 RoundUp = 1,
186}
187
188impl PoolState {
189 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 pub fn pool_type(&self) -> &str {
209 &self.base().pool_type
210 }
211
212 pub fn pool_address(&self) -> &str {
214 &self.base().pool_address
215 }
216}