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
//! This code was AUTOGENERATED using the Codama library.
use {
crate::types::{ContractTier, ContractType, InsuranceClaim, MarketStatus, PoolBalance, AMM},
solana_pubkey::Pubkey,
};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct PerpMarket {
/// The perp market's address. It is a pda of the market index
pub pubkey: Pubkey,
/// The automated market maker
pub amm: AMM,
/// The market's pnl pool. When users settle negative pnl, the balance
/// increases. When users settle positive pnl, the balance decreases.
/// Can not go negative.
pub pnl_pool: PoolBalance,
/// Encoded display name for the perp market e.g. SOL-PERP
pub name: [u8; 32],
/// The perp market's claim on the insurance fund
pub insurance_claim: InsuranceClaim,
/// The max pnl imbalance before positive pnl asset weight is discounted
/// pnl imbalance is the difference between long and short pnl. When it's
/// greater than 0, the amm has negative pnl and the initial asset
/// weight for positive pnl is discounted precision = QUOTE_PRECISION
pub unrealized_pnl_max_imbalance: u64,
/// The ts when the market will be expired. Only set if market is in reduce
/// only mode
pub expiry_ts: i64,
/// The price at which positions will be settled. Only set if market is
/// expired precision = PRICE_PRECISION
pub expiry_price: i64,
/// Every trade has a fill record id. This is the next id to be used
pub next_fill_record_id: u64,
/// Every funding rate update has a record id. This is the next id to be
/// used
pub next_funding_rate_record_id: u64,
/// Every amm k updated has a record id. This is the next id to be used
pub next_curve_record_id: u64,
/// The initial margin fraction factor. Used to increase margin ratio for
/// large positions precision: MARGIN_PRECISION
pub imf_factor: u32,
/// The imf factor for unrealized pnl. Used to discount asset weight for
/// large positive pnl precision: MARGIN_PRECISION
pub unrealized_pnl_imf_factor: u32,
/// The fee the liquidator is paid for taking over perp position
/// 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 margin ratio which determines how much collateral is required to
/// open a position e.g. margin ratio of .1 means a user must have $100
/// of total collateral to open a $1000 position precision:
/// MARGIN_PRECISION
pub margin_ratio_initial: u32,
/// The margin ratio which determines when a user will be liquidated
/// e.g. margin ratio of .05 means a user must have $50 of total collateral
/// to maintain a $1000 position else they will be liquidated
/// precision: MARGIN_PRECISION
pub margin_ratio_maintenance: u32,
/// The initial asset weight for positive pnl. Negative pnl always has an
/// asset weight of 1 precision: SPOT_WEIGHT_PRECISION
pub unrealized_pnl_initial_asset_weight: u32,
/// The maintenance asset weight for positive pnl. Negative pnl always has
/// an asset weight of 1 precision: SPOT_WEIGHT_PRECISION
pub unrealized_pnl_maintenance_asset_weight: u32,
/// number of users in a position (base)
pub number_of_users_with_base: u32,
/// number of users in a position (pnl) or pnl (quote)
pub number_of_users: u32,
pub market_index: u16,
/// Whether a market is active, reduce only, expired, etc
/// Affects whether users can open/close positions
pub status: MarketStatus,
/// Currently only Perpetual markets are supported
pub contract_type: ContractType,
/// The contract tier determines how much insurance a market can receive,
/// with more speculative markets receiving less insurance
/// It also influences the order perp markets can be liquidated, with less
/// speculative markets being liquidated first
pub contract_tier: ContractTier,
pub paused_operations: u8,
/// The spot market that pnl is settled in
pub quote_spot_market_index: u16,
/// Between -100 and 100, represents what % to increase/decrease the fee by
/// E.g. if this is -50 and the fee is 5bps, the new fee will be 2.5bps
/// if this is 50 and the fee is 5bps, the new fee will be 7.5bps
pub fee_adjustment: i16,
/// fuel multiplier for perp funding
/// precision: 10
pub fuel_boost_position: u8,
/// fuel multiplier for perp taker
/// precision: 10
pub fuel_boost_taker: u8,
/// fuel multiplier for perp maker
/// precision: 10
pub fuel_boost_maker: u8,
pub pool_id: u8,
pub high_leverage_margin_ratio_initial: u16,
pub high_leverage_margin_ratio_maintenance: u16,
pub protected_maker_limit_price_divisor: u8,
pub protected_maker_dynamic_divisor: u8,
pub lp_fee_transfer_scalar: u8,
pub lp_status: u8,
pub lp_paused_operations: u8,
pub lp_exchange_fee_excluscion_scalar: u8,
pub last_fill_price: u64,
pub lp_pool_id: u8,
pub market_config: u8,
pub padding: [u8; 22],
}
impl PerpMarket {
pub fn decode(data: &[u8]) -> Option<Self> {
if data.len() < 8 {
return None;
}
let discriminator = &data[0..8];
if discriminator != [10, 223, 12, 44, 107, 245, 55, 247] {
return None;
}
let mut data_slice = data;
data_slice = &data_slice[8..];
borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
}
}