polyoxide-core 0.25.0

Core utilities and shared types for Polyoxide Polymarket API clients
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Per-signer token-bucket limits for CLOB trading.
//!
//! Polymarket evaluates order and cancellation requests against token buckets
//! keyed on the **signer address**, independently of the Cloudflare IP limits in
//! [`crate::rate_limit`]. A request must satisfy both layers.
//!
//! The two layers count different things: Cloudflare counts *requests*, this one
//! counts *orders*. For batch endpoints they diverge by the batch size, which is
//! why this module exists at all — [`RateLimiter`](crate::RateLimiter) charges
//! exactly one token per call and cannot express "this request costs 500".
//!
//! Transcribed from <https://docs.polymarket.com/api-reference/trading-rate-limits>
//! as fetched 2026-08-05; mirrored in `docs/specs/clob/trading-rate-limits.md`.

/// Which of a signer's two buckets a request draws from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TradingBucket {
    /// Order placement.
    Order,
    /// Order cancellation.
    Cancel,
}

/// An account's volume tier, which sets both buckets' rate and capacity.
///
/// Tier is assigned upstream from 30-day volume. Clients cannot compute it, so
/// it is discovered from the `Poly-RateLimit-Tier` response header; until one is
/// seen, [`Tier::Standard`] is assumed because it is the tightest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Tier {
    /// No volume requirement.
    #[default]
    Standard,
    /// $30,000+ 30-day volume.
    Copper,
    /// $50,000+ 30-day volume.
    Bronze,
    /// $100,000+ 30-day volume.
    Silver,
    /// $500,000+ 30-day volume.
    Gold,
    /// $2.5M+ 30-day volume.
    Platinum,
    /// $5M+ 30-day volume.
    Diamond,
    /// $10M+ 30-day volume.
    Elite,
}

impl Tier {
    /// Sustained refill rate for `bucket`, in tokens per second.
    pub fn rate(self, bucket: TradingBucket) -> u32 {
        let (order_rate, _, cancel_rate, _) = self.allowances();
        match bucket {
            TradingBucket::Order => order_rate,
            TradingBucket::Cancel => cancel_rate,
        }
    }

    /// Burst capacity for `bucket` — the most tokens it ever holds.
    ///
    /// A request costing more than this can never be satisfied, however long
    /// the caller waits.
    pub fn burst(self, bucket: TradingBucket) -> u32 {
        let (_, order_burst, _, cancel_burst) = self.allowances();
        match bucket {
            TradingBucket::Order => order_burst,
            TradingBucket::Cancel => cancel_burst,
        }
    }

    /// `(order rate, order burst, cancel rate, cancel burst)` as published.
    ///
    /// Transcribed literally rather than derived. The figures look regular —
    /// cancel is twice order throughout, burst is 1.5x rate — but Diamond
    /// breaks the second pattern (787, not 787.5), and a table copied from
    /// vendor docs is exactly where a clever formula goes stale unnoticed.
    fn allowances(self) -> (u32, u32, u32, u32) {
        match self {
            Tier::Standard => (40, 60, 80, 120),
            Tier::Copper => (60, 90, 120, 180),
            Tier::Bronze => (80, 120, 160, 240),
            Tier::Silver => (200, 300, 400, 600),
            Tier::Gold => (400, 600, 800, 1_200),
            Tier::Platinum => (450, 675, 900, 1_350),
            Tier::Diamond => (525, 787, 1_050, 1_575),
            Tier::Elite => (600, 900, 1_200, 1_800),
        }
    }

    /// Parse the value of a `Poly-RateLimit-Tier` header.
    ///
    /// Matching is case-insensitive; an unrecognised tier returns `None` rather
    /// than guessing, so an upstream addition cannot silently widen a bucket.
    pub fn from_header(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "standard" => Some(Tier::Standard),
            "copper" => Some(Tier::Copper),
            "bronze" => Some(Tier::Bronze),
            "silver" => Some(Tier::Silver),
            "gold" => Some(Tier::Gold),
            "platinum" => Some(Tier::Platinum),
            "diamond" => Some(Tier::Diamond),
            "elite" => Some(Tier::Elite),
            _ => None,
        }
    }
}

