use alloy_primitives::{Address, U256, address};
use crate::types::{EscrowEnvironment, VenueId};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct PermitDomain {
pub name: &'static str,
pub version: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Chain {
pub id: u64,
pub token: Address,
pub decimals: u8,
pub permit: Option<PermitDomain>,
pub wallet_token: Option<Address>,
}
impl Chain {
pub(crate) fn step(&self) -> U256 {
U256::from(10u8).pow(U256::from(self.decimals.saturating_sub(6)))
}
}
pub(crate) const HYPERLIQUID: Chain = Chain {
id: 999,
token: address!("b88339cb7199b77e23db6e890353e22632ba630f"),
decimals: 6,
permit: Some(PermitDomain { name: "USDC", version: "2" }),
wallet_token: Some(address!("b88339cb7199b77e23db6e890353e22632ba630f")),
};
pub(crate) const POLYMARKET: Chain = Chain {
id: 137,
token: address!("c011a7e12a19f7b1f670d46f03b03f3342e82dfb"),
decimals: 6,
permit: Some(PermitDomain { name: "Polymarket USD", version: "1" }),
wallet_token: Some(address!("3c499c542cef5e3811e1192ce70d8cc03d5c3359")),
};
pub(crate) const GMX: Chain = Chain {
id: 42161,
token: address!("af88d065e77c8cc2239327c5edb3a432268e5831"),
decimals: 6,
permit: Some(PermitDomain { name: "USD Coin", version: "2" }),
wallet_token: Some(address!("af88d065e77c8cc2239327c5edb3a432268e5831")),
};
pub(crate) const LIGHTER: Chain = Chain {
id: 304,
token: address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"),
decimals: 6,
permit: None,
wallet_token: None,
};
pub(crate) const ASTER: Chain = Chain {
id: 56,
token: address!("55d398326f99059ff775485246999027b3197955"),
decimals: 18,
permit: None,
wallet_token: None,
};
pub const CHAINS: [(VenueId, Chain); 5] = [
(VenueId::Hyperliquid, HYPERLIQUID),
(VenueId::Polymarket, POLYMARKET),
(VenueId::Aster, ASTER),
(VenueId::Lighter, LIGHTER),
(VenueId::Gmx, GMX),
];
pub(crate) fn chain(venue: &VenueId) -> Option<&'static Chain> {
match venue {
VenueId::Hyperliquid => Some(&HYPERLIQUID),
VenueId::Polymarket => Some(&POLYMARKET),
VenueId::Gmx => Some(&GMX),
VenueId::Lighter => Some(&LIGHTER),
VenueId::Aster => Some(&ASTER),
VenueId::Unknown(_) => None,
}
}
pub fn is_venue_settled(venue: &VenueId) -> bool {
matches!(venue, VenueId::Lighter | VenueId::Aster)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Escrow {
pub chain_id: u64,
pub address: Address,
pub environment: EscrowEnvironment,
}
pub const ESCROWS: [Escrow; 6] = [
Escrow { chain_id: 999, address: ESCROW, environment: EscrowEnvironment::Production },
Escrow { chain_id: 137, address: ESCROW, environment: EscrowEnvironment::Production },
Escrow {
chain_id: 42161,
address: address!("7b0c3eb38153614452a56bc9295d1bd084a9190a"),
environment: EscrowEnvironment::Production,
},
Escrow {
chain_id: 999,
address: address!("948e65c40442d935d4ee990bdbd01bdd825c4873"),
environment: EscrowEnvironment::Development,
},
Escrow {
chain_id: 137,
address: address!("a3fca394739ec4b984e908bd7a3c3b72d0ed77d1"),
environment: EscrowEnvironment::Development,
},
Escrow {
chain_id: 42161,
address: address!("66e4c25d7e92c3e2765b021a748e740f775ac112"),
environment: EscrowEnvironment::Development,
},
];
pub const ESCROW: Address = address!("3100b7b49c0f9631c6af25877b5a4e13a47afad6");
pub(crate) fn escrow(chain_id: u64, environment: EscrowEnvironment) -> Option<Address> {
ESCROWS
.iter()
.find(|escrow| escrow.chain_id == chain_id && escrow.environment == environment)
.map(|escrow| escrow.address)
}
pub(crate) fn escrow_environment(chain_id: u64, address: Address) -> Option<EscrowEnvironment> {
ESCROWS
.iter()
.find(|escrow| escrow.chain_id == chain_id && escrow.address == address)
.map(|escrow| escrow.environment)
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct VenueDomain {
pub venue: VenueId,
pub chain_id: u64,
pub address: Address,
}
pub const VENUE_DOMAINS: [VenueDomain; 2] = [
VenueDomain {
venue: VenueId::Lighter,
chain_id: LIGHTER.id,
address: address!("3b4d794a66304f130a4db8f2551b0070dfcf5ca7"),
},
VenueDomain {
venue: VenueId::Aster,
chain_id: ASTER.id,
address: address!("128463a60784c4d3f46c23af3f65ed859ba87974"),
},
];
pub(crate) fn venue_contract(venue: &VenueId) -> Option<Address> {
VENUE_DOMAINS.iter().find(|domain| domain.venue == *venue).map(|domain| domain.address)
}
pub(crate) fn contract(venue: &VenueId, environment: EscrowEnvironment) -> Option<Address> {
venue_contract(venue).or_else(|| escrow(chain(venue)?.id, environment))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Slots {
pub aster_agent_name: &'static str,
pub lighter_key_index: u8,
}
pub(crate) const fn slots(environment: EscrowEnvironment) -> Slots {
match environment {
EscrowEnvironment::Production => {
Slots { aster_agent_name: "LaMarge", lighter_key_index: 254 }
}
EscrowEnvironment::Development => {
Slots { aster_agent_name: "LaMargeDev", lighter_key_index: 253 }
}
}
}
pub const ENVIRONMENT_SLOTS: [(EscrowEnvironment, Slots); 2] = [
(EscrowEnvironment::Production, slots(EscrowEnvironment::Production)),
(EscrowEnvironment::Development, slots(EscrowEnvironment::Development)),
];
pub const MAKER_TIMEOUT_SECONDS: u64 = 3600;
pub const TAKER_TIMEOUT_SECONDS: u64 = 86_400;
pub const ROUTE_PAY_WINDOW_SECONDS: u64 = 210;
pub const AT_ONCE_SEND_MARGIN_SECONDS: u64 = 30;
pub const AT_ONCE_SIGNING_SECONDS: u64 = 30;
pub const WALLET_SWAP_MIN_BPS: u16 = 9_995;
pub const UNLIMITED_ALLOWANCE: U256 = U256::MAX;
pub const LIGHTER_MAX_TAKER_FEE: U256 = U256::from_limbs([1_000_000, 0, 0, 0]);
pub(crate) const HYPERLIQUID_USDC: &str = "USDC:0x6d1e7cde53ba9467b783cb7c530ce054";
pub(crate) const HYPERLIQUID_BRIDGE: Address = address!("2000000000000000000000000000000000000000");
pub(crate) const POLYMARKET_WITHDRAW_CONTRACT: Address =
address!("dca4af75705dbb50f62437045aff9921947917d2");
pub fn venue_units(value: U256, venue: &VenueId) -> Option<U256> {
let step = chain(venue)?.step();
let remainder = value % step;
if remainder.is_zero() { Some(value) } else { value.checked_add(step - remainder) }
}
pub fn venue_step(venue: &VenueId) -> Option<U256> {
chain(venue).map(Chain::step)
}
pub fn decimal_units(text: &str, decimals: u8) -> Option<U256> {
let (whole, fraction) = text.split_once('.').unwrap_or((text, ""));
let digits = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit());
if !digits(whole) || (text.contains('.') && (!digits(fraction) || fraction.len() > 6)) {
return None;
}
crate::types::Amount::parse_units(text, decimals).ok().map(crate::types::Amount::get)
}
#[cfg(test)]
mod tests;