1#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
2use serde::{Deserialize, Serialize};
8
9use chrono::{DateTime, Utc};
11use rust_decimal::Decimal;
12use serde_json::Value as JsonValue;
13use uuid::Uuid;
14
15use crate::enums::MarketType;
17use crate::enums::PositionDirection;
18use crate::enums::PositionStatus;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct Position {
23 pub position_id: Uuid,
24 pub chain_id: i32,
25 pub exchange: String,
26 pub market_type: String,
27 pub position_direction: String,
28 pub wallet_address: String,
29 pub token_symbol_pair: String,
30 pub token_address_pair: String,
31 pub base_token_symbol: String,
32 pub base_token_address: String,
33 pub quote_token_symbol: String,
34 pub quote_token_address: String,
35 pub status: String,
36 pub notional_amount_usd: Decimal,
37 pub current_base_amount: Decimal,
38 pub original_base_amount: Decimal,
39 pub avg_entry_price: Decimal,
40 pub avg_exit_price: Decimal,
41 pub cost_basis: Decimal,
42 pub realized_pnl: Decimal,
43 pub realized_pnl_usd: Decimal,
44 pub opening_trades: JsonValue,
45 pub closing_trades: JsonValue,
46 pub realized_roi: Decimal,
47 pub leverage: Decimal,
48 pub opened_at: DateTime<Utc>,
49 pub closed_at: DateTime<Utc>,
50 pub fee_json: JsonValue,
51 pub id: Uuid,
52 pub created_at: DateTime<Utc>,
53 pub updated_at: DateTime<Utc>,
54 pub created_by: String,
55 pub updated_by: String,
56}
57
58impl Position {
59 pub fn new(
61 position_id: Uuid,
62 chain_id: i32,
63 exchange: String,
64 market_type: String,
65 position_direction: String,
66 wallet_address: String,
67 token_symbol_pair: String,
68 token_address_pair: String,
69 base_token_symbol: String,
70 base_token_address: String,
71 quote_token_symbol: String,
72 quote_token_address: String,
73 status: String,
74 notional_amount_usd: Decimal,
75 current_base_amount: Decimal,
76 original_base_amount: Decimal,
77 avg_entry_price: Decimal,
78 avg_exit_price: Decimal,
79 cost_basis: Decimal,
80 realized_pnl: Decimal,
81 realized_pnl_usd: Decimal,
82 opening_trades: JsonValue,
83 closing_trades: JsonValue,
84 realized_roi: Decimal,
85 leverage: Decimal,
86 opened_at: DateTime<Utc>,
87 closed_at: DateTime<Utc>,
88 fee_json: JsonValue,
89 id: Uuid,
90 created_at: DateTime<Utc>,
91 updated_at: DateTime<Utc>,
92 created_by: String,
93 updated_by: String,
94 ) -> Self {
95 Self {
96 position_id,
97 chain_id,
98 exchange,
99 market_type,
100 position_direction,
101 wallet_address,
102 token_symbol_pair,
103 token_address_pair,
104 base_token_symbol,
105 base_token_address,
106 quote_token_symbol,
107 quote_token_address,
108 status,
109 notional_amount_usd,
110 current_base_amount,
111 original_base_amount,
112 avg_entry_price,
113 avg_exit_price,
114 cost_basis,
115 realized_pnl,
116 realized_pnl_usd,
117 opening_trades,
118 closing_trades,
119 realized_roi,
120 leverage,
121 opened_at,
122 closed_at,
123 fee_json,
124 id,
125 created_at,
126 updated_at,
127 created_by,
128 updated_by,
129 }
130 }
131
132 pub fn to_json(&self) -> Result<String, serde_json::Error> {
134 serde_json::to_string(self)
135 }
136
137 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
139 serde_json::from_str(json)
140 }
141
142 pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
144 let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
145 if let serde_json::Value::Object(map) = json {
146 map
147 } else {
148 serde_json::Map::new()
149 }
150 }
151}