perpcity-sdk 0.2.1

Rust SDK for the PerpCity perpetual futures protocol on Base L2
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
//! Multi-layer TTL-based state cache for frequently-read on-chain data.
//!
//! Two TTL tiers match the data's expected rate of change on Base L2:
//!
//! | Layer | TTL | Data | Why |
//! |---|---|---|---|
//! | **Slow** | 60 s | Fees, bounds | Change only via governance |
//! | **Fast** | 2 s (1 block) | Mark prices, funding rates, USDC balance | Change every block |
//!
//! All methods take an explicit `now_ts` (Unix seconds) for deterministic
//! testing — no hidden clock dependencies.
//!
//! # Example
//!
//! ```
//! use perpcity_sdk::hft::state_cache::{StateCache, StateCacheConfig, CachedFees};
//!
//! let mut cache = StateCache::new(StateCacheConfig::default());
//! let addr = [0xAA; 20];
//! let fees = CachedFees {
//!     creator_fee: 0.001,
//!     insurance_fee: 0.0005,
//!     lp_fee: 0.003,
//!     liquidation_fee: 0.01,
//! };
//! cache.put_fees(addr, fees, 1000);
//! assert!(cache.get_fees(&addr, 1050).is_some()); // within 60s TTL
//! assert!(cache.get_fees(&addr, 1061).is_none()); // expired
//! ```

use std::collections::HashMap;

/// A cached value with an expiration timestamp.
#[derive(Debug, Clone, Copy)]
pub struct CachedValue<T> {
    /// The cached value.
    pub value: T,
    /// Unix timestamp (seconds) when this entry expires.
    pub expires_at: u64,
}

impl<T> CachedValue<T> {
    /// Check if the cached value is still valid at the given time.
    #[inline]
    pub fn is_valid(&self, now_ts: u64) -> bool {
        now_ts < self.expires_at
    }
}

/// Cached fee configuration for a perpetual market.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CachedFees {
    /// Creator fee (e.g. 0.001 = 0.1%).
    pub creator_fee: f64,
    /// Insurance fund fee.
    pub insurance_fee: f64,
    /// LP fee.
    pub lp_fee: f64,
    /// Liquidation fee.
    pub liquidation_fee: f64,
}

/// Cached position/leverage bounds for a perpetual market.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CachedBounds {
    /// Minimum margin in USDC.
    pub min_margin: f64,
    /// Minimum taker leverage.
    pub min_taker_leverage: f64,
    /// Maximum taker leverage.
    pub max_taker_leverage: f64,
    /// Liquidation margin ratio for takers.
    pub liquidation_taker_ratio: f64,
}

/// Configuration for [`StateCache`] TTL tiers.
#[derive(Debug, Clone, Copy)]
pub struct StateCacheConfig {
    /// TTL for slowly-changing data: fees, bounds (seconds).
    pub slow_ttl: u64,
    /// TTL for fast-changing data: prices, funding rates, balances (seconds).
    pub fast_ttl: u64,
}

impl Default for StateCacheConfig {
    fn default() -> Self {
        Self {
            slow_ttl: 60,
            fast_ttl: 2,
        }
    }
}

/// Multi-layer TTL cache for on-chain state.
///
/// Keyed by address (`[u8; 20]`) for per-market data, or by perp ID
/// (`[u8; 32]`) for per-perp data. The USDC balance is a singleton.
#[derive(Debug)]
pub struct StateCache {
    // Slow layer (60s TTL): governance-controlled
    fees: HashMap<[u8; 20], CachedValue<CachedFees>>,
    bounds: HashMap<[u8; 20], CachedValue<CachedBounds>>,

    // Fast layer (2s TTL): changes every block
    mark_prices: HashMap<[u8; 32], CachedValue<f64>>,
    funding_rates: HashMap<[u8; 32], CachedValue<f64>>,
    usdc_balance: Option<CachedValue<f64>>,

    slow_ttl: u64,
    fast_ttl: u64,
}

impl StateCache {
    /// Create a new cache with the given TTL configuration.
    pub fn new(config: StateCacheConfig) -> Self {
        Self {
            fees: HashMap::new(),
            bounds: HashMap::new(),
            mark_prices: HashMap::new(),
            funding_rates: HashMap::new(),
            usdc_balance: None,
            slow_ttl: config.slow_ttl,
            fast_ttl: config.fast_ttl,
        }
    }

    // ── Slow layer: fees ───────────────────────────────────────────

    /// Get cached fees for a market address, or `None` if stale/absent.
    #[inline]
    pub fn get_fees(&self, addr: &[u8; 20], now_ts: u64) -> Option<&CachedFees> {
        self.fees
            .get(addr)
            .filter(|cv| cv.is_valid(now_ts))
            .map(|cv| &cv.value)
    }