/// A trading request, carrying whatever its token cost depends on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TradingRequest {
    /// `POST /order` — always 1 token.
    PostOrder,
    /// `POST /orders` — one token per order in the batch.
    PostOrders {
        /// Number of orders in the batch.
        count: u32,
    },
    /// `DELETE /order` — always 1 token.
    CancelOrder,
    /// `DELETE /orders` — one token per submitted order ID.
    CancelOrders {
        /// Number of order IDs submitted.
        count: u32,
    },
    /// `DELETE /cancel-all` — costs `1 + orders canceled`.
    ///
    /// The true cost is not knowable client-side; see [`TradingRequest::cost`].
    CancelAll,
    /// `DELETE /cancel-market-orders` — costs `1 + matching orders canceled`.
    ///
    /// The true cost is not knowable client-side; see [`TradingRequest::cost`].
    CancelMarketOrders,
}

impl TradingRequest {
    /// Which bucket this request draws from.
    pub fn bucket(self) -> TradingBucket {
        match self {
            TradingRequest::PostOrder | TradingRequest::PostOrders { .. } => TradingBucket::Order,
            TradingRequest::CancelOrder
            | TradingRequest::CancelOrders { .. }
            | TradingRequest::CancelAll
            | TradingRequest::CancelMarketOrders => TradingBucket::Cancel,
        }
    }

    /// The token cost, as far as the client can know it.
    ///
    /// Exact for the four request kinds whose cost is a function of the payload.
    /// For [`CancelAll`](Self::CancelAll) and
    /// [`CancelMarketOrders`](Self::CancelMarketOrders) the published cost is
    /// `1 + orders canceled`, and the client does not know how many orders are
    /// open — so this returns the floor of 1. Those two can therefore overdraw
    /// the real bucket, and a resulting 429 is genuine rather than a client bug.
    pub fn cost(self) -> u32 {
        match self {
            TradingRequest::PostOrder | TradingRequest::CancelOrder => 1,
            TradingRequest::PostOrders { count } | TradingRequest::CancelOrders { count } => count,
            // Floor of the published `1 + orders canceled`.
            TradingRequest::CancelAll | TradingRequest::CancelMarketOrders => 1,
        }
    }

    /// Whether this request's cost is exactly known client-side.
    ///
    /// Only requests where this is true can be safely rejected before sending;
    /// the rest must be attempted and may come back 429.
    pub fn cost_is_exact(self) -> bool {
        !matches!(
            self,
            TradingRequest::CancelAll | TradingRequest::CancelMarketOrders
        )
    }
}

/// What the venue reported about a signer's trading allowance on one response.
///
/// Polymarket returns these on every evaluated order/cancel request. They are
/// the only way a client learns its own tier, since tier derives from 30-day
/// volume that the client cannot compute.
///
/// Every field is optional: the headers are absent on responses that never
/// reached the trading limiter (market data, auth, anything non-trading), and a
/// malformed value is dropped rather than guessed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RateLimitStatus {
    /// `Poly-RateLimit-Remaining` — token balance after this request.
    pub remaining: Option<u32>,
    /// `Poly-RateLimit-Reset` — Unix timestamp when the wait period ends.
    pub reset: Option<u64>,
    /// `Poly-RateLimit-Tier` — the tier the venue applied.
    pub tier: Option<Tier>,
    /// `Poly-RateLimit-Warning` — true when the venue is in warning mode.
    pub warning: bool,
}

impl RateLimitStatus {
    /// Read the `Poly-RateLimit-*` family from a response's headers.
    ///
    /// Returns an all-`None` value rather than an error when the headers are
    /// absent, so callers need not distinguish "not a trading request" from
    /// "trading request with no telemetry".
    pub fn from_headers(headers: &reqwest::header::HeaderMap) -> Self {
        let get = |name: &str| headers.get(name).and_then(|v| v.to_str().ok());

        Self {
            remaining: get("poly-ratelimit-remaining").and_then(|v| v.trim().parse().ok()),
            reset: get("poly-ratelimit-reset").and_then(|v| v.trim().parse().ok()),
            tier: get("poly-ratelimit-tier").and_then(Tier::from_header),
            warning: get("poly-ratelimit-warning")
                .is_some_and(|v| v.trim().eq_ignore_ascii_case("true")),
        }
    }

    /// Whether the venue reported anything at all.
    pub fn is_empty(&self) -> bool {
        self.remaining.is_none() && self.reset.is_none() && self.tier.is_none() && !self.warning
    }
}

