use std::collections::HashMap;
use solana_pubkey::Pubkey;
use pyra_types::{Cache, SpendLimitsOrderAccount, Vault, WithdrawOrderAccount};
use crate::{RedisClient, RedisKey, RedisResult};
impl RedisClient {
pub async fn fetch_vault(&self, vault: &Pubkey) -> RedisResult<Option<Vault>> {
let key = RedisKey::vault(vault);
let cached: Option<Cache<Vault>> = self.get_json(key.as_str()).await?;
Ok(cached.map(|c| c.account))
}
pub async fn fetch_withdraw_order(
&self,
order: &Pubkey,
) -> RedisResult<Option<WithdrawOrderAccount>> {
let key = RedisKey::withdraw_order(order);
let cached: Option<Cache<WithdrawOrderAccount>> = self.get_json(key.as_str()).await?;
Ok(cached.map(|c| c.account))
}
pub async fn fetch_spend_limits_order(
&self,
order: &Pubkey,
) -> RedisResult<Option<SpendLimitsOrderAccount>> {
let key = RedisKey::spend_limits_order(order);
let cached: Option<Cache<SpendLimitsOrderAccount>> = self.get_json(key.as_str()).await?;
Ok(cached.map(|c| c.account))
}
pub async fn fetch_price(&self, market_index: u16) -> RedisResult<Option<f64>> {
let key = RedisKey::price(market_index);
self.get_json(key.as_str()).await
}
pub async fn fetch_prices(&self, market_indices: &[u16]) -> RedisResult<HashMap<u16, f64>> {
if market_indices.is_empty() {
return Ok(HashMap::new());
}
let keys: Vec<String> = market_indices
.iter()
.map(|&i| RedisKey::price(i).to_string())
.collect();
let values = self.mget(&keys).await?;
let mut result = HashMap::with_capacity(market_indices.len());
for (idx, val) in market_indices.iter().zip(values.into_iter()) {
if let Some(raw) = val {
if let Ok(price) = serde_json::from_str::<f64>(&raw) {
result.insert(*idx, price);
}
}
}
Ok(result)
}
}