Skip to main content

pyra_redis/kamino/
keys.rs

1use solana_pubkey::Pubkey;
2
3use crate::RedisKey;
4
5impl RedisKey {
6    // ── Kamino-specific prefixes ─────────────────────────────────────
7
8    pub const KAMINO_OBLIGATION_PREFIX: &'static str = "account:kamino:obligation";
9    pub const KAMINO_OBLIGATION_BY_PUBKEY_PREFIX: &'static str = "reverse:kamino_obligation_pubkey";
10    pub const KAMINO_RESERVE_PREFIX: &'static str = "account:kamino:reserve";
11
12    // ── Kamino-specific keys ─────────────────────────────────────────
13
14    pub fn kamino_obligation(vault_address: &Pubkey, lending_market: &Pubkey) -> Self {
15        let p = Self::KAMINO_OBLIGATION_PREFIX;
16        Self::from_string(format!("{p}:{vault_address}:{lending_market}"))
17    }
18
19    /// Reverse mapping: Kamino Obligation account pubkey → vault authority.
20    pub fn kamino_obligation_by_pubkey(obligation_pubkey: &Pubkey) -> Self {
21        let p = Self::KAMINO_OBLIGATION_BY_PUBKEY_PREFIX;
22        Self::from_string(format!("{p}:{obligation_pubkey}"))
23    }
24
25    pub fn kamino_reserve(reserve_pubkey: &Pubkey) -> Self {
26        let p = Self::KAMINO_RESERVE_PREFIX;
27        Self::from_string(format!("{p}:{reserve_pubkey}"))
28    }
29
30    // ── Glob patterns for SCAN ───────────────────────────────────────
31
32    pub fn kamino_obligation_glob() -> String {
33        Self::pattern(Self::KAMINO_OBLIGATION_PREFIX)
34    }
35
36    pub fn kamino_reserve_glob() -> String {
37        Self::pattern(Self::KAMINO_RESERVE_PREFIX)
38    }
39}
40
41#[cfg(test)]
42#[allow(
43    clippy::allow_attributes,
44    clippy::allow_attributes_without_reason,
45    clippy::unwrap_used,
46    clippy::expect_used,
47    clippy::panic,
48    clippy::arithmetic_side_effects
49)]
50mod tests {
51    use super::*;
52    use std::str::FromStr;
53
54    #[test]
55    fn kamino_obligation_key_format() {
56        let vault = Pubkey::from_str("11111111111111111111111111111111").unwrap();
57        let market = Pubkey::new_from_array([2; 32]);
58        let key = RedisKey::kamino_obligation(&vault, &market);
59        let expected = format!("account:kamino:obligation:{vault}:{market}");
60        assert_eq!(key.to_string(), expected);
61    }
62
63    #[test]
64    fn kamino_obligation_by_pubkey_key_format() {
65        let pk = Pubkey::from_str("11111111111111111111111111111111").unwrap();
66        let key = RedisKey::kamino_obligation_by_pubkey(&pk);
67        assert_eq!(
68            key.to_string(),
69            "reverse:kamino_obligation_pubkey:11111111111111111111111111111111"
70        );
71    }
72
73    #[test]
74    fn kamino_reserve_key_format() {
75        let pk = Pubkey::from_str("11111111111111111111111111111111").unwrap();
76        let key = RedisKey::kamino_reserve(&pk);
77        assert_eq!(
78            key.to_string(),
79            "account:kamino:reserve:11111111111111111111111111111111"
80        );
81    }
82
83    #[test]
84    fn kamino_obligation_glob_format() {
85        assert_eq!(
86            RedisKey::kamino_obligation_glob(),
87            "account:kamino:obligation:*"
88        );
89    }
90
91    #[test]
92    fn kamino_reserve_glob_format() {
93        assert_eq!(RedisKey::kamino_reserve_glob(), "account:kamino:reserve:*");
94    }
95}