    /// Cache fees for a market address.
    pub fn put_fees(&mut self, addr: [u8; 20], value: CachedFees, now_ts: u64) {
        self.fees.insert(
            addr,
            CachedValue {
                value,
                expires_at: now_ts.saturating_add(self.slow_ttl),
            },
        );
    }

    // ── Slow layer: bounds ─────────────────────────────────────────

    /// Get cached bounds for a market address, or `None` if stale/absent.
    #[inline]
    pub fn get_bounds(&self, addr: &[u8; 20], now_ts: u64) -> Option<&CachedBounds> {
        self.bounds
            .get(addr)
            .filter(|cv| cv.is_valid(now_ts))
            .map(|cv| &cv.value)
    }

    /// Cache bounds for a market address.
    pub fn put_bounds(&mut self, addr: [u8; 20], value: CachedBounds, now_ts: u64) {
        self.bounds.insert(
            addr,
            CachedValue {
                value,
                expires_at: now_ts.saturating_add(self.slow_ttl),
            },
        );
    }

    // ── Fast layer: mark prices ────────────────────────────────────

    /// Get cached mark price for a perp, or `None` if stale/absent.
    #[inline]
    pub fn get_mark_price(&self, perp_id: &[u8; 32], now_ts: u64) -> Option<f64> {
        self.mark_prices
            .get(perp_id)
            .filter(|cv| cv.is_valid(now_ts))
            .map(|cv| cv.value)
    }

    /// Cache a mark price for a perp.
    pub fn put_mark_price(&mut self, perp_id: [u8; 32], price: f64, now_ts: u64) {
        self.mark_prices.insert(
            perp_id,
            CachedValue {
                value: price,
                expires_at: now_ts.saturating_add(self.fast_ttl),
            },
        );
    }

    // ── Fast layer: funding rates ──────────────────────────────────

    /// Get cached funding rate for a perp, or `None` if stale/absent.
    #[inline]
    pub fn get_funding_rate(&self, perp_id: &[u8; 32], now_ts: u64) -> Option<f64> {
        self.funding_rates
            .get(perp_id)
            .filter(|cv| cv.is_valid(now_ts))
            .map(|cv| cv.value)
    }

    /// Cache a funding rate for a perp.
    pub fn put_funding_rate(&mut self, perp_id: [u8; 32], rate: f64, now_ts: u64) {
        self.funding_rates.insert(
            perp_id,
            CachedValue {
                value: rate,
                expires_at: now_ts.saturating_add(self.fast_ttl),
            },
        );
    }

    // ── Fast layer: USDC balance ───────────────────────────────────

    /// Get cached USDC balance, or `None` if stale/absent.
    #[inline]
    pub fn get_usdc_balance(&self, now_ts: u64) -> Option<f64> {
        self.usdc_balance
            .filter(|cv| cv.is_valid(now_ts))
            .map(|cv| cv.value)
    }

    /// Cache the USDC balance.
    pub fn put_usdc_balance(&mut self, balance: f64, now_ts: u64) {
        self.usdc_balance = Some(CachedValue {
            value: balance,
            expires_at: now_ts.saturating_add(self.fast_ttl),
        });
    }

    // ── Invalidation ───────────────────────────────────────────────

    /// Invalidate all fast-layer data (prices, funding, balance).
    ///
    /// Call on new-block events. The slow layer (fees, bounds) is preserved.
    pub fn invalidate_fast_layer(&mut self) {
        self.mark_prices.clear();
        self.funding_rates.clear();
        self.usdc_balance = None;
    }

