Skip to content

Implement From<Pool> → CurrencyPair & InstrumentAny #2693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions crates/model/src/defi/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use std::{borrow::Cow, fmt::Display, sync::Arc};

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

/// Represents different types of Automated Market Makers (AMMs) in DeFi protocols.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -96,3 +99,46 @@ impl Display for Dex {
write!(f, "Dex(chain={}, name={})", self.chain, self.name)
}
}

impl From<Pool> for CurrencyPair {
fn from(p: Pool) -> Self {
let symbol = Symbol::from(format!("{}/{}", p.token0.symbol, p.token1.symbol));
let id = InstrumentId::new(symbol, Venue::from("DEFI"));

let size_precision = p.token0.decimals;
let price_precision = p.token0.decimals.saturating_add(p.token1.decimals);

let price_increment = Price::new(10f64.powi(-(price_precision as i32)), price_precision);
let size_increment = Quantity::new(10f64.powi(-(size_precision as i32)), size_precision);

CurrencyPair::new(
id,
symbol,
Currency::from(p.token0.symbol.as_str()),
Currency::from(p.token1.symbol.as_str()),
price_precision,
size_precision,
price_increment,
size_increment,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
0.into(),
0.into(),
)
}
}

impl From<Pool> for InstrumentAny {
fn from(p: Pool) -> Self {
CurrencyPair::from(p).into_any()
}
}
Loading