pub struct PolymarketEndpoints {
pub clob_base: &'static str,
pub gamma_base: &'static str,
pub data_base: &'static str,
pub ws_clob: &'static str,
}
impl Default for PolymarketEndpoints {
fn default() -> Self {
Self {
clob_base: "https://clob.polymarket.com",
gamma_base: "https://gamma-api.polymarket.com",
data_base: "https://data-api.polymarket.com",
ws_clob: "wss://ws-subscriptions-clob.polymarket.com/ws/market",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PolymarketEndpoint {
ClobMarkets,
ClobMarket,
OrderBook,
Midpoint,
Price,
Spread,
LastTradePrice,
PricesHistory,
Time,
GammaEvents,
GammaEvent,
GammaMarkets,
GammaMarket,
ClobOrders,
ClobOrder,
DataPositions,
}
impl PolymarketEndpoint {
pub fn path(&self) -> &'static str {
match self {
Self::ClobMarkets => "/markets",
Self::ClobMarket => "/markets",
Self::OrderBook => "/book",
Self::Midpoint => "/midpoint",
Self::Price => "/price",
Self::Spread => "/spread",
Self::LastTradePrice => "/last-trade-price",
Self::PricesHistory => "/prices-history",
Self::Time => "/time",
Self::GammaEvents => "/events",
Self::GammaEvent => "/events",
Self::GammaMarkets => "/markets",
Self::GammaMarket => "/markets",
Self::ClobOrders => "/orders",
Self::ClobOrder => "/orders",
Self::DataPositions => "/positions",
}
}
pub fn is_gamma(&self) -> bool {
matches!(
self,
Self::GammaEvents | Self::GammaEvent | Self::GammaMarkets | Self::GammaMarket
)
}
pub fn is_data(&self) -> bool {
matches!(self, Self::DataPositions)
}
pub fn requires_auth(&self) -> bool {
matches!(self, Self::ClobOrders | Self::ClobOrder)
}
}
pub fn _clob_markets() -> String {
"https://clob.polymarket.com/markets".to_string()
}
pub fn _clob_market(condition_id: &str) -> String {
format!("https://clob.polymarket.com/markets/{}", condition_id)
}
pub fn _clob_book(token_id: &str) -> String {
format!("https://clob.polymarket.com/book?token_id={}", token_id)
}
pub fn _clob_midpoint(token_id: &str) -> String {
format!("https://clob.polymarket.com/midpoint?token_id={}", token_id)
}
pub fn _clob_price(token_id: &str, side: &str) -> String {
format!(
"https://clob.polymarket.com/price?token_id={}&side={}",
token_id, side
)
}
pub fn _clob_spread(token_id: &str) -> String {
format!("https://clob.polymarket.com/spread?token_id={}", token_id)
}
pub fn _clob_last_trade_price(token_id: &str) -> String {
format!(
"https://clob.polymarket.com/last-trade-price?token_id={}",
token_id
)
}
pub fn _prices_history(token_id: &str, interval: &str, fidelity: u32) -> String {
format!(
"https://clob.polymarket.com/prices-history?market={}&interval={}&fidelity={}",
token_id, interval, fidelity
)
}
pub fn _clob_time() -> String {
"https://clob.polymarket.com/time".to_string()
}
pub fn _gamma_events() -> String {
"https://gamma-api.polymarket.com/events".to_string()
}
pub fn _gamma_event(id: &str) -> String {
format!("https://gamma-api.polymarket.com/events/{}", id)
}
pub fn _gamma_markets() -> String {
"https://gamma-api.polymarket.com/markets".to_string()
}
pub fn _gamma_market(id: &str) -> String {
format!("https://gamma-api.polymarket.com/markets/{}", id)
}
pub fn _clob_orders() -> String {
"https://clob.polymarket.com/orders".to_string()
}
pub fn _clob_order(id: &str) -> String {
format!("https://clob.polymarket.com/orders/{}", id)
}
pub fn _data_positions(address: &str) -> String {
format!("https://data-api.polymarket.com/positions?user={}", address)
}
pub fn map_interval(interval: &str) -> &'static str {
match interval {
"1m" | "3m" | "5m" | "15m" | "30m" => "1m",
"1h" | "2h" | "4h" => "1h",
"6h" | "8h" | "12h" => "6h",
"1d" | "1D" => "1d",
"1w" | "1W" => "1w",
_ => "1d",
}
}
pub fn get_fidelity(limit: Option<u16>) -> u32 {
limit.unwrap_or(500).min(1000) as u32
}