polymarket-hft 0.0.7

A high-frequency trading system for Polymarket with built-in API clients (Data API, CLOB, CLOB WebSocket, Gamma, RTDS) and CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Helper functions for order creation.
//!
//! Provides rounding utilities, amount calculations, and market price calculations.

use alloy_primitives::{Address, U256};
use std::str::FromStr;

use super::builder::ExchangeOrderBuilder;
use super::constants::COLLATERAL_TOKEN_DECIMALS;
use super::types::{OrderData, Side, SignedOrder};
use crate::client::polymarket::clob::orderbook::PriceLevel;
use crate::client::polymarket::clob::types::{
    OrderType, TickSize, UserLimitOrder, UserMarketOrder,
};
use crate::error::{PolymarketError, Result};

// =============================================================================
// Rounding Configuration
// =============================================================================

/// Rounding configuration for price, size, and amount.
#[derive(Debug, Clone, Copy)]
pub struct RoundConfig {
    /// Decimal places for price.
    pub price: usize,
    /// Decimal places for size.
    pub size: usize,
    /// Decimal places for amount.
    pub amount: usize,
}

/// Gets the rounding configuration for a given tick size.
pub fn get_rounding_config(tick_size: TickSize) -> RoundConfig {
    match tick_size {
        TickSize::PointOne => RoundConfig {
            price: 1,
            size: 2,
            amount: 3,
        },
        TickSize::PointZeroOne => RoundConfig {
            price: 2,
            size: 2,
            amount: 4,
        },
        TickSize::PointZeroZeroOne => RoundConfig {
            price: 3,
            size: 2,
            amount: 5,
        },
        TickSize::PointZeroZeroZeroOne => RoundConfig {
            price: 4,
            size: 2,
            amount: 6,
        },
    }
}

// =============================================================================
// Rounding Utilities
// =============================================================================

/// Rounds a number to the specified decimal places (standard rounding).
pub fn round_normal(value: f64, decimals: usize) -> f64 {
    let multiplier = 10_f64.powi(decimals as i32);
    (value * multiplier).round() / multiplier
}

/// Rounds a number down to the specified decimal places.
pub fn round_down(value: f64, decimals: usize) -> f64 {
    let multiplier = 10_f64.powi(decimals as i32);
    (value * multiplier).floor() / multiplier
}

/// Rounds a number up to the specified decimal places.
pub fn round_up(value: f64, decimals: usize) -> f64 {
    let multiplier = 10_f64.powi(decimals as i32);
    (value * multiplier).ceil() / multiplier
}

/// Counts the number of decimal places in a number.
pub fn decimal_places(value: f64) -> usize {
    let s = format!("{:.15}", value);
    if let Some(pos) = s.find('.') {
        let decimal_part = s[pos + 1..].trim_end_matches('0');
        decimal_part.len()
    } else {
        0
    }
}

// =============================================================================
// Amount Calculations
// =============================================================================

/// Raw amounts for order creation.
#[derive(Debug, Clone)]
pub struct RawAmounts {
    /// Adjusted side.
    pub side: Side,
    /// Raw maker amount.
    pub raw_maker_amt: f64,
    /// Raw taker amount.
    pub raw_taker_amt: f64,
}

/// Calculates raw amounts for a limit order.
pub fn get_order_raw_amounts(
    side: Side,
    size: f64,
    price: f64,
    round_config: &RoundConfig,
) -> RawAmounts {
    let raw_price = round_normal(price, round_config.price);

    match side {
        Side::Buy => {
            let raw_taker_amt = round_down(size, round_config.size);
            let mut raw_maker_amt = raw_taker_amt * raw_price;

            if decimal_places(raw_maker_amt) > round_config.amount {
                raw_maker_amt = round_up(raw_maker_amt, round_config.amount + 4);
                if decimal_places(raw_maker_amt) > round_config.amount {
                    raw_maker_amt = round_down(raw_maker_amt, round_config.amount);
                }
            }

            RawAmounts {
                side: Side::Buy,
                raw_maker_amt,
                raw_taker_amt,
            }
        }
        Side::Sell => {
            let raw_maker_amt = round_down(size, round_config.size);
            let mut raw_taker_amt = raw_maker_amt * raw_price;

            if decimal_places(raw_taker_amt) > round_config.amount {
                raw_taker_amt = round_up(raw_taker_amt, round_config.amount + 4);
                if decimal_places(raw_taker_amt) > round_config.amount {
                    raw_taker_amt = round_down(raw_taker_amt, round_config.amount);
                }
            }

            RawAmounts {
                side: Side::Sell,
                raw_maker_amt,
                raw_taker_amt,
            }
        }
    }
}

