Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/clob/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,14 @@ impl<'de> Deserialize<'de> for TickSize {
where
D: Deserializer<'de>,
{
let dec = <Decimal as Deserialize>::deserialize(deserializer)?;
// The Polymarket API returns `minimum_tick_size` as a JSON number
// (e.g. `0.01`), which `Decimal`'s default deserializer rejects
// because it expects a string. Delegate to the shared flexible
// Decimal deserializer so we handle both representations.
use serde_with::DeserializeAs;
let dec = <crate::serde_helpers::DecimalFromAny as DeserializeAs<Decimal>>::deserialize_as(
deserializer,
)?;
TickSize::try_from(dec).map_err(de::Error::custom)
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/data/types/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Deserializer};
use serde_with::{DefaultOnNull, DisplayFromStr, NoneAsEmptyString, serde_as};

use super::{ActivityType, Side};
use crate::serde_helpers::DecimalFromAny;
use crate::types::{Address, B256, Decimal, U256};

/// Deserializes an optional Side, treating empty strings as None.
Expand Down Expand Up @@ -74,24 +75,34 @@ pub struct Position {
/// The market condition ID (unique market identifier).
pub condition_id: B256,
/// Number of outcome tokens held.
#[serde_as(as = "DecimalFromAny")]
pub size: Decimal,
/// Average entry price for the position.
#[serde_as(as = "DecimalFromAny")]
pub avg_price: Decimal,
/// Initial value (cost basis) of the position.
#[serde_as(as = "DecimalFromAny")]
pub initial_value: Decimal,
/// Current market value of the position.
#[serde_as(as = "DecimalFromAny")]
pub current_value: Decimal,
/// Unrealized cash profit/loss.
#[serde_as(as = "DecimalFromAny")]
pub cash_pnl: Decimal,
/// Unrealized percentage profit/loss.
#[serde_as(as = "DecimalFromAny")]
pub percent_pnl: Decimal,
/// Total amount bought (cumulative).
#[serde_as(as = "DecimalFromAny")]
pub total_bought: Decimal,
/// Realized profit/loss from closed portions.
#[serde_as(as = "DecimalFromAny")]
pub realized_pnl: Decimal,
/// Realized percentage profit/loss.
#[serde_as(as = "DecimalFromAny")]
pub percent_realized_pnl: Decimal,
/// Current market price of the outcome.
#[serde_as(as = "DecimalFromAny")]
pub cur_price: Decimal,
/// Whether the position can be redeemed (market resolved).
pub redeemable: bool,
Expand Down Expand Up @@ -129,6 +140,7 @@ pub struct Position {
///
/// Returned by the `/closed-positions` endpoint. Represents positions that
/// have been fully sold or redeemed, with final profit/loss figures.
#[serde_as]
#[derive(Debug, Clone, Deserialize, Builder)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
Expand All @@ -140,12 +152,16 @@ pub struct ClosedPosition {
/// The market condition ID (unique market identifier).
pub condition_id: B256,
/// Average entry price for the position.
#[serde_as(as = "DecimalFromAny")]
pub avg_price: Decimal,
/// Total amount bought (cumulative).
#[serde_as(as = "DecimalFromAny")]
pub total_bought: Decimal,
/// Realized profit/loss from the closed position.
#[serde_as(as = "DecimalFromAny")]
pub realized_pnl: Decimal,
/// Final market price when position was closed.
#[serde_as(as = "DecimalFromAny")]
pub cur_price: Decimal,
/// Unix timestamp when the position was closed.
pub timestamp: i64,
Expand Down Expand Up @@ -187,8 +203,10 @@ pub struct Trade {
/// The market condition ID (unique market identifier).
pub condition_id: B256,
/// Number of tokens traded.
#[serde_as(as = "DecimalFromAny")]
pub size: Decimal,
/// Execution price per token.
#[serde_as(as = "DecimalFromAny")]
pub price: Decimal,
/// Unix timestamp when the trade occurred.
pub timestamp: i64,
Expand Down
60 changes: 59 additions & 1 deletion src/gamma/types/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_with::NoneAsEmptyString;
use serde_with::json::JsonString;
use serde_with::{DisplayFromStr, StringWithSeparator, formats::CommaSeparator, serde_as};

use crate::serde_helpers::StringFromAny;
use crate::serde_helpers::{DecimalFromAny, StringFromAny};
use crate::types::{Address, B256, Decimal, U256};

/// Image optimization metadata.
Expand Down Expand Up @@ -245,8 +245,11 @@ pub struct Event {
pub new: Option<bool>,
pub featured: Option<bool>,
pub restricted: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub open_interest: Option<Decimal>,
pub sort_by: Option<String>,
pub category: Option<String>,
Expand All @@ -260,10 +263,15 @@ pub struct Event {
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
pub comments_enabled: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub competitive: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_24hr: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1wk: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1mo: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1yr: Option<Decimal>,
pub featured_image: Option<String>,
pub disqus_thread: Option<String>,
Expand All @@ -274,7 +282,9 @@ pub struct Event {
#[serde_as(as = "Option<StringFromAny>")]
pub turn_provider_id: Option<String>,
pub enable_order_book: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity_clob: Option<Decimal>,
pub neg_risk: Option<bool>,
#[serde_as(as = "NoneAsEmptyString")]
Expand Down Expand Up @@ -317,7 +327,9 @@ pub struct Event {
pub cant_estimate: Option<bool>,
pub estimated_value: Option<String>,
pub templates: Option<Vec<Template>>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub spreads_main_line: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub totals_main_line: Option<Decimal>,
pub carousel_map: Option<String>,
pub pending_deployment: Option<bool>,
Expand Down Expand Up @@ -353,13 +365,15 @@ pub struct Market {
pub end_date: Option<DateTime<Utc>>,
pub category: Option<String>,
pub amm_type: Option<String>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity: Option<Decimal>,
pub sponsor_name: Option<String>,
pub sponsor_image: Option<String>,
pub start_date: Option<DateTime<Utc>>,
pub x_axis_value: Option<String>,
pub y_axis_value: Option<String>,
pub denomination_token: Option<U256>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub fee: Option<Decimal>,
pub image: Option<String>,
pub icon: Option<String>,
Expand All @@ -370,6 +384,7 @@ pub struct Market {
pub outcomes: Option<Vec<String>>,
#[serde_as(as = "Option<JsonString>")]
pub outcome_prices: Option<Vec<Decimal>>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume: Option<Decimal>,
pub active: Option<bool>,
pub market_type: Option<String>,
Expand Down Expand Up @@ -400,21 +415,29 @@ pub struct Market {
pub question_id: Option<B256>,
pub uma_end_date: Option<String>,
pub enable_order_book: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub order_price_min_tick_size: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub order_min_size: Option<Decimal>,
pub uma_resolution_status: Option<String>,
pub curation_order: Option<i32>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_num: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity_num: Option<Decimal>,
pub end_date_iso: Option<NaiveDate>,
pub start_date_iso: Option<NaiveDate>,
pub uma_end_date_iso: Option<NaiveDate>,
pub has_reviewed_dates: Option<bool>,
pub ready_for_cron: Option<bool>,
pub comments_enabled: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_24hr: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1wk: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1mo: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1yr: Option<Decimal>,
pub game_start_time: Option<String>,
pub seconds_delay: Option<i32>,
Expand All @@ -427,19 +450,32 @@ pub struct Market {
#[serde(rename = "teamBID")]
pub team_b_id: Option<String>,
pub uma_bond: Option<String>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub uma_reward: Option<Decimal>,
pub fpmm_live: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_24hr_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1wk_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1mo_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1yr_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_24hr_clob: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1wk_clob: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1mo_clob: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_1yr_clob: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_clob: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity_amm: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity_clob: Option<Decimal>,
pub maker_base_fee: Option<i32>,
pub taker_base_fee: Option<i32>,
Expand All @@ -460,18 +496,30 @@ pub struct Market {
pub ready_timestamp: Option<DateTime<Utc>>,
pub funded_timestamp: Option<DateTime<Utc>>,
pub accepting_orders_timestamp: Option<DateTime<Utc>>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub competitive: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub rewards_min_size: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub rewards_max_spread: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub spread: Option<Decimal>,
pub automatically_resolved: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub one_day_price_change: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub one_hour_price_change: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub one_week_price_change: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub one_month_price_change: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub one_year_price_change: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub last_trade_price: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub best_bid: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub best_ask: Option<Decimal>,
pub automatically_active: Option<bool>,
pub clear_book_on_start: Option<bool>,
Expand All @@ -484,6 +532,7 @@ pub struct Market {
pub game_id: Option<String>,
pub group_item_range: Option<String>,
pub sports_market_type: Option<String>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub line: Option<Decimal>,
pub uma_resolution_statuses: Option<String>,
pub pending_deployment: Option<bool>,
Expand Down Expand Up @@ -532,11 +581,14 @@ pub struct ClobReward {
pub condition_id: Option<B256>,
pub start_date: Option<NaiveDate>,
pub end_date: Option<NaiveDate>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub rewards_amount: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub rewards_daily_rate: Option<Decimal>,
}

/// A series of related events.
#[serde_as]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
Expand Down Expand Up @@ -566,9 +618,13 @@ pub struct Series {
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
pub comments_enabled: Option<bool>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub competitive: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume_24hr: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub volume: Option<Decimal>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub liquidity: Option<Decimal>,
pub start_date: Option<DateTime<Utc>>,
#[serde(rename = "pythTokenID")]
Expand All @@ -585,11 +641,13 @@ pub struct Series {
}

/// A comment position.
#[serde_as]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CommentPosition {
pub token_id: Option<U256>,
#[serde_as(as = "Option<DecimalFromAny>")]
pub position_size: Option<Decimal>,
}

Expand Down
Loading
Loading