balancer_maths_rust/common/
types.rs1use crate::pools::buffer::BufferState;
4use num_bigint::BigInt;
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: BigInt,
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<BigInt>,
56 pub min_bpt_amount_out_raw: BigInt,
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<BigInt>,
69 pub max_bpt_amount_in_raw: BigInt,
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<BigInt>,
86 pub token_rates: Vec<BigInt>,
88 pub balances_live_scaled_18: Vec<BigInt>,
90 pub swap_fee: BigInt,
92 pub aggregate_swap_fee: BigInt,
94 pub total_supply: BigInt,
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}
124
125#[derive(Debug, Clone)]
127pub enum PoolStateOrBuffer {
128 Pool(Box<PoolState>),
129 Buffer(Box<BufferState>),
130}
131
132#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct SwapResult {
135 pub amount_out_raw: BigInt,
137 pub fee_amount_raw: BigInt,
139}
140
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
143pub struct AddLiquidityResult {
144 pub bpt_amount_out_raw: BigInt,
146 pub amounts_in_raw: Vec<BigInt>,
148}
149
150#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
152pub struct RemoveLiquidityResult {
153 pub bpt_amount_in_raw: BigInt,
155 pub amounts_out_raw: Vec<BigInt>,
157}
158
159#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161pub struct SwapParams {
162 pub swap_kind: SwapKind,
164 pub token_in_index: usize,
166 pub token_out_index: usize,
168 pub amount_scaled_18: BigInt,
170 pub balances_live_scaled_18: Vec<BigInt>,
172}
173
174pub trait HookStateBase {
176 fn hook_type(&self) -> &str;
177}
178
179#[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 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 pub fn pool_type(&self) -> &str {
212 &self.base().pool_type
213 }
214
215 pub fn pool_address(&self) -> &str {
217 &self.base().pool_address
218 }
219}