/// Calculates raw amounts for a market order.
pub fn get_market_order_raw_amounts(
    side: Side,
    amount: f64,
    price: f64,
    round_config: &RoundConfig,
) -> RawAmounts {
    let raw_price = round_down(price, round_config.price);

    match side {
        Side::Buy => {
            let raw_maker_amt = round_down(amount, round_config.size);
            let mut raw_taker_amt = if raw_price > 0.0 {
                raw_maker_amt / raw_price
            } else {
                0.0
            };

            if decimal_places(raw_taker_amt) > round_config.amount {
                raw_taker_amt = round_up(raw_taker_amt, round_config.amount + 4);
                if decimal_places(raw_taker_amt) > round_config.amount {
                    raw_taker_amt = round_down(raw_taker_amt, round_config.amount);
                }
            }

            RawAmounts {
                side: Side::Buy,
                raw_maker_amt,
                raw_taker_amt,
            }
        }
        Side::Sell => {
            let raw_maker_amt = round_down(amount, round_config.size);
            let mut raw_taker_amt = raw_maker_amt * raw_price;

            if decimal_places(raw_taker_amt) > round_config.amount {
                raw_taker_amt = round_up(raw_taker_amt, round_config.amount + 4);
                if decimal_places(raw_taker_amt) > round_config.amount {
                    raw_taker_amt = round_down(raw_taker_amt, round_config.amount);
                }
            }

            RawAmounts {
                side: Side::Sell,
                raw_maker_amt,
                raw_taker_amt,
            }
        }
    }
}

/// Converts a float value to U256 with the given decimals.
pub fn parse_units(value: f64, decimals: u8) -> U256 {
    let multiplier = 10_f64.powi(decimals as i32);
    let raw_value = (value * multiplier) as u128;
    U256::from(raw_value)
}

// =============================================================================
// Market Price Calculation
// =============================================================================

/// Calculates the buy market price from ask positions.
pub fn calculate_buy_market_price(
    asks: &[PriceLevel],
    amount_to_match: f64,
    order_type: OrderType,
) -> Result<f64> {
    if asks.is_empty() {
        return Err(PolymarketError::other("No asks available in orderbook"));
    }

    let mut sum = 0.0;

    // Iterate from lowest ask to highest
    for ask in asks.iter() {
        let price: f64 = ask
            .price
            .parse()
            .map_err(|_| PolymarketError::other("Invalid price in orderbook"))?;
        let size: f64 = ask
            .size
            .parse()
            .map_err(|_| PolymarketError::other("Invalid size in orderbook"))?;

        sum += size * price;
        if sum >= amount_to_match {
            return Ok(price);
        }
    }

    // Not enough liquidity
    if order_type == OrderType::Fok {
        return Err(PolymarketError::other(
            "Insufficient liquidity for FOK order",
        ));
    }

    // For FAK, return the last price
    let last_price: f64 = asks
        .last()
        .unwrap()
        .price
        .parse()
        .map_err(|_| PolymarketError::other("Invalid price in orderbook"))?;
    Ok(last_price)
}

/// Calculates the sell market price from bid positions.
pub fn calculate_sell_market_price(
    bids: &[PriceLevel],
    amount_to_match: f64,
    order_type: OrderType,
) -> Result<f64> {
    if bids.is_empty() {
        return Err(PolymarketError::other("No bids available in orderbook"));
    }

    let mut sum = 0.0;

    // Iterate from highest bid to lowest
    for bid in bids.iter().rev() {
        let price: f64 = bid
            .price
            .parse()
            .map_err(|_| PolymarketError::other("Invalid price in orderbook"))?;
        let size: f64 = bid
            .size
            .parse()
            .map_err(|_| PolymarketError::other("Invalid size in orderbook"))?;

        sum += size;
        if sum >= amount_to_match {
            return Ok(price);
        }
    }

    // Not enough liquidity
    if order_type == OrderType::Fok {
        return Err(PolymarketError::other(
            "Insufficient liquidity for FOK order",
        ));
    }

    // For FAK, return the first price
    let first_price: f64 = bids
        .first()
        .unwrap()
        .price
        .parse()
        .map_err(|_| PolymarketError::other("Invalid price in orderbook"))?;
    Ok(first_price)
}

// =============================================================================
// Order Creation Helpers
// =============================================================================

/// Builds OrderData for a limit order.
pub fn build_limit_order_data(
    builder: &ExchangeOrderBuilder,
    user_order: &UserLimitOrder,
    tick_size: TickSize,
) -> Result<OrderData> {
    let round_config = get_rounding_config(tick_size);

    // Convert Side from pricing::Side to order_utils::Side
    let side = match user_order.side {
        crate::client::polymarket::clob::pricing::Side::Buy => Side::Buy,
        crate::client::polymarket::clob::pricing::Side::Sell => Side::Sell,
    };

    let raw_amounts = get_order_raw_amounts(side, user_order.size, user_order.price, &round_config);

    let maker_amount = parse_units(raw_amounts.raw_maker_amt, COLLATERAL_TOKEN_DECIMALS);
    let taker_amount = parse_units(raw_amounts.raw_taker_amt, COLLATERAL_TOKEN_DECIMALS);

    let taker = user_order.taker.unwrap_or(Address::ZERO);
    let fee_rate_bps = U256::from(user_order.fee_rate_bps.unwrap_or(0));
    let nonce = U256::from(user_order.nonce.unwrap_or(0));
    let expiration = user_order.expiration.map(U256::from);

    let token_id = U256::from_str(&user_order.token_id)
        .map_err(|e| PolymarketError::other(format!("Invalid token_id: {}", e)))?;

    Ok(OrderData {
        maker: builder.maker_address(),
        taker,
        token_id,
        maker_amount,
        taker_amount,
        side: raw_amounts.side,
        fee_rate_bps,
        nonce,
        signer: Some(builder.signer_address()),
        expiration,
        signature_type: None,
    })
}