/// A request whose token cost exceeds its bucket's capacity.
///
/// This is **not** a throttle. A token bucket never holds more than its burst
/// capacity, so a request costing more can never be satisfied no matter how long
/// the caller waits. Splitting the batch is the only remedy, which is why this
/// is a distinct error rather than a 429 the retry loop would burn attempts on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error(
    "batch costs {cost} tokens but the {bucket:?} bucket at tier {tier:?} holds at most \
     {capacity}; this can never succeed — split it into batches of {capacity} or fewer"
)]
pub struct BurstCapacityExceeded {
    /// Tokens the request would cost.
    pub cost: u32,
    /// The bucket's maximum capacity at the current tier.
    pub capacity: u32,
    /// Tier in force when the request was rejected.
    pub tier: Tier,
    /// Which bucket the request draws from.
    pub bucket: TradingBucket,
}

type DirectLimiter = governor::RateLimiter<
    governor::state::NotKeyed,
    governor::state::InMemoryState,
    governor::clock::DefaultClock,
>;

struct Buckets {
    tier: Tier,
    order: std::sync::Arc<DirectLimiter>,
    cancel: std::sync::Arc<DirectLimiter>,
}

impl Buckets {
    fn for_tier(tier: Tier) -> Self {
        let build = |bucket: TradingBucket| {
            let rate = tier.rate(bucket).max(1);
            let burst = tier.burst(bucket).max(1);
            let quota = governor::Quota::with_period(std::time::Duration::from_secs(1) / rate)
                .expect("per-token interval is non-zero")
                .allow_burst(std::num::NonZeroU32::new(burst).expect("burst is non-zero"));
            std::sync::Arc::new(DirectLimiter::direct(quota))
        };
        Self {
            tier,
            order: build(TradingBucket::Order),
            cancel: build(TradingBucket::Cancel),
        }
    }
}

/// Per-signer trading limiter, with the tier discovered from response headers.
///
/// Starts at [`Tier::Standard`] — the tightest — and resizes both buckets the
/// first time a `Poly-RateLimit-Tier` header reports something different.
///
/// # The resize discards accumulated state
///
/// governor buckets cannot be resized in place, so adopting a new tier replaces
/// them with fresh ones at full capacity. Moving *up* a tier is therefore safe:
/// the venue already permits the wider allowance. Moving *down* briefly permits
/// a full burst at the narrower capacity, which the venue may throttle. Tier
/// changes are rare (they track 30-day volume), so this is preferred to the
/// complexity of draining the old bucket into the new one.
#[derive(Clone)]
pub struct SignerLimiter {
    inner: std::sync::Arc<SignerLimiterInner>,
}

struct SignerLimiterInner {
    buckets: std::sync::RwLock<Buckets>,
    status: std::sync::RwLock<RateLimitStatus>,
}

impl std::fmt::Debug for SignerLimiter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SignerLimiter")
            .field("tier", &self.tier())
            .finish()
    }
}

impl Default for SignerLimiter {
    fn default() -> Self {
        Self::new()
    }
}

impl SignerLimiter {
    /// Create a limiter at the default (tightest) tier.
    pub fn new() -> Self {
        Self {
            inner: std::sync::Arc::new(SignerLimiterInner {
                buckets: std::sync::RwLock::new(Buckets::for_tier(Tier::default())),
                status: std::sync::RwLock::new(RateLimitStatus::default()),
            }),
        }
    }

    /// The tier currently in force.
    pub fn tier(&self) -> Tier {
        self.inner
            .buckets
            .read()
            .expect("lock is never poisoned")
            .tier
    }

    /// The most recent `Poly-RateLimit-*` telemetry seen.
    pub fn last_status(&self) -> RateLimitStatus {
        *self.inner.status.read().expect("lock is never poisoned")
    }

    /// Record the rate-limit headers from a response, adopting a reported tier.
    ///
    /// Responses carrying none of the headers are ignored, so non-trading
    /// requests cannot clear the telemetry.
    pub fn observe(&self, headers: &reqwest::header::HeaderMap) {
        let status = RateLimitStatus::from_headers(headers);
        if status.is_empty() {
            return;
        }
        *self.inner.status.write().expect("lock is never poisoned") = status;

        // An unrecognised tier parses to None and so leaves the buckets alone,
        // keeping the tighter allowance rather than guessing a wider one.
        if let Some(tier) = status.tier {
            let mut buckets = self.inner.buckets.write().expect("lock is never poisoned");
            if buckets.tier != tier {
                tracing::debug!("adopting rate limit tier {tier:?} (was {:?})", buckets.tier);
                *buckets = Buckets::for_tier(tier);
            }
        }
    }