    /// Invalidate everything (both layers).
    pub fn invalidate_all(&mut self) {
        self.fees.clear();
        self.bounds.clear();
        self.invalidate_fast_layer();
    }
}

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

    fn sample_fees() -> CachedFees {
        CachedFees {
            creator_fee: 0.001,
            insurance_fee: 0.0005,
            lp_fee: 0.003,
            liquidation_fee: 0.01,
        }
    }

    fn sample_bounds() -> CachedBounds {
        CachedBounds {
            min_margin: 5.0,
            min_taker_leverage: 1.0,
            max_taker_leverage: 100.0,
            liquidation_taker_ratio: 0.05,
        }
    }

    #[test]
    fn empty_cache_returns_none() {
        let c = StateCache::new(StateCacheConfig::default());
        assert!(c.get_fees(&[0; 20], 0).is_none());
        assert!(c.get_bounds(&[0; 20], 0).is_none());
        assert!(c.get_mark_price(&[0; 32], 0).is_none());
        assert!(c.get_funding_rate(&[0; 32], 0).is_none());
        assert!(c.get_usdc_balance(0).is_none());
    }

    #[test]
    fn slow_layer_respects_ttl() {
        let mut c = StateCache::new(StateCacheConfig::default()); // slow_ttl = 60
        let addr = [0xAA; 20];

        c.put_fees(addr, sample_fees(), 1000);
        // Valid at 1059 (59s elapsed < 60s TTL)
        assert!(c.get_fees(&addr, 1059).is_some());
        // Expired at 1060 (60s elapsed >= 60s TTL)
        assert!(c.get_fees(&addr, 1060).is_none());
    }

    #[test]
    fn fast_layer_respects_ttl() {
        let mut c = StateCache::new(StateCacheConfig::default()); // fast_ttl = 2
        let perp = [0xBB; 32];

        c.put_mark_price(perp, 42000.0, 1000);
        assert_eq!(c.get_mark_price(&perp, 1001), Some(42000.0));
        assert!(c.get_mark_price(&perp, 1002).is_none());
    }

    #[test]
    fn funding_rate_ttl() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let perp = [0xCC; 32];

        c.put_funding_rate(perp, 0.0001, 500);
        assert_eq!(c.get_funding_rate(&perp, 501), Some(0.0001));
        assert!(c.get_funding_rate(&perp, 502).is_none());
    }

    #[test]
    fn usdc_balance_ttl() {
        let mut c = StateCache::new(StateCacheConfig::default());

        c.put_usdc_balance(10_000.0, 100);
        assert_eq!(c.get_usdc_balance(101), Some(10_000.0));
        assert!(c.get_usdc_balance(102).is_none());
    }

    #[test]
    fn bounds_caching() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let addr = [0xDD; 20];

        c.put_bounds(addr, sample_bounds(), 0);
        let b = c.get_bounds(&addr, 30).unwrap();
        assert_eq!(b.max_taker_leverage, 100.0);
        assert_eq!(b.min_margin, 5.0);
    }

    #[test]
    fn invalidate_fast_preserves_slow() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let addr = [0xAA; 20];
        let perp = [0xBB; 32];

        c.put_fees(addr, sample_fees(), 0);
        c.put_bounds(addr, sample_bounds(), 0);
        c.put_mark_price(perp, 42000.0, 0);
        c.put_funding_rate(perp, 0.0001, 0);
        c.put_usdc_balance(1000.0, 0);

        c.invalidate_fast_layer();

        // Slow layer survives
        assert!(c.get_fees(&addr, 0).is_some());
        assert!(c.get_bounds(&addr, 0).is_some());

        // Fast layer cleared
        assert!(c.get_mark_price(&perp, 0).is_none());
        assert!(c.get_funding_rate(&perp, 0).is_none());
        assert!(c.get_usdc_balance(0).is_none());
    }

    #[test]
    fn invalidate_all_clears_everything() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let addr = [0xAA; 20];
        let perp = [0xBB; 32];

        c.put_fees(addr, sample_fees(), 0);
        c.put_mark_price(perp, 42000.0, 0);

        c.invalidate_all();

        assert!(c.get_fees(&addr, 0).is_none());
        assert!(c.get_mark_price(&perp, 0).is_none());
    }

    #[test]
    fn overwrite_updates_value_and_ttl() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let perp = [0xBB; 32];

        c.put_mark_price(perp, 42000.0, 100);
        c.put_mark_price(perp, 43000.0, 200);

        // Old value gone (would have expired at 102), new value valid
        assert_eq!(c.get_mark_price(&perp, 201), Some(43000.0));
        assert!(c.get_mark_price(&perp, 202).is_none());
    }

    #[test]
    fn custom_config_ttls() {
        let config = StateCacheConfig {
            slow_ttl: 10,
            fast_ttl: 1,
        };
        let mut c = StateCache::new(config);
        let addr = [0xAA; 20];
        let perp = [0xBB; 32];

        c.put_fees(addr, sample_fees(), 0);
        c.put_mark_price(perp, 100.0, 0);

        // Custom slow TTL: 10s
        assert!(c.get_fees(&addr, 9).is_some());
        assert!(c.get_fees(&addr, 10).is_none());

        // Custom fast TTL: 1s
        assert!(c.get_mark_price(&perp, 0).is_some());
        assert!(c.get_mark_price(&perp, 1).is_none());
    }

    #[test]
    fn different_keys_independent() {
        let mut c = StateCache::new(StateCacheConfig::default());
        let perp_a = [0xAA; 32];
        let perp_b = [0xBB; 32];

        c.put_mark_price(perp_a, 100.0, 0);
        c.put_mark_price(perp_b, 200.0, 0);

        assert_eq!(c.get_mark_price(&perp_a, 0), Some(100.0));
        assert_eq!(c.get_mark_price(&perp_b, 0), Some(200.0));
    }

    #[test]
    fn cached_value_is_valid_boundary() {
        let cv = CachedValue {
            value: 42,
            expires_at: 100,
        };
        assert!(cv.is_valid(99)); // 1 second before
        assert!(!cv.is_valid(100)); // exactly at expiry
        assert!(!cv.is_valid(101)); // after expiry
    }
}