/// Builds OrderData for a market order.
pub fn build_market_order_data(
    builder: &ExchangeOrderBuilder,
    user_order: &UserMarketOrder,
    tick_size: TickSize,
) -> Result<OrderData> {
    let round_config = get_rounding_config(tick_size);
    let price = user_order.price.unwrap_or(1.0);

    // Convert Side from pricing::Side to order_utils::Side
    let side = match user_order.side {
        crate::client::polymarket::clob::pricing::Side::Buy => Side::Buy,
        crate::client::polymarket::clob::pricing::Side::Sell => Side::Sell,
    };

    let raw_amounts = get_market_order_raw_amounts(side, user_order.amount, price, &round_config);

    let maker_amount = parse_units(raw_amounts.raw_maker_amt, COLLATERAL_TOKEN_DECIMALS);
    let taker_amount = parse_units(raw_amounts.raw_taker_amt, COLLATERAL_TOKEN_DECIMALS);

    let taker = user_order.taker.unwrap_or(Address::ZERO);
    let fee_rate_bps = U256::from(user_order.fee_rate_bps.unwrap_or(0));
    let nonce = U256::from(user_order.nonce.unwrap_or(0));

    let token_id = U256::from_str(&user_order.token_id)
        .map_err(|e| PolymarketError::other(format!("Invalid token_id: {}", e)))?;

    Ok(OrderData {
        maker: builder.maker_address(),
        taker,
        token_id,
        maker_amount,
        taker_amount,
        side: raw_amounts.side,
        fee_rate_bps,
        nonce,
        signer: Some(builder.signer_address()),
        expiration: Some(U256::ZERO),
        signature_type: None,
    })
}

/// Creates and signs a limit order.
pub async fn create_limit_order(
    builder: &ExchangeOrderBuilder,
    user_order: &UserLimitOrder,
    tick_size: TickSize,
    neg_risk: bool,
) -> Result<SignedOrder> {
    let order_data = build_limit_order_data(builder, user_order, tick_size)?;
    builder.build_signed_order(order_data, neg_risk).await
}

/// Creates and signs a market order.
pub async fn create_market_order(
    builder: &ExchangeOrderBuilder,
    user_order: &UserMarketOrder,
    tick_size: TickSize,
    neg_risk: bool,
) -> Result<SignedOrder> {
    let order_data = build_market_order_data(builder, user_order, tick_size)?;
    builder.build_signed_order(order_data, neg_risk).await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_rounding_config() {
        let config = get_rounding_config(TickSize::PointZeroOne);
        assert_eq!(config.price, 2);
        assert_eq!(config.size, 2);
        assert_eq!(config.amount, 4);
    }

    #[test]
    fn test_round_normal() {
        assert_eq!(round_normal(0.555, 2), 0.56);
        assert_eq!(round_normal(0.554, 2), 0.55);
    }

    #[test]
    fn test_round_down() {
        assert_eq!(round_down(0.559, 2), 0.55);
        assert_eq!(round_down(0.551, 2), 0.55);
    }

    #[test]
    fn test_round_up() {
        assert_eq!(round_up(0.551, 2), 0.56);
        // Due to floating-point precision, 0.55 * 100 slightly exceeds 55.0
        // ceil rounds to 56.0, so result is 0.56
        assert_eq!(round_up(0.55, 2), 0.56);
    }

    #[test]
    fn test_get_order_raw_amounts_buy() {
        let round_config = RoundConfig {
            price: 2,
            size: 2,
            amount: 4,
        };
        let result = get_order_raw_amounts(Side::Buy, 100.0, 0.55, &round_config);
        assert_eq!(result.side, Side::Buy);
        assert_eq!(result.raw_taker_amt, 100.0);
        assert_eq!(result.raw_maker_amt, 55.0);
    }

    #[test]
    fn test_get_order_raw_amounts_sell() {
        let round_config = RoundConfig {
            price: 2,
            size: 2,
            amount: 4,
        };
        let result = get_order_raw_amounts(Side::Sell, 100.0, 0.55, &round_config);
        assert_eq!(result.side, Side::Sell);
        assert_eq!(result.raw_maker_amt, 100.0);
        assert_eq!(result.raw_taker_amt, 55.0);
    }

    #[test]
    fn test_parse_units() {
        assert_eq!(parse_units(1.0, 6), U256::from(1_000_000u64));
        assert_eq!(parse_units(0.5, 6), U256::from(500_000u64));
    }
}