use std::{collections::HashMap, str::FromStr, sync::LazyLock};
use num_bigint::BigUint;
use tycho_common::Bytes;
pub const PRICE_LEVEL_STREAM_FAMILY: &str = "pricelevelstream";
pub const FALLBACK_FAMILY: &str = "fallback";
#[derive(Debug, Clone)]
pub struct PriceLevelStreamConfig {
pub protocol: String,
pub address: Bytes,
pub gas_cost: BigUint,
}
impl PriceLevelStreamConfig {
pub fn new(protocol: impl Into<String>, address: Bytes, gas_cost: BigUint) -> Self {
Self { protocol: protocol.into(), address, gas_cost }
}
pub(super) fn auto_detected(address: Bytes, gas_cost: BigUint) -> Self {
let protocol = address.to_string();
Self::new(protocol, address, gas_cost)
}
pub fn protocol_system(&self) -> String {
format!("{PRICE_LEVEL_STREAM_FAMILY}:{}", self.protocol)
}
pub fn fallback_protocol_system(&self) -> String {
format!("{FALLBACK_FAMILY}:{}", self.protocol)
}
}
pub const DEFAULT_AUTO_DETECTED_GAS_COST: u64 = 335_000;
pub fn default_served_pamms() -> Vec<PriceLevelStreamConfig> {
let pamms = [
("fermiswap", "0x5979458912f80b96d30d4220af8e2e4925a33320", 185_000u64),
("kipseli", "0x71e790dd841c8a9061487cb3e78c288e75ce0b3d", 335_000u64),
("metric", "0x99ab7c1c5eab47801f7937075998527a550fba8a", 280_000u64),
("bebop", "0xb09aaa8933626d7e4c48d65dad2d77021cfbca9a", 140_000u64),
("taurusfi", "0x20861433d9119b866eeb398cb1ad46652a3cf953", 110_000u64),
("tempest", "0x00000003f1ec2379e79f58e12ec6c4f51ee92149", 160_000u64),
("goliath", "0x4367985a63063fc970b009c6e599e9225181f551", 105_000u64),
("stelaxis", "0x77047af6cd8f7f84d96a020a2833d24916b75fde", 105_000u64),
("elzorro", "0xcf211b4dd0d2be5c173ea57bcf938fc61d1d3bd3", 105_000u64),
(
"0x6e27bb694393840e253262361b79187d4899e9cf",
"0x6e27bb694393840e253262361b79187d4899e9cf",
145_000u64,
),
(
"0xf6d5b2fefec7b420d9990b95790adfd3d4c4f977",
"0xf6d5b2fefec7b420d9990b95790adfd3d4c4f977",
210_000u64,
),
];
pamms
.into_iter()
.map(|(protocol, address, gas_cost)| {
PriceLevelStreamConfig::new(
protocol,
Bytes::from_str(address).expect("hardcoded pAMM address must parse"),
BigUint::from(gas_cost),
)
})
.collect()
}
pub fn default_denied_pamms() -> Vec<Bytes> {
Vec::new()
}
pub const PAMM_ADDRESS_ALIASES: &[(&str, &str)] = &[
("0xc9a956b5196dfe110debe8781857bc8b97d32091", "0x71e790dd841c8a9061487cb3e78c288e75ce0b3d"),
("0xe844701934d9c6085b292e428f41b911cc0b3872", "0x99ab7c1c5eab47801f7937075998527a550fba8a"),
];
static CANONICAL_PAMM_ADDRESSES: LazyLock<HashMap<Bytes, Bytes>> = LazyLock::new(|| {
PAMM_ADDRESS_ALIASES
.iter()
.map(|(streamed, public)| {
(
Bytes::from_str(streamed).expect("hardcoded streamed pAMM address must parse"),
Bytes::from_str(public).expect("hardcoded public pAMM address must parse"),
)
})
.collect()
});
pub(super) fn canonical_pamm_address(address: &Bytes) -> Bytes {
CANONICAL_PAMM_ADDRESSES
.get(address)
.unwrap_or(address)
.clone()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_alias_points_at_a_registered_venue() {
let registered: Vec<Bytes> = default_served_pamms()
.into_iter()
.map(|config| config.address)
.collect();
for (streamed, public) in PAMM_ADDRESS_ALIASES {
let public = Bytes::from_str(public).expect("hardcoded public pAMM address");
assert!(
registered.contains(&public),
"the alias of {streamed} points at {public}, which no venue registers"
);
}
}
#[test]
fn a_streamed_key_is_never_itself_a_registered_venue() {
let registered: Vec<Bytes> = default_served_pamms()
.into_iter()
.map(|config| config.address)
.collect();
for (streamed, public) in PAMM_ADDRESS_ALIASES {
let streamed = Bytes::from_str(streamed).expect("hardcoded streamed pAMM address");
assert_ne!(&streamed, &Bytes::from_str(public).expect("public"));
assert!(
!registered.contains(&streamed),
"{streamed} is both a streamed key and a registered venue"
);
}
}
}