stellar_base/
liquidity_pool.rs1use crate::asset::Asset;
2use crate::error::{Error, Result};
3use crate::xdr;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct LiquidityPoolId(Vec<u8>);
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct LiquidityPoolConstantFeeParameters {
10 assets: (Asset, Asset),
11 fee: i32,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum LiquidityPoolParameters {
16 ConstantFee(LiquidityPoolConstantFeeParameters),
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct LiquidityPoolConstantFee {
21 assets: (Asset, Asset),
22 fee: i32,
23 reserves: (i64, i64),
24 total_pool_shares: i64,
25 pool_shares_trust_line_count: i64,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum LiquidityPool {
30 ConstantFee(LiquidityPoolConstantFee),
31}
32
33impl LiquidityPoolId {
34 pub fn new(hash: Vec<u8>) -> Result<Self> {
36 if hash.len() != 32 {
37 return Err(Error::InvalidLiquidityPoolIdLength);
38 }
39
40 Ok(Self(hash))
41 }
42
43 pub fn as_bytes(&self) -> &[u8] {
45 &self.0
46 }
47
48 pub fn to_xdr(&self) -> xdr::PoolId {
50 xdr::PoolId(self.0.as_slice().try_into().unwrap())
51 }
52
53 pub fn from_xdr(x: &xdr::PoolId) -> Result<Self> {
55 Ok(Self(x.0 .0.to_vec()))
56 }
57}
58
59impl LiquidityPoolConstantFeeParameters {
60 pub fn to_xdr(&self) -> Result<xdr::LiquidityPoolConstantProductParameters> {
62 Ok(xdr::LiquidityPoolConstantProductParameters {
63 asset_a: self.assets.0.to_xdr()?,
64 asset_b: self.assets.1.to_xdr()?,
65 fee: self.fee,
66 })
67 }
68
69 pub fn from_xdr(x: &xdr::LiquidityPoolConstantProductParameters) -> Result<Self> {
70 let asset_a = Asset::from_xdr(&x.asset_a)?;
71 let asset_b = Asset::from_xdr(&x.asset_b)?;
72 Ok(Self {
73 assets: (asset_a, asset_b),
74 fee: x.fee,
75 })
76 }
77}
78
79impl LiquidityPoolParameters {
80 pub fn to_xdr(&self) -> Result<xdr::LiquidityPoolParameters> {
81 match self {
82 LiquidityPoolParameters::ConstantFee(ref params) => {
83 let params_xdr = params.to_xdr()?;
84 Ok(xdr::LiquidityPoolParameters::LiquidityPoolConstantProduct(
85 params_xdr,
86 ))
87 }
88 }
89 }
90
91 pub fn from_xdr(x: &xdr::LiquidityPoolParameters) -> Result<Self> {
92 match *x {
93 xdr::LiquidityPoolParameters::LiquidityPoolConstantProduct(ref constant_params) => {
94 let params = LiquidityPoolConstantFeeParameters::from_xdr(constant_params)?;
95 Ok(Self::ConstantFee(params))
96 }
97 }
98 }
99}