use crate::generated::dex_catalog as generated;
pub use generated::{
DEX_ALIASES, DEX_CATALOG_AS_OF_DATE, DEX_CATALOG_CONTENT_DIGEST, DEX_CATALOG_VERSION,
DEX_CHAIN_IDS, DEX_DEPLOYMENTS, DexAlias, DexDeployment, NATIVE_WRAP_DEFINITIONS,
NativeWrapDefinition, POOL_DEFINITIONS, PoolAdapter, PoolDefinition,
};
pub type DexChainId = &'static str;
pub use generated::dexes;
pub use generated::pools;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ListPoolDefinitionsOptions<'a> {
pub chain_id: Option<&'a str>,
pub token_deployment_id: Option<&'a str>,
pub adapter_kind: Option<&'a str>,
}
#[must_use]
pub fn get_dex_deployment(dex_deployment_id: &str) -> Option<&'static DexDeployment> {
DEX_DEPLOYMENTS
.iter()
.find(|deployment| deployment.dex_deployment_id == dex_deployment_id)
}
#[must_use]
pub fn get_pool_definition(pool_definition_id: &str) -> Option<&'static PoolDefinition> {
POOL_DEFINITIONS
.iter()
.find(|pool| pool.pool_definition_id == pool_definition_id)
}
#[must_use]
pub fn find_pool_definition_by_address(
chain_id: &str,
address: &str,
) -> Option<&'static PoolDefinition> {
if !is_known_chain(chain_id) || address.is_empty() {
return None;
}
if is_evm_chain(chain_id) {
if !is_evm_address(address) {
return None;
}
POOL_DEFINITIONS
.iter()
.find(|pool| pool.chain_id == chain_id && pool.address.eq_ignore_ascii_case(address))
} else {
POOL_DEFINITIONS
.iter()
.find(|pool| pool.chain_id == chain_id && pool.address == address)
}
}
#[must_use]
pub fn find_pool_definitions_by_pair(
chain_id: &str,
first_token_deployment_id: &str,
second_token_deployment_id: &str,
) -> Vec<&'static PoolDefinition> {
if !is_known_chain(chain_id)
|| first_token_deployment_id.is_empty()
|| second_token_deployment_id.is_empty()
|| first_token_deployment_id == second_token_deployment_id
{
return Vec::new();
}
let (wanted_left, wanted_right) =
ordered_pair(first_token_deployment_id, second_token_deployment_id);
let mut matches: Vec<_> = POOL_DEFINITIONS
.iter()
.filter(|pool| {
if pool.chain_id != chain_id {
return false;
}
let (left, right) = ordered_pair(pool.token0_deployment_id, pool.token1_deployment_id);
left == wanted_left && right == wanted_right
})
.collect();
matches.sort_unstable_by_key(|pool| pool.pool_definition_id);
matches
}
#[must_use]
pub fn list_pool_definitions(
options: ListPoolDefinitionsOptions<'_>,
) -> Vec<&'static PoolDefinition> {
if options.chain_id.is_some_and(|chain| !is_known_chain(chain)) {
return Vec::new();
}
let mut matches: Vec<_> = POOL_DEFINITIONS
.iter()
.filter(|pool| {
options.chain_id.is_none_or(|chain| pool.chain_id == chain)
&& options.token_deployment_id.is_none_or(|token| {
!token.is_empty()
&& (pool.token0_deployment_id == token
|| pool.token1_deployment_id == token)
})
&& options
.adapter_kind
.is_none_or(|adapter| !adapter.is_empty() && pool.adapter.kind == adapter)
})
.collect();
matches.sort_unstable_by_key(|pool| pool.pool_definition_id);
matches
}
#[must_use]
pub fn get_native_wrap_definition(
native_token_deployment_id: &str,
) -> Option<&'static NativeWrapDefinition> {
NATIVE_WRAP_DEFINITIONS
.iter()
.find(|definition| definition.native_token_deployment_id == native_token_deployment_id)
}
fn is_known_chain(chain_id: &str) -> bool {
DEX_CHAIN_IDS
.iter()
.any(|(_, candidate)| *candidate == chain_id)
}
fn is_evm_chain(chain_id: &str) -> bool {
DEX_DEPLOYMENTS.iter().any(|deployment| {
deployment.chain_id == chain_id && deployment.program_address.starts_with("0x")
})
}
fn is_evm_address(address: &str) -> bool {
let bytes = address.as_bytes();
bytes.len() == 42
&& bytes[0] == b'0'
&& bytes[1] == b'x'
&& bytes[2..].iter().all(u8::is_ascii_hexdigit)
}
fn ordered_pair<'a>(left: &'a str, right: &'a str) -> (&'a str, &'a str) {
if left <= right {
(left, right)
} else {
(right, left)
}
}