1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! This code was AUTOGENERATED using the Codama library.
use {
crate::types::{
AssetTier, HistoricalIndexData, HistoricalOracleData, InsuranceFund, MarketStatus,
OracleSource, PoolBalance,
},
solana_pubkey::Pubkey,
};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct SpotMarket {
/// The address of the spot market. It is a pda of the market index
pub pubkey: Pubkey,
/// The oracle used to price the markets deposits/borrows
pub oracle: Pubkey,
/// The token mint of the market
pub mint: Pubkey,
/// The vault used to store the market's deposits
/// The amount in the vault should be equal to or greater than deposits -
/// borrows
pub vault: Pubkey,
/// The encoded display name for the market e.g. SOL
pub name: [u8; 32],
pub historical_oracle_data: HistoricalOracleData,
pub historical_index_data: HistoricalIndexData,
/// Revenue the protocol has collected in this markets token
/// e.g. for SOL-PERP, funds can be settled in usdc and will flow into the
/// USDC revenue pool
pub revenue_pool: PoolBalance,
/// The fees collected from swaps between this market and the quote market
/// Is settled to the quote markets revenue pool
pub spot_fee_pool: PoolBalance,
/// Details on the insurance fund covering bankruptcies in this markets
/// token Covers bankruptcies for borrows with this markets token and
/// perps settling in this markets token
pub insurance_fund: InsuranceFund,
/// The total spot fees collected for this market
/// precision: QUOTE_PRECISION
pub total_spot_fee: u128,
/// The sum of the scaled balances for deposits across users and pool
/// balances To convert to the deposit token amount, multiply by the
/// cumulative deposit interest precision: SPOT_BALANCE_PRECISION
pub deposit_balance: u128,
/// The sum of the scaled balances for borrows across users and pool
/// balances To convert to the borrow token amount, multiply by the
/// cumulative borrow interest precision: SPOT_BALANCE_PRECISION
pub borrow_balance: u128,
/// The cumulative interest earned by depositors
/// Used to calculate the deposit token amount from the deposit balance
/// precision: SPOT_CUMULATIVE_INTEREST_PRECISION
pub cumulative_deposit_interest: u128,
/// The cumulative interest earned by borrowers
/// Used to calculate the borrow token amount from the borrow balance
/// precision: SPOT_CUMULATIVE_INTEREST_PRECISION
pub cumulative_borrow_interest: u128,
/// The total socialized loss from borrows, in the mint's token
/// precision: token mint precision
pub total_social_loss: u128,
/// The total socialized loss from borrows, in the quote market's token
/// preicision: QUOTE_PRECISION
pub total_quote_social_loss: u128,
/// no withdraw limits/guards when deposits below this threshold
/// precision: token mint precision
pub withdraw_guard_threshold: u64,
/// The max amount of token deposits in this market
/// 0 if there is no limit
/// precision: token mint precision
pub max_token_deposits: u64,
/// 24hr average of deposit token amount
/// precision: token mint precision
pub deposit_token_twap: u64,
/// 24hr average of borrow token amount
/// precision: token mint precision
pub borrow_token_twap: u64,
/// 24hr average of utilization
/// which is borrow amount over token amount
/// precision: SPOT_UTILIZATION_PRECISION
pub utilization_twap: u64,
/// Last time the cumulative deposit and borrow interest was updated
pub last_interest_ts: u64,
/// Last time the deposit/borrow/utilization averages were updated
pub last_twap_ts: u64,
/// The time the market is set to expire. Only set if market is in reduce
/// only mode
pub expiry_ts: i64,
/// Spot orders must be a multiple of the step size
/// precision: token mint precision
pub order_step_size: u64,
/// Spot orders must be a multiple of the tick size
/// precision: PRICE_PRECISION
pub order_tick_size: u64,
/// The minimum order size
/// precision: token mint precision
pub min_order_size: u64,
/// The maximum spot position size
/// if the limit is 0, there is no limit
/// precision: token mint precision
pub max_position_size: u64,
/// Every spot trade has a fill record id. This is the next id to use
pub next_fill_record_id: u64,
/// Every deposit has a deposit record id. This is the next id to use
pub next_deposit_record_id: u64,
/// The initial asset weight used to calculate a deposits contribution to a
/// users initial total collateral e.g. if the asset weight is .8, $100
/// of deposits contributes $80 to the users initial total collateral
/// precision: SPOT_WEIGHT_PRECISION
pub initial_asset_weight: u32,
/// The maintenance asset weight used to calculate a deposits contribution
/// to a users maintenance total collateral e.g. if the asset weight is
/// .9, $100 of deposits contributes $90 to the users maintenance total
/// collateral precision: SPOT_WEIGHT_PRECISION
pub maintenance_asset_weight: u32,
/// The initial liability weight used to calculate a borrows contribution to
/// a users initial margin requirement e.g. if the liability weight is
/// .9, $100 of borrows contributes $90 to the users initial margin
/// requirement precision: SPOT_WEIGHT_PRECISION
pub initial_liability_weight: u32,
/// The maintenance liability weight used to calculate a borrows
/// contribution to a users maintenance margin requirement e.g. if the
/// liability weight is .8, $100 of borrows contributes $80 to the users
/// maintenance margin requirement precision: SPOT_WEIGHT_PRECISION
pub maintenance_liability_weight: u32,
/// The initial margin fraction factor. Used to increase liability
/// weight/decrease asset weight for large positions precision:
/// MARGIN_PRECISION
pub imf_factor: u32,
/// The fee the liquidator is paid for taking over borrow/deposit
/// precision: LIQUIDATOR_FEE_PRECISION
pub liquidator_fee: u32,
/// The fee the insurance fund receives from liquidation
/// precision: LIQUIDATOR_FEE_PRECISION
pub if_liquidation_fee: u32,
/// The optimal utilization rate for this market.
/// Used to determine the markets borrow rate
/// precision: SPOT_UTILIZATION_PRECISION
pub optimal_utilization: u32,
/// The borrow rate for this market when the market has optimal utilization
/// precision: SPOT_RATE_PRECISION
pub optimal_borrow_rate: u32,
/// The borrow rate for this market when the market has 1000 utilization
/// precision: SPOT_RATE_PRECISION
pub max_borrow_rate: u32,
/// The market's token mint's decimals. To from decimals to a precision,
/// 10^decimals
pub decimals: u32,
pub market_index: u16,
/// Whether or not spot trading is enabled
pub orders_enabled: bool,
pub oracle_source: OracleSource,
pub status: MarketStatus,
/// The asset tier affects how a deposit can be used as collateral and the
/// priority for a borrow being liquidated
pub asset_tier: AssetTier,
pub paused_operations: u8,
pub if_paused_operations: u8,
pub fee_adjustment: i16,
/// What fraction of max_token_deposits
/// disabled when 0, 1 => 1/10000 => .01% of max_token_deposits
/// precision: X/10000
pub max_token_borrows_fraction: u16,
/// For swaps, the amount of token loaned out in the begin_swap ix
/// precision: token mint precision
pub flash_loan_amount: u64,
/// For swaps, the amount in the users token account in the begin_swap ix
/// Used to calculate how much of the token left the system in end_swap ix
/// precision: token mint precision
pub flash_loan_initial_token_amount: u64,
/// The total fees received from swaps
/// precision: token mint precision
pub total_swap_fee: u64,
/// When to begin scaling down the initial asset weight
/// disabled when 0
/// precision: QUOTE_PRECISION
pub scale_initial_asset_weight_start: u64,
/// The min borrow rate for this market when the market regardless of
/// utilization 1 => 1/200 => .5%
/// precision: X/200
pub min_borrow_rate: u8,
/// fuel multiplier for spot deposits
/// precision: 10
pub fuel_boost_deposits: u8,
/// fuel multiplier for spot borrows
/// precision: 10
pub fuel_boost_borrows: u8,
/// fuel multiplier for spot taker
/// precision: 10
pub fuel_boost_taker: u8,
/// fuel multiplier for spot maker
/// precision: 10
pub fuel_boost_maker: u8,
/// fuel multiplier for spot insurance stake
/// precision: 10
pub fuel_boost_insurance: u8,
pub token_program_flag: u8,
pub pool_id: u8,
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub padding: [u8; 40],
}
impl SpotMarket {
pub fn decode(data: &[u8]) -> Option<Self> {
if data.len() < 8 {
return None;
}
let discriminator = &data[0..8];
if discriminator != [100, 177, 8, 107, 168, 65, 65, 39] {
return None;
}
let mut data_slice = data;
data_slice = &data_slice[8..];
borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
}
}