    /// Wait for `request`'s token cost to be available, then consume it.
    ///
    /// # Errors
    ///
    /// [`BurstCapacityExceeded`] when the cost exceeds the bucket's capacity.
    /// This returns immediately rather than waiting forever.
    pub async fn acquire(&self, request: TradingRequest) -> Result<(), BurstCapacityExceeded> {
        let bucket = request.bucket();

        // Clone the Arc out under the lock and drop the guard before awaiting:
        // an RwLock guard held across an await would make this future !Send.
        let (tier, limiter) = {
            let buckets = self.inner.buckets.read().expect("lock is never poisoned");
            let limiter = match bucket {
                TradingBucket::Order => buckets.order.clone(),
                TradingBucket::Cancel => buckets.cancel.clone(),
            };
            (buckets.tier, limiter)
        };

        let cost = request.cost().max(1);
        let n = std::num::NonZeroU32::new(cost).expect("cost floor is 1");

        // governor reports InsufficientCapacity when n exceeds the bucket's
        // burst — exactly the permanently-impossible case, and it returns
        // straight away rather than parking the task forever.
        limiter
            .until_n_ready(n)
            .await
            .map_err(|_| BurstCapacityExceeded {
                cost,
                capacity: tier.burst(bucket),
                tier,
                bucket,
            })
    }
}

#[cfg(test)]
mod limiter_tests {
    use super::*;
    use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
    use std::time::Duration;

    fn tier_header(tier: &str) -> HeaderMap {
        let mut map = HeaderMap::new();
        map.insert(
            HeaderName::from_static("poly-ratelimit-tier"),
            HeaderValue::from_str(tier).unwrap(),
        );
        map
    }

    #[test]
    fn starts_at_the_tightest_tier() {
        assert_eq!(SignerLimiter::new().tier(), Tier::Standard);
    }

    #[test]
    fn observing_a_tier_header_adopts_it() {
        let limiter = SignerLimiter::new();
        limiter.observe(&tier_header("gold"));
        assert_eq!(limiter.tier(), Tier::Gold);
        assert_eq!(limiter.last_status().tier, Some(Tier::Gold));
    }

    #[test]
    fn an_unrecognised_tier_leaves_the_current_one_in_force() {
        // Adopting an unknown tier would mean guessing a capacity. Staying put
        // keeps the tighter, known-safe allowance.
        let limiter = SignerLimiter::new();
        limiter.observe(&tier_header("silver"));
        limiter.observe(&tier_header("titanium"));
        assert_eq!(limiter.tier(), Tier::Silver);
    }

    #[test]
    fn a_response_without_the_headers_does_not_clear_telemetry() {
        let limiter = SignerLimiter::new();
        limiter.observe(&tier_header("silver"));
        limiter.observe(&HeaderMap::new());
        assert_eq!(limiter.tier(), Tier::Silver);
        assert_eq!(limiter.last_status().tier, Some(Tier::Silver));
    }

    #[tokio::test]
    async fn an_over_capacity_batch_is_rejected_immediately_not_queued() {
        // The headline case: 2,000 IDs exceeds every tier's cancel burst, so
        // waiting can never help. It must come back as an error, fast.
        let limiter = SignerLimiter::new();
        let request = TradingRequest::CancelOrders { count: 2_000 };

        let result = tokio::time::timeout(Duration::from_millis(100), limiter.acquire(request))
            .await
            .expect("must not hang waiting for capacity that can never exist");

        let err = result.expect_err("2,000 tokens exceeds Standard's 120 cancel burst");
        assert_eq!(err.cost, 2_000);
        assert_eq!(err.capacity, 120);
        assert_eq!(err.bucket, TradingBucket::Cancel);
    }

    #[tokio::test]
    async fn a_batch_within_capacity_is_admitted() {
        let limiter = SignerLimiter::new();
        limiter
            .acquire(TradingRequest::CancelOrders { count: 100 })
            .await
            .expect("100 fits Standard's 120 cancel burst");
    }

    #[tokio::test]
    async fn adopting_a_higher_tier_admits_a_batch_that_was_impossible() {
        // Proves the resize actually changes capacity rather than just the
        // reported tier: 500 IDs is impossible at Standard (120) and fine at
        // Gold (1,200).
        let limiter = SignerLimiter::new();
        let batch = TradingRequest::CancelOrders { count: 500 };
        assert!(limiter.acquire(batch).await.is_err());

        limiter.observe(&tier_header("gold"));
        limiter
            .acquire(batch)
            .await
            .expect("500 fits Gold's 1,200 cancel burst");
    }

