1use serde::{Deserialize, Serialize};
4use solana_program::pubkey::Pubkey;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Market {
8 pub market_id: Pubkey,
9 pub admin: Pubkey,
10 pub collateral_mint: Pubkey,
11 pub vault: Pubkey,
12 pub oracle: Pubkey,
13 pub max_leverage: u8,
14 pub total_volume: u64,
15 pub trade_count: u64,
16 pub open_interest: u64,
17 pub insurance_fund: u64,
18 pub created_at: i64,
19 pub is_resolved: bool,
20}
21
22#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
23pub enum PositionSide {
24 Long,
25 Short,
26}
27
28#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
29pub enum PositionStatus {
30 Open,
31 Closed,
32 Liquidated,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct Position {
37 pub position_id: Pubkey,
38 pub trader: Pubkey,
39 pub market_id: Pubkey,
40 pub side: PositionSide,
41 pub size: u64,
42 pub entry_price: u64,
43 pub current_price: u64,
44 pub collateral: u64,
45 pub leverage: u8,
46 pub liquidation_price: u64,
47 pub status: PositionStatus,
48 pub pnl: i64,
49 pub pnl_percent: i32,
50 pub opened_at: i64,
51 pub closed_at: i64,
52 pub last_updated: i64,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct Account {
57 pub owner: Pubkey,
58 pub market_id: Pubkey,
59 pub balance: u64,
60 pub total_deposited: u64,
61 pub total_withdrawn: u64,
62 pub position_count: u64,
63 pub created_at: i64,
64 pub is_active: bool,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct LiquidityPool {
69 pub pool_id: Pubkey,
70 pub market_id: Pubkey,
71 pub available_liquidity: u64,
72 pub utilization_pct: u8,
73 pub fee_accrued: u64,
74}
75
76impl Market {
77 pub fn new(market_id: Pubkey, admin: Pubkey, collateral_mint: Pubkey, vault: Pubkey, oracle: Pubkey) -> Self {
78 Self {
79 market_id,
80 admin,
81 collateral_mint,
82 vault,
83 oracle,
84 max_leverage: 50,
85 total_volume: 0,
86 trade_count: 0,
87 open_interest: 0,
88 insurance_fund: 0,
89 created_at: 0,
90 is_resolved: false,
91 }
92 }
93}
94
95impl Position {
96 pub fn new(
97 position_id: Pubkey,
98 trader: Pubkey,
99 market_id: Pubkey,
100 side: PositionSide,
101 size: u64,
102 entry_price: u64,
103 collateral: u64,
104 leverage: u8,
105 ) -> Self {
106 let liquidation_price = match side {
107 PositionSide::Long => {
108 ((entry_price as u128 * (leverage - 1) as u128) / leverage as u128) as u64
109 }
110 PositionSide::Short => {
111 ((entry_price as u128 * (leverage + 1) as u128) / leverage as u128) as u64
112 }
113 };
114
115 Self {
116 position_id,
117 trader,
118 market_id,
119 side,
120 size,
121 entry_price,
122 current_price: entry_price,
123 collateral,
124 leverage,
125 liquidation_price,
126 status: PositionStatus::Open,
127 pnl: 0,
128 pnl_percent: 0,
129 opened_at: 0,
130 closed_at: 0,
131 last_updated: 0,
132 }
133 }
134}