use alloy_primitives::{Address, U256};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal::prelude::ToPrimitive;
use serde::{Deserialize, Serialize};
use serde_json;
use std::fmt;
pub type Price = u32;
pub type Qty = i64;
pub const SCALE_FACTOR: i64 = 10_000;
pub const MAX_PRICE_TICKS: Price = Price::MAX;
pub const MIN_PRICE_TICKS: Price = 1;
pub const MAX_QTY: Qty = Qty::MAX / 2;
pub fn decimal_to_price(decimal: Decimal) -> std::result::Result<Price, &'static str> {
let scaled = decimal * Decimal::from(SCALE_FACTOR);
let rounded = scaled.round();
let as_u64 = rounded.to_u64().ok_or("Price too large or negative")?;
if as_u64 < MIN_PRICE_TICKS as u64 {
return Ok(MIN_PRICE_TICKS); }
if as_u64 > MAX_PRICE_TICKS as u64 {
return Err("Price exceeds maximum");
}
Ok(as_u64 as Price)
}
pub fn price_to_decimal(ticks: Price) -> Decimal {
Decimal::from(ticks) / Decimal::from(SCALE_FACTOR)
}
pub fn decimal_to_qty(decimal: Decimal) -> std::result::Result<Qty, &'static str> {
let scaled = decimal * Decimal::from(SCALE_FACTOR);
let rounded = scaled.round();
let as_i64 = rounded.to_i64().ok_or("Quantity too large")?;
if as_i64.abs() > MAX_QTY {
return Err("Quantity exceeds maximum");
}
Ok(as_i64)
}
pub fn qty_to_decimal(units: Qty) -> Decimal {
Decimal::from(units) / Decimal::from(SCALE_FACTOR)
}
pub fn is_price_tick_aligned(decimal: Decimal, tick_size_decimal: Decimal) -> bool {
let tick_size_ticks = match decimal_to_price(tick_size_decimal) {
Ok(ticks) => ticks,
Err(_) => return false,
};
let price_ticks = match decimal_to_price(decimal) {
Ok(ticks) => ticks,
Err(_) => return false,
};
if tick_size_ticks == 0 {
return true;
}
price_ticks % tick_size_ticks == 0
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Side {
BUY = 0,
SELL = 1,
}
impl Side {
pub fn as_str(&self) -> &'static str {
match self {
Side::BUY => "BUY",
Side::SELL => "SELL",
}
}
pub fn opposite(&self) -> Self {
match self {
Side::BUY => Side::SELL,
Side::SELL => Side::BUY,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OrderType {
GTC,
FOK,
GTD,
}
impl OrderType {
pub fn as_str(&self) -> &'static str {
match self {
OrderType::GTC => "GTC",
OrderType::FOK => "FOK",
OrderType::GTD => "GTD",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum OrderStatus {
#[serde(rename = "LIVE")]
Live,
#[serde(rename = "CANCELLED")]
Cancelled,
#[serde(rename = "FILLED")]
Filled,
#[serde(rename = "PARTIAL")]
Partial,
#[serde(rename = "EXPIRED")]
Expired,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketSnapshot {
pub token_id: String,
pub market_id: String,
pub timestamp: DateTime<Utc>,
pub bid: Option<Decimal>,
pub ask: Option<Decimal>,
pub mid: Option<Decimal>,
pub spread: Option<Decimal>,
pub last_price: Option<Decimal>,
pub volume_24h: Option<Decimal>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookLevel {
#[serde(with = "rust_decimal::serde::str")]
pub price: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub size: Decimal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FastBookLevel {
pub price: Price, pub size: Qty, }
impl FastBookLevel {
pub fn new(price: Price, size: Qty) -> Self {
Self { price, size }
}
pub fn to_book_level(self) -> BookLevel {
BookLevel {
price: price_to_decimal(self.price),
size: qty_to_decimal(self.size),
}
}
pub fn from_book_level(level: &BookLevel) -> std::result::Result<Self, &'static str> {
let price = decimal_to_price(level.price)?;
let size = decimal_to_qty(level.size)?;
Ok(Self::new(price, size))
}
pub fn notional(self) -> i64 {
let price_i64 = self.price as i64;
(price_i64 * self.size) / SCALE_FACTOR
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderBook {
pub token_id: String,
pub timestamp: DateTime<Utc>,
pub bids: Vec<BookLevel>,
pub asks: Vec<BookLevel>,
pub sequence: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderDelta {
pub token_id: String,
pub timestamp: DateTime<Utc>,
pub side: Side,
pub price: Decimal,
pub size: Decimal, pub sequence: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FastOrderDelta {
pub token_id_hash: u64, pub timestamp: DateTime<Utc>,
pub side: Side,
pub price: Price, pub size: Qty, pub sequence: u64,
}
impl FastOrderDelta {
pub fn from_order_delta(
delta: &OrderDelta,
tick_size: Option<Decimal>,
) -> std::result::Result<Self, &'static str> {
if let Some(tick_size) = tick_size {
if !is_price_tick_aligned(delta.price, tick_size) {
return Err("Price not aligned to tick size");
}
}
let price = decimal_to_price(delta.price)?;
let size = decimal_to_qty(delta.size)?;
let token_id_hash = {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
delta.token_id.hash(&mut hasher);
hasher.finish()
};
Ok(Self {
token_id_hash,
timestamp: delta.timestamp,
side: delta.side,
price,
size,
sequence: delta.sequence,
})
}
pub fn to_order_delta(self, token_id: String) -> OrderDelta {
OrderDelta {
token_id,
timestamp: self.timestamp,
side: self.side,
price: price_to_decimal(self.price),
size: qty_to_decimal(self.size),
sequence: self.sequence,
}
}
pub fn is_removal(self) -> bool {
self.size == 0
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FillEvent {
pub id: String,
pub order_id: String,
pub token_id: String,
pub side: Side,
pub price: Decimal,
pub size: Decimal,
pub timestamp: DateTime<Utc>,
pub maker_address: Address,
pub taker_address: Address,
pub fee: Decimal,
}
#[derive(Debug, Clone)]
pub struct OrderRequest {
pub token_id: String,
pub side: Side,
pub price: Decimal,
pub size: Decimal,
pub order_type: OrderType,
pub expiration: Option<DateTime<Utc>>,
pub client_id: Option<String>,
}
#[derive(Debug, Clone)]
pub struct MarketOrderRequest {
pub token_id: String,
pub side: Side,
pub amount: Decimal, pub slippage_tolerance: Option<Decimal>,
pub client_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
pub id: String,
pub token_id: String,
pub side: Side,
pub price: Decimal,
pub original_size: Decimal,
pub filled_size: Decimal,
pub remaining_size: Decimal,
pub status: OrderStatus,
pub order_type: OrderType,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub expiration: Option<DateTime<Utc>>,
pub client_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ApiCredentials {
#[serde(rename = "apiKey")]
pub api_key: String,
pub secret: String,
pub passphrase: String,
}
#[derive(Debug, Clone)]
pub struct OrderOptions {
pub tick_size: Option<Decimal>,
pub neg_risk: Option<bool>,
pub fee_rate_bps: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct ExtraOrderArgs {
pub fee_rate_bps: u32,
pub nonce: U256,
pub taker: String,
}
impl Default for ExtraOrderArgs {
fn default() -> Self {
Self {
fee_rate_bps: 0,
nonce: U256::ZERO,
taker: "0x0000000000000000000000000000000000000000".to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct MarketOrderArgs {
pub token_id: String,
pub amount: Decimal,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignedOrderRequest {
pub salt: u64,
pub maker: String,
pub signer: String,
pub taker: String,
pub token_id: String,
pub maker_amount: String,
pub taker_amount: String,
pub expiration: String,
pub nonce: String,
pub fee_rate_bps: String,
pub side: String,
pub signature_type: u8,
pub signature: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PostOrder {
pub order: SignedOrderRequest,
pub owner: String,
pub order_type: OrderType,
}
impl PostOrder {
pub fn new(order: SignedOrderRequest, owner: String, order_type: OrderType) -> Self {
Self {
order,
owner,
order_type,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Market {
pub condition_id: String,
pub tokens: [Token; 2],
#[serde(default, skip)]
pub clob_token_ids: Vec<String>,
pub rewards: Rewards,
pub min_incentive_size: Option<String>,
pub max_incentive_spread: Option<String>,
pub active: bool,
pub closed: bool,
pub question_id: String,
#[serde(with = "rust_decimal::serde::str")]
pub minimum_order_size: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub minimum_tick_size: Decimal,
pub description: String,
pub category: Option<String>,
pub end_date_iso: Option<String>,
pub game_start_time: Option<String>,
pub question: String,
pub market_slug: String,
#[serde(with = "rust_decimal::serde::str")]
pub seconds_delay: Decimal,
pub icon: String,
pub fpmm: String,
pub liquidity: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub liquidity_num: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub liquidity_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub liquidity_clob: Option<Decimal>,
pub volume: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_num: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_24hr: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1wk: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1mo: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1yr: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_24hr_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1wk_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1mo_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1yr_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_24hr_clob: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1wk_clob: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1mo_clob: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_1yr_clob: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_amm: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_clob: Option<Decimal>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Token {
pub token_id: String,
pub outcome: String,
}
impl GammaMarket {
fn parse_token_ids(&self) -> Vec<String> {
self.clob_token_ids
.as_ref()
.and_then(|raw| serde_json::from_str::<Vec<String>>(raw).ok())
.unwrap_or_default()
}
fn normalized_outcomes(&self) -> Vec<String> {
let default_outcomes = vec!["Yes".to_string(), "No".to_string()];
if let Some(raw) = self.outcomes.as_ref() {
if let Ok(values) = serde_json::from_str::<Vec<String>>(raw) {
return values;
}
}
default_outcomes
}
}
impl From<GammaMarket> for Market {
fn from(gamma: GammaMarket) -> Self {
let token_ids = gamma.parse_token_ids();
let outcomes = gamma.normalized_outcomes();
let tokens = [
Token {
token_id: token_ids.first().cloned().unwrap_or_default(),
outcome: outcomes
.first()
.cloned()
.unwrap_or_else(|| "Yes".to_string()),
},
Token {
token_id: token_ids.get(1).cloned().unwrap_or_default(),
outcome: outcomes.get(1).cloned().unwrap_or_else(|| "No".to_string()),
},
];
Market {
condition_id: gamma.condition_id.clone(),
tokens,
clob_token_ids: token_ids.clone(),
rewards: Rewards {
rates: None,
min_size: Decimal::ZERO,
max_spread: Decimal::ZERO,
event_start_date: None,
event_end_date: gamma.end_date.clone(),
in_game_multiplier: None,
reward_epoch: None,
},
min_incentive_size: None,
max_incentive_spread: None,
active: gamma.active,
closed: gamma.closed,
question_id: gamma.condition_id.clone(),
minimum_order_size: Decimal::ZERO,
minimum_tick_size: Decimal::ZERO,
description: gamma.description.unwrap_or_default(),
category: gamma.category.clone(),
end_date_iso: gamma.end_date.clone(),
game_start_time: None,
question: gamma.question.unwrap_or_default(),
market_slug: gamma.slug.clone(),
seconds_delay: Decimal::ZERO,
icon: gamma.icon.unwrap_or_default(),
fpmm: String::new(),
liquidity: gamma.liquidity.clone(),
liquidity_num: gamma.liquidity_num,
liquidity_amm: gamma.liquidity_amm,
liquidity_clob: gamma.liquidity_clob,
volume: gamma.volume.clone(),
volume_num: gamma.volume_num,
volume_24hr: gamma.volume_24hr,
volume_1wk: gamma.volume_1wk,
volume_1mo: gamma.volume_1mo,
volume_1yr: gamma.volume_1yr,
volume_24hr_amm: gamma.volume_24hr_amm,
volume_1wk_amm: gamma.volume_1wk_amm,
volume_1mo_amm: gamma.volume_1mo_amm,
volume_1yr_amm: gamma.volume_1yr_amm,
volume_24hr_clob: gamma.volume_24hr_clob,
volume_1wk_clob: gamma.volume_1wk_clob,
volume_1mo_clob: gamma.volume_1mo_clob,
volume_1yr_clob: gamma.volume_1yr_clob,
volume_amm: gamma.volume_amm,
volume_clob: gamma.volume_clob,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientConfig {
pub base_url: String,
pub chain_id: u64,
pub private_key: Option<String>,
pub api_credentials: Option<ApiCredentials>,
pub max_slippage: Option<Decimal>,
pub fee_rate: Option<Decimal>,
pub timeout: Option<std::time::Duration>,
pub max_connections: Option<usize>,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
base_url: "https://clob.polymarket.com".to_string(),
chain_id: 137, private_key: None,
api_credentials: None,
timeout: Some(std::time::Duration::from_secs(30)),
max_connections: Some(100),
max_slippage: None,
fee_rate: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WssAuth {
pub address: String,
pub signature: String,
pub timestamp: u64,
pub nonce: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WssSubscription {
pub auth: WssAuth,
pub markets: Option<Vec<String>>,
pub asset_ids: Option<Vec<String>>,
#[serde(rename = "type")]
pub channel_type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum StreamMessage {
#[serde(rename = "book_update")]
BookUpdate { data: OrderDelta },
#[serde(rename = "trade")]
Trade { data: FillEvent },
#[serde(rename = "order_update")]
OrderUpdate { data: Order },
#[serde(rename = "heartbeat")]
Heartbeat { timestamp: DateTime<Utc> },
#[serde(rename = "user_order_update")]
UserOrderUpdate { data: Order },
#[serde(rename = "user_trade")]
UserTrade { data: FillEvent },
#[serde(rename = "market_book_update")]
MarketBookUpdate { data: OrderDelta },
#[serde(rename = "market_trade")]
MarketTrade { data: FillEvent },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subscription {
pub token_ids: Vec<String>,
pub channels: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WssChannelType {
#[serde(rename = "USER")]
User,
#[serde(rename = "MARKET")]
Market,
}
impl WssChannelType {
pub fn as_str(&self) -> &'static str {
match self {
WssChannelType::User => "USER",
WssChannelType::Market => "MARKET",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Quote {
pub token_id: String,
pub side: Side,
#[serde(with = "rust_decimal::serde::str")]
pub price: Decimal,
pub timestamp: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Balance {
pub token_id: String,
pub available: Decimal,
pub locked: Decimal,
pub total: Decimal,
}
#[derive(Debug, Clone)]
pub struct Metrics {
pub orders_per_second: f64,
pub avg_latency_ms: f64,
pub error_rate: f64,
pub uptime_pct: f64,
}
pub type TokenId = String;
pub type OrderId = String;
pub type MarketId = String;
pub type ClientId = String;
#[derive(Debug, Clone)]
pub struct OpenOrderParams {
pub id: Option<String>,
pub asset_id: Option<String>,
pub market: Option<String>,
}
impl OpenOrderParams {
pub fn to_query_params(&self) -> Vec<(&str, &String)> {
let mut params = Vec::with_capacity(3);
if let Some(x) = &self.id {
params.push(("id", x));
}
if let Some(x) = &self.asset_id {
params.push(("asset_id", x));
}
if let Some(x) = &self.market {
params.push(("market", x));
}
params
}
}
#[derive(Debug, Clone)]
pub struct TradeParams {
pub id: Option<String>,
pub maker_address: Option<String>,
pub market: Option<String>,
pub asset_id: Option<String>,
pub before: Option<u64>,
pub after: Option<u64>,
}
impl TradeParams {
pub fn to_query_params(&self) -> Vec<(&str, String)> {
let mut params = Vec::with_capacity(6);
if let Some(x) = &self.id {
params.push(("id", x.clone()));
}
if let Some(x) = &self.asset_id {
params.push(("asset_id", x.clone()));
}
if let Some(x) = &self.market {
params.push(("market", x.clone()));
}
if let Some(x) = &self.maker_address {
params.push(("maker_address", x.clone()));
}
if let Some(x) = &self.before {
params.push(("before", x.to_string()));
}
if let Some(x) = &self.after {
params.push(("after", x.to_string()));
}
params
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenOrder {
pub associate_trades: Vec<String>,
pub id: String,
pub status: String,
pub market: String,
#[serde(with = "rust_decimal::serde::str")]
pub original_size: Decimal,
pub outcome: String,
pub maker_address: String,
pub owner: String,
#[serde(with = "rust_decimal::serde::str")]
pub price: Decimal,
pub side: Side,
#[serde(with = "rust_decimal::serde::str")]
pub size_matched: Decimal,
pub asset_id: String,
#[serde(deserialize_with = "crate::decode::deserializers::number_from_string")]
pub expiration: u64,
#[serde(rename = "type")]
pub order_type: OrderType,
#[serde(deserialize_with = "crate::decode::deserializers::number_from_string")]
pub created_at: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceAllowance {
pub asset_id: String,
#[serde(with = "rust_decimal::serde::str")]
pub balance: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub allowance: Decimal,
}
#[derive(Default)]
pub struct BalanceAllowanceParams {
pub asset_type: Option<AssetType>,
pub token_id: Option<String>,
pub signature_type: Option<u8>,
}
impl BalanceAllowanceParams {
pub fn to_query_params(&self) -> Vec<(&str, String)> {
let mut params = Vec::with_capacity(3);
if let Some(x) = &self.asset_type {
params.push(("asset_type", x.to_string()));
}
if let Some(x) = &self.token_id {
params.push(("token_id", x.to_string()));
}
if let Some(x) = &self.signature_type {
params.push(("signature_type", x.to_string()));
}
params
}
pub fn set_signature_type(&mut self, s: u8) {
self.signature_type = Some(s);
}
}
pub enum AssetType {
COLLATERAL,
CONDITIONAL,
}
impl fmt::Display for AssetType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AssetType::COLLATERAL => write!(f, "COLLATERAL"),
AssetType::CONDITIONAL => write!(f, "CONDITIONAL"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotificationParams {
pub signature: String,
pub timestamp: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchMidpointRequest {
pub token_ids: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchMidpointResponse {
pub midpoints: std::collections::HashMap<String, Option<Decimal>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchPriceRequest {
pub token_ids: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenPrice {
pub token_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub bid: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ask: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mid: Option<Decimal>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchPriceResponse {
pub prices: Vec<TokenPrice>,
}
#[derive(Debug, Deserialize)]
pub struct ApiKeysResponse {
#[serde(rename = "apiKeys")]
pub api_keys: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct MidpointResponse {
#[serde(with = "rust_decimal::serde::str")]
pub mid: Decimal,
}
#[derive(Debug, Deserialize)]
pub struct PriceResponse {
#[serde(with = "rust_decimal::serde::str")]
pub price: Decimal,
}
#[derive(Debug, Deserialize)]
pub struct SpreadResponse {
#[serde(with = "rust_decimal::serde::str")]
pub spread: Decimal,
}
#[derive(Debug, Deserialize)]
pub struct TickSizeResponse {
#[serde(with = "rust_decimal::serde::str")]
pub minimum_tick_size: Decimal,
}
#[derive(Debug, Deserialize)]
pub struct NegRiskResponse {
pub neg_risk: bool,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BookParams {
pub token_id: String,
pub side: Side,
}
#[derive(Debug, Deserialize, Clone)]
pub struct OrderBookSummary {
pub market: String,
pub asset_id: String,
pub hash: String,
#[serde(deserialize_with = "crate::decode::deserializers::number_from_string")]
pub timestamp: u64,
pub bids: Vec<OrderSummary>,
pub asks: Vec<OrderSummary>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct OrderSummary {
#[serde(with = "rust_decimal::serde::str")]
pub price: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub size: Decimal,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MarketsResponse {
#[serde(with = "rust_decimal::serde::str")]
pub limit: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub count: Decimal,
pub next_cursor: Option<String>,
pub data: Vec<Market>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SimplifiedMarketsResponse {
#[serde(with = "rust_decimal::serde::str")]
pub limit: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub count: Decimal,
pub next_cursor: Option<String>,
pub data: Vec<SimplifiedMarket>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimplifiedMarket {
#[serde(default)]
pub condition_id: String,
pub tokens: [Token; 2],
pub rewards: Rewards,
pub min_incentive_size: Option<String>,
pub max_incentive_spread: Option<String>,
pub active: bool,
pub closed: bool,
}
#[derive(Debug, Clone, Default)]
pub struct GammaListParams {
pub limit: Option<u32>,
pub offset: Option<u32>,
pub closed: Option<bool>,
pub tag_id: Option<String>,
pub exclude_tag_id: Option<String>,
pub related_tags: Option<String>,
pub order: Option<String>,
pub ascending: Option<bool>,
pub liquidity_num_min: Option<Decimal>,
pub end_date_max: Option<DateTime<Utc>>,
pub start_date_min: Option<DateTime<Utc>>,
}
impl GammaListParams {
pub fn builder() -> Self {
Self::default()
}
pub fn to_query_params(&self) -> Vec<(&str, String)> {
let mut params = Vec::with_capacity(8);
if let Some(limit) = self.limit {
params.push(("limit", limit.to_string()));
}
if let Some(offset) = self.offset {
params.push(("offset", offset.to_string()));
}
if let Some(closed) = self.closed {
params.push(("closed", closed.to_string()));
}
if let Some(tag_id) = &self.tag_id {
params.push(("tag_id", tag_id.clone()));
}
if let Some(exclude_tag_id) = &self.exclude_tag_id {
params.push(("exclude_tag_id", exclude_tag_id.clone()));
}
if let Some(related_tags) = &self.related_tags {
params.push(("related_tags", related_tags.clone()));
}
if let Some(order) = &self.order {
params.push(("order", order.clone()));
}
if let Some(ascending) = self.ascending {
params.push(("ascending", ascending.to_string()));
}
if let Some(liquidity_num_min) = &self.liquidity_num_min {
params.push(("liquidity_num_min", liquidity_num_min.to_string()));
}
if let Some(end_date_max) = &self.end_date_max {
params.push(("end_date_max", end_date_max.to_rfc3339()));
}
if let Some(start_date_min) = &self.start_date_min {
params.push(("start_date_min", start_date_min.to_rfc3339()));
}
params
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GammaEvent {
#[serde(rename = "event_id")]
pub id: String,
pub slug: String,
pub name: Option<String>,
pub description: Option<String>,
pub active: Option<bool>,
pub closed: Option<bool>,
pub start_date_iso: Option<String>,
pub end_date_iso: Option<String>,
pub sport: Option<String>,
#[serde(default)]
pub tags: Vec<Tag>,
#[serde(default)]
pub markets: Vec<SimplifiedMarket>,
#[serde(default)]
#[serde(flatten)]
pub metadata: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tag {
pub id: Option<String>,
pub slug: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
#[serde(default)]
#[serde(flatten)]
pub metadata: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sport {
pub id: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
#[serde(default)]
pub tag_ids: Vec<String>,
#[serde(default)]
pub tags: Vec<Tag>,
#[serde(default)]
#[serde(flatten)]
pub metadata: serde_json::Value,
}
#[derive(Debug, Clone, Deserialize)]
pub struct GammaMarket {
#[serde(rename = "conditionId")]
pub condition_id: String,
pub slug: String,
pub question: Option<String>,
pub description: Option<String>,
pub category: Option<String>,
pub active: bool,
pub closed: bool,
pub outcomes: Option<String>,
#[serde(rename = "clobTokenIds")]
pub clob_token_ids: Option<String>,
pub icon: Option<String>,
#[serde(rename = "endDate")]
pub end_date: Option<String>,
pub liquidity: Option<String>,
#[serde(rename = "liquidityNum")]
pub liquidity_num: Option<Decimal>,
pub volume: Option<String>,
#[serde(rename = "volumeNum")]
pub volume_num: Option<Decimal>,
#[serde(rename = "volume24hr")]
pub volume_24hr: Option<Decimal>,
#[serde(rename = "volume1wk")]
pub volume_1wk: Option<Decimal>,
#[serde(rename = "volume1mo")]
pub volume_1mo: Option<Decimal>,
#[serde(rename = "volume1yr")]
pub volume_1yr: Option<Decimal>,
#[serde(rename = "volume24hrAmm")]
pub volume_24hr_amm: Option<Decimal>,
#[serde(rename = "volume1wkAmm")]
pub volume_1wk_amm: Option<Decimal>,
#[serde(rename = "volume1moAmm")]
pub volume_1mo_amm: Option<Decimal>,
#[serde(rename = "volume1yrAmm")]
pub volume_1yr_amm: Option<Decimal>,
#[serde(rename = "volume24hrClob")]
pub volume_24hr_clob: Option<Decimal>,
#[serde(rename = "volume1wkClob")]
pub volume_1wk_clob: Option<Decimal>,
#[serde(rename = "volume1moClob")]
pub volume_1mo_clob: Option<Decimal>,
#[serde(rename = "volume1yrClob")]
pub volume_1yr_clob: Option<Decimal>,
#[serde(rename = "volumeAmm")]
pub volume_amm: Option<Decimal>,
#[serde(rename = "volumeClob")]
pub volume_clob: Option<Decimal>,
#[serde(rename = "liquidityAmm")]
pub liquidity_amm: Option<Decimal>,
#[serde(rename = "liquidityClob")]
pub liquidity_clob: Option<Decimal>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rewards {
pub rates: Option<serde_json::Value>,
#[serde(with = "rust_decimal::serde::str")]
pub min_size: Decimal,
#[serde(with = "rust_decimal::serde::str")]
pub max_spread: Decimal,
pub event_start_date: Option<String>,
pub event_end_date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub in_game_multiplier: Option<Decimal>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reward_epoch: Option<Decimal>,
}
pub type ClientResult<T> = anyhow::Result<T>;
pub type Result<T> = std::result::Result<T, crate::errors::PolyError>;