Skip to content

Commit 6cc80cf

Browse files
Vadim Nicolainicolad
Vadim Nicolai
authored andcommitted
feat(defi): implement From<Pool> → CurrencyPair & InstrumentAny
1 parent 7368e4c commit 6cc80cf

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

crates/model/src/defi/dex.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use std::{borrow::Cow, fmt::Display, sync::Arc};
1717

1818
use crate::defi::{amm::Pool, chain::Chain};
19+
use crate::identifiers::{InstrumentId, Symbol, Venue};
20+
use crate::instruments::{Instrument, any::InstrumentAny, currency_pair::CurrencyPair};
21+
use crate::types::{currency::Currency, price::Price, quantity::Quantity};
1922

2023
/// Represents different types of Automated Market Makers (AMMs) in DeFi protocols.
2124
#[derive(Debug, Clone)]
@@ -96,3 +99,46 @@ impl Display for Dex {
9699
write!(f, "Dex(chain={}, name={})", self.chain, self.name)
97100
}
98101
}
102+
103+
impl From<Pool> for CurrencyPair {
104+
fn from(p: Pool) -> Self {
105+
let symbol = Symbol::from(format!("{}/{}", p.token0.symbol, p.token1.symbol));
106+
let id = InstrumentId::new(symbol, Venue::from("DEFI"));
107+
108+
let size_precision = p.token0.decimals;
109+
let price_precision = p.token0.decimals.saturating_add(p.token1.decimals);
110+
111+
let price_increment = Price::new(10f64.powi(-(price_precision as i32)), price_precision);
112+
let size_increment = Quantity::new(10f64.powi(-(size_precision as i32)), size_precision);
113+
114+
CurrencyPair::new(
115+
id,
116+
symbol,
117+
Currency::from(p.token0.symbol.as_str()),
118+
Currency::from(p.token1.symbol.as_str()),
119+
price_precision,
120+
size_precision,
121+
price_increment,
122+
size_increment,
123+
None,
124+
None,
125+
None,
126+
None,
127+
None,
128+
None,
129+
None,
130+
None,
131+
None,
132+
None,
133+
None,
134+
0.into(),
135+
0.into(),
136+
)
137+
}
138+
}
139+
140+
impl From<Pool> for InstrumentAny {
141+
fn from(p: Pool) -> Self {
142+
CurrencyPair::from(p).into_any()
143+
}
144+
}

0 commit comments

Comments
 (0)