    #[tokio::test]
    async fn the_order_and_cancel_buckets_are_independent() {
        // Draining orders must not throttle cancels — they are separate buckets
        // upstream, and conflating them would block cancels during a burst of
        // order placement, which is exactly when cancelling matters most.
        let limiter = SignerLimiter::new();
        limiter
            .acquire(TradingRequest::PostOrders { count: 60 })
            .await
            .expect("60 fills Standard's order burst exactly");

        let start = std::time::Instant::now();
        limiter
            .acquire(TradingRequest::CancelOrder)
            .await
            .expect("cancel bucket is untouched");
        assert!(
            start.elapsed() < Duration::from_millis(25),
            "cancelling was throttled by order placement"
        );
    }

    #[tokio::test]
    async fn batch_cost_is_charged_in_full_not_as_one_request() {
        // The whole point of this layer. Draining the order burst with one
        // 60-order batch must leave the next single order waiting — if batches
        // were charged as 1, this would return instantly.
        let limiter = SignerLimiter::new();
        limiter
            .acquire(TradingRequest::PostOrders { count: 60 })
            .await
            .unwrap();

        let start = std::time::Instant::now();
        limiter.acquire(TradingRequest::PostOrder).await.unwrap();
        assert!(
            start.elapsed() >= Duration::from_millis(10),
            "a 60-order batch was charged as a single token"
        );
    }
}

#[cfg(test)]
mod status_tests {
    use super::*;
    use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

    fn headers(pairs: &[(&str, &str)]) -> HeaderMap {
        let mut map = HeaderMap::new();
        for (k, v) in pairs {
            map.insert(
                HeaderName::from_bytes(k.as_bytes()).unwrap(),
                HeaderValue::from_str(v).unwrap(),
            );
        }
        map
    }

    #[test]
    fn reads_the_full_header_family() {
        let status = RateLimitStatus::from_headers(&headers(&[
            ("poly-ratelimit-remaining", "57"),
            ("poly-ratelimit-reset", "1767225660"),
            ("poly-ratelimit-tier", "silver"),
            ("poly-ratelimit-warning", "true"),
        ]));

        assert_eq!(status.remaining, Some(57));
        assert_eq!(status.reset, Some(1_767_225_660));
        assert_eq!(status.tier, Some(Tier::Silver));
        assert!(status.warning);
        assert!(!status.is_empty());
    }

    #[test]
    fn header_names_are_matched_case_insensitively() {
        // reqwest lowercases header names on receipt, but a HeaderMap built by
        // hand (or a proxy preserving case) must resolve identically.
        let status = RateLimitStatus::from_headers(&headers(&[
            ("Poly-RateLimit-Tier", "GOLD"),
            ("POLY-RATELIMIT-REMAINING", "3"),
        ]));
        assert_eq!(status.tier, Some(Tier::Gold));
        assert_eq!(status.remaining, Some(3));
    }

    #[test]
    fn absent_headers_yield_an_empty_status_not_an_error() {
        let status = RateLimitStatus::from_headers(&HeaderMap::new());
        assert!(status.is_empty());
        assert_eq!(status, RateLimitStatus::default());
    }

