Skip to main content

hypercall_db/traits/
tiers.rs

1//! User tier and margin mode persistence traits.
2//!
3//! Sync because tier reads happen at engine startup and writes go
4//! through the engine command loop.
5
6use anyhow::Result;
7use hypercall_types::{MarginMode, WalletAddress};
8
9use crate::{TierDefaultsRecord, UserTierRecord, UserTierUpdate};
10
11/// Read-only tier queries.
12pub trait TierReader: Send + Sync {
13    /// Get margin mode for a wallet, defaulting to Standard if no row exists.
14    fn get_margin_mode_sync(&self, wallet: &WalletAddress) -> Result<MarginMode>;
15    /// Get margin mode only if a tier row exists (returns None otherwise).
16    fn get_existing_margin_mode_sync(&self, wallet: &WalletAddress) -> Result<Option<MarginMode>>;
17    /// Load default rate limits for a tier name (e.g. "default", "mm").
18    fn get_tier_defaults_sync(&self, tier_name: &str) -> Result<Option<TierDefaultsRecord>>;
19    /// Load the full tier record for a wallet.
20    fn get_user_tier_sync(&self, wallet: &WalletAddress) -> Result<Option<UserTierRecord>>;
21    /// Load all user tiers across all wallets.
22    fn get_all_user_tiers_sync(&self) -> Result<Vec<UserTierRecord>>;
23}
24
25/// Tier mutations.
26pub trait TierWriter: TierReader {
27    /// Upsert a tier. None fields in UserTierUpdate are left unchanged in the DB.
28    fn save_user_tier_sync(&self, update: &UserTierUpdate) -> Result<()>;
29    /// Set margin mode for a wallet. Returns the new version counter.
30    fn set_margin_mode_sync(&self, wallet: &WalletAddress, margin_mode: MarginMode) -> Result<i64>;
31    /// Insert a tier row with margin mode only if one doesn't exist.
32    /// Returns the new version counter, or None if the row already existed.
33    fn insert_margin_mode_if_missing_sync(
34        &self,
35        wallet: &WalletAddress,
36        margin_mode: MarginMode,
37    ) -> Result<Option<i64>>;
38    /// Delete the tier row for a wallet.
39    fn delete_user_tier_sync(&self, wallet: &WalletAddress) -> Result<()>;
40}