nomy_data_models/models/
position_trade.rs1#![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::PositionTradeType;
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct PositionTrade {
21 pub event_at: DateTime<Utc>,
22 pub position_id: Uuid,
23 pub position_opened_at: DateTime<Utc>,
24 pub raw_trade_id: Uuid,
25 pub raw_trade_txn_id: String,
26 pub trade_type: String,
27 pub amount: Decimal,
28 pub price: Decimal,
29 pub fees_trading: Option<Decimal>,
30 pub fees_gas: Option<Decimal>,
31 pub fees_total: Option<Decimal>,
32 pub is_taker: Option<bool>,
33 pub extra_data: Option<JsonValue>,
34 pub unmatched_amount: Decimal,
35 pub is_fully_matched: bool,
36 pub id: Uuid,
37 pub created_at: DateTime<Utc>,
38 pub updated_at: DateTime<Utc>,
39 pub created_by: Option<String>,
40 pub updated_by: Option<String>,
41}
42
43impl PositionTrade {
44 pub fn new(
46 event_at: DateTime<Utc>,
47 position_id: Uuid,
48 position_opened_at: DateTime<Utc>,
49 raw_trade_id: Uuid,
50 raw_trade_txn_id: String,
51 trade_type: String,
52 amount: Decimal,
53 price: Decimal,
54 fees_trading: Decimal,
55 fees_gas: Decimal,
56 fees_total: Decimal,
57 is_taker: bool,
58 extra_data: JsonValue,
59 unmatched_amount: Decimal,
60 is_fully_matched: bool,
61 id: Uuid,
62 created_at: DateTime<Utc>,
63 updated_at: DateTime<Utc>,
64 created_by: String,
65 updated_by: String,
66 ) -> Self {
67 Self {
68 event_at,
69 position_id,
70 position_opened_at,
71 raw_trade_id,
72 raw_trade_txn_id,
73 trade_type,
74 amount,
75 price,
76 fees_trading: Some(fees_trading),
77 fees_gas: Some(fees_gas),
78 fees_total: Some(fees_total),
79 is_taker: Some(is_taker),
80 extra_data: Some(extra_data),
81 unmatched_amount,
82 is_fully_matched,
83 id,
84 created_at,
85 updated_at,
86 created_by: Some(created_by),
87 updated_by: Some(updated_by),
88 }
89 }
90
91 pub fn to_json(&self) -> Result<String, serde_json::Error> {
93 serde_json::to_string(self)
94 }
95
96 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
98 serde_json::from_str(json)
99 }
100
101 pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
103 let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
104 if let serde_json::Value::Object(map) = json {
105 map
106 } else {
107 serde_json::Map::new()
108 }
109 }
110}