Skip to main content

pyra_redis/drift/
keys.rs

1use pyra_tokens::AssetId;
2use solana_pubkey::Pubkey;
3
4use crate::RedisKey;
5
6impl RedisKey {
7    // ── Drift-specific prefixes ──────────────────────────────────────
8
9    pub const DRIFT_USER_PREFIX: &'static str = "account:drift:user";
10    pub const DRIFT_USER_BY_PUBKEY_PREFIX: &'static str = "reverse:drift_user_pubkey";
11    pub const DRIFT_SPOT_MARKET_PREFIX: &'static str = "account:drift:spot_market";
12
13    // ── Drift-specific keys ──────────────────────────────────────────
14
15    pub fn drift_user(authority: &Pubkey) -> Self {
16        let p = Self::DRIFT_USER_PREFIX;
17        Self::from_string(format!("{p}:{authority}"))
18    }
19
20    /// Reverse mapping: DriftUser account pubkey → authority (vault pubkey).
21    pub fn drift_user_by_pubkey(pubkey: &Pubkey) -> Self {
22        let p = Self::DRIFT_USER_BY_PUBKEY_PREFIX;
23        Self::from_string(format!("{p}:{pubkey}"))
24    }
25
26    pub fn drift_spot_market(asset_id: AssetId) -> Self {
27        let p = Self::DRIFT_SPOT_MARKET_PREFIX;
28        Self::from_string(format!("{p}:{asset_id}"))
29    }
30}
31
32#[cfg(test)]
33#[allow(
34    clippy::allow_attributes,
35    clippy::allow_attributes_without_reason,
36    clippy::unwrap_used,
37    clippy::expect_used,
38    clippy::panic,
39    clippy::arithmetic_side_effects
40)]
41mod tests {
42    use super::*;
43    use std::str::FromStr;
44
45    #[test]
46    fn drift_user_key_format() {
47        let pk = Pubkey::from_str("11111111111111111111111111111111").unwrap();
48        let key = RedisKey::drift_user(&pk);
49        assert_eq!(
50            key.to_string(),
51            "account:drift:user:11111111111111111111111111111111"
52        );
53    }
54
55    #[test]
56    fn drift_spot_market_key_format() {
57        let key = RedisKey::drift_spot_market(AssetId::USDC);
58        assert_eq!(key.to_string(), "account:drift:spot_market:0");
59    }
60
61    #[test]
62    fn drift_user_by_pubkey_key_format() {
63        let pk = Pubkey::from_str("11111111111111111111111111111111").unwrap();
64        let key = RedisKey::drift_user_by_pubkey(&pk);
65        assert_eq!(
66            key.to_string(),
67            "reverse:drift_user_pubkey:11111111111111111111111111111111"
68        );
69    }
70}