    #[test]
    fn malformed_values_are_dropped_rather_than_guessed() {
        // A garbage tier must not fall back to Standard here — that would be
        // indistinguishable from the venue actually reporting Standard, and the
        // limiter would resize a bucket on noise.
        let status = RateLimitStatus::from_headers(&headers(&[
            ("poly-ratelimit-remaining", "not-a-number"),
            ("poly-ratelimit-reset", ""),
            ("poly-ratelimit-tier", "titanium"),
            ("poly-ratelimit-warning", "false"),
        ]));

        assert_eq!(status.remaining, None);
        assert_eq!(status.reset, None);
        assert_eq!(status.tier, None);
        assert!(!status.warning);
    }
}

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

    /// The published tier table, transcribed by hand. This is the golden vector.
    ///
    /// `(tier, order rate, order burst, cancel rate, cancel burst)`
    fn published() -> Vec<(Tier, u32, u32, u32, u32)> {
        vec![
            (Tier::Standard, 40, 60, 80, 120),
            (Tier::Copper, 60, 90, 120, 180),
            (Tier::Bronze, 80, 120, 160, 240),
            (Tier::Silver, 200, 300, 400, 600),
            (Tier::Gold, 400, 600, 800, 1_200),
            (Tier::Platinum, 450, 675, 900, 1_350),
            (Tier::Diamond, 525, 787, 1_050, 1_575),
            (Tier::Elite, 600, 900, 1_200, 1_800),
        ]
    }

    #[test]
    fn every_tier_matches_the_published_table() {
        for (tier, o_rate, o_burst, c_rate, c_burst) in published() {
            assert_eq!(
                tier.rate(TradingBucket::Order),
                o_rate,
                "{tier:?} order rate"
            );
            assert_eq!(
                tier.burst(TradingBucket::Order),
                o_burst,
                "{tier:?} order burst"
            );
            assert_eq!(
                tier.rate(TradingBucket::Cancel),
                c_rate,
                "{tier:?} cancel rate"
            );
            assert_eq!(
                tier.burst(TradingBucket::Cancel),
                c_burst,
                "{tier:?} cancel burst"
            );
        }
    }

    #[test]
    fn the_default_tier_is_the_tightest_one() {
        // An unconfigured client must never assume more allowance than it has.
        let default = Tier::default();
        for (tier, ..) in published() {
            assert!(
                default.rate(TradingBucket::Order) <= tier.rate(TradingBucket::Order),
                "default tier {default:?} is looser than {tier:?}"
            );
            assert!(
                default.burst(TradingBucket::Cancel) <= tier.burst(TradingBucket::Cancel),
                "default tier {default:?} bursts higher than {tier:?}"
            );
        }
    }

    #[test]
    fn tier_headers_parse_case_insensitively() {
        assert_eq!(Tier::from_header("standard"), Some(Tier::Standard));
        assert_eq!(Tier::from_header("Silver"), Some(Tier::Silver));
        assert_eq!(Tier::from_header("ELITE"), Some(Tier::Elite));
    }

    #[test]
    fn an_unknown_tier_header_is_not_guessed() {
        // Guessing would widen a bucket on an upstream addition we know nothing
        // about. Returning None keeps the current (tighter) tier in force.
        assert_eq!(Tier::from_header("titanium"), None);
        assert_eq!(Tier::from_header(""), None);
    }

    #[test]
    fn batch_costs_scale_with_the_payload() {
        assert_eq!(TradingRequest::PostOrder.cost(), 1);
        assert_eq!(TradingRequest::PostOrders { count: 40 }.cost(), 40);
        assert_eq!(TradingRequest::CancelOrder.cost(), 1);
        assert_eq!(TradingRequest::CancelOrders { count: 250 }.cost(), 250);
    }

    #[test]
    fn requests_draw_from_the_right_bucket() {
        assert_eq!(TradingRequest::PostOrder.bucket(), TradingBucket::Order);
        assert_eq!(
            TradingRequest::PostOrders { count: 2 }.bucket(),
            TradingBucket::Order
        );
        assert_eq!(TradingRequest::CancelOrder.bucket(), TradingBucket::Cancel);
        assert_eq!(
            TradingRequest::CancelOrders { count: 2 }.bucket(),
            TradingBucket::Cancel
        );
        assert_eq!(TradingRequest::CancelAll.bucket(), TradingBucket::Cancel);
        assert_eq!(
            TradingRequest::CancelMarketOrders.bucket(),
            TradingBucket::Cancel
        );
    }

    #[test]
    fn cancel_all_reports_a_floor_cost_and_says_so() {
        // Published cost is 1 + orders canceled, which the client cannot know.
        // Reporting 1 while flagging it inexact is the honest answer; claiming
        // exactness here would let the guard reject or admit batches wrongly.
        assert_eq!(TradingRequest::CancelAll.cost(), 1);
        assert!(!TradingRequest::CancelAll.cost_is_exact());
        assert_eq!(TradingRequest::CancelMarketOrders.cost(), 1);
        assert!(!TradingRequest::CancelMarketOrders.cost_is_exact());

        for exact in [
            TradingRequest::PostOrder,
            TradingRequest::PostOrders { count: 3 },
            TradingRequest::CancelOrder,
            TradingRequest::CancelOrders { count: 3 },
        ] {
            assert!(exact.cost_is_exact(), "{exact:?} cost is computable");
        }
    }

    #[test]
    fn a_batch_larger_than_elite_burst_is_impossible_on_every_tier() {
        // The finding that motivated this module: a token bucket never holds
        // more than its capacity, so an over-capacity batch is permanently
        // rejected rather than throttled. 2,000 IDs exceeds every cancel burst.
        let batch = TradingRequest::CancelOrders { count: 2_000 };
        for (tier, ..) in published() {
            assert!(
                batch.cost() > tier.burst(batch.bucket()),
                "{tier:?} could absorb a 2,000-ID batch — check the published table"
            );
        }
    }
}