Skip to main content

pyra_types/
drift.rs

1use serde::{Deserialize, Serialize};
2use solana_pubkey::Pubkey;
3
4/// Subset of the Drift SpotMarket account fields needed by consumer services.
5///
6/// Includes fields for margin weight calculations, interest rate computation,
7/// and oracle price lookups. Core fields (market_index, decimals, interest,
8/// margin weights) are required; optional fields used only by specific
9/// services use `#[serde(default)]` for flexibility.
10#[derive(Serialize, Deserialize, Clone, Debug)]
11pub struct SpotMarket {
12    /// Raw pubkey bytes of the market account (used by api-v2 for lookups).
13    #[serde(default)]
14    pub pubkey: Vec<u8>,
15
16    pub market_index: u16,
17
18    // --- Margin weight fields ---
19    pub initial_asset_weight: u32,
20
21    pub initial_liability_weight: u32,
22
23    pub imf_factor: u32,
24
25    pub scale_initial_asset_weight_start: u64,
26
27    // --- Token config ---
28    pub decimals: u32,
29
30    // --- Interest fields ---
31    pub cumulative_deposit_interest: u128,
32
33    pub cumulative_borrow_interest: u128,
34
35    #[serde(default)]
36    pub deposit_balance: u128,
37
38    #[serde(default)]
39    pub borrow_balance: u128,
40
41    // --- Rate calculation fields (used by api-v2 yield service) ---
42    #[serde(default)]
43    pub optimal_utilization: u32,
44
45    #[serde(default)]
46    pub optimal_borrow_rate: u32,
47
48    #[serde(default)]
49    pub max_borrow_rate: u32,
50
51    #[serde(default)]
52    pub min_borrow_rate: u8,
53
54    // --- Insurance fund (used by api-v2 yield service) ---
55    #[serde(default)]
56    pub insurance_fund: InsuranceFund,
57
58    // --- Oracle ---
59    #[serde(default)]
60    pub historical_oracle_data: HistoricalOracleData,
61
62    /// Oracle account pubkey (used by settlement-service for price lookups).
63    #[serde(default)]
64    pub oracle: Option<Pubkey>,
65}
66
67/// Subset of the Drift InsuranceFund struct.
68///
69/// Only `total_factor` is needed for deposit rate calculation.
70#[derive(Serialize, Deserialize, Clone, Debug, Default)]
71pub struct InsuranceFund {
72    #[serde(default)]
73    pub total_factor: u32,
74}
75
76/// Subset of the Drift HistoricalOracleData struct.
77#[derive(Serialize, Deserialize, Clone, Debug, Default)]
78pub struct HistoricalOracleData {
79    #[serde(default)]
80    pub last_oracle_price_twap5min: i64,
81}
82
83/// Drift user account containing spot positions.
84#[derive(Serialize, Deserialize, Clone, Debug)]
85pub struct DriftUser {
86    #[serde(default)]
87    pub authority: Pubkey,
88    pub spot_positions: Vec<SpotPosition>,
89}
90
91/// A single spot position within a Drift user account.
92#[derive(Serialize, Deserialize, Clone, Debug, Default)]
93pub struct SpotPosition {
94    pub scaled_balance: u64,
95    pub market_index: u16,
96
97    pub balance_type: SpotBalanceType,
98
99    #[serde(default)]
100    pub open_orders: u8,
101
102    #[serde(default)]
103    pub open_bids: i64,
104
105    #[serde(default)]
106    pub open_asks: i64,
107
108    #[serde(default)]
109    pub cumulative_deposits: i64,
110}
111
112/// Whether a spot balance is a deposit or borrow.
113#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Default)]
114pub enum SpotBalanceType {
115    #[default]
116    Deposit,
117    Borrow,
118}