use std::cell::RefCell;
use std::collections::HashMap;
use std::future::Future;
use crate::utils::Lane;
use df_hyperliquid_core::utils::Address;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Slot {
Session,
Nonce(Lane),
}
impl Slot {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Slot::Session => "session",
Slot::Nonce(Lane::Exchange) => "nonce-exchange",
Slot::Nonce(Lane::Staking) => "nonce-staking",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("the key store: {0}")]
pub struct StoreError(pub String);
pub trait Store {
fn get(&self, wallet: Address, slot: Slot) -> impl Future<Output = Result<Option<Vec<u8>>, StoreError>>;
fn put(
&self,
wallet: Address,
slot: Slot,
bytes: Vec<u8>,
ttl_ms: Option<u64>,
) -> impl Future<Output = Result<(), StoreError>>;
}
#[derive(Debug, Default)]
pub struct MemoryStore {
kept: RefCell<HashMap<(Address, &'static str), Vec<u8>>>,
}
impl Store for MemoryStore {
fn get(&self, wallet: Address, slot: Slot) -> impl Future<Output = Result<Option<Vec<u8>>, StoreError>> {
std::future::ready(Ok(self.kept.borrow().get(&(wallet, slot.name())).cloned()))
}
fn put(
&self,
wallet: Address,
slot: Slot,
bytes: Vec<u8>,
_ttl_ms: Option<u64>,
) -> impl Future<Output = Result<(), StoreError>> {
self.kept.borrow_mut().insert((wallet, slot.name()), bytes);
std::future::ready(Ok(()))
}
}