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::TradeDirection;
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct EnrichedTrade {
22 pub pnl_usd: Option<Decimal>,
23 pub pnl: Option<Decimal>,
24 pub roi: Option<Decimal>,
25 pub holding_duration: Option<chrono::Duration>,
26 pub fee_json: Option<JsonValue>,
27 pub id: Uuid,
28 pub event_at: DateTime<Utc>,
29 pub txn_id: String,
30 pub wallet_address: String,
31 pub chain_id: i32,
32 pub exchange: String,
33 pub is_buy: bool,
34 pub is_taker: Option<bool>,
35 pub direction: Option<String>,
36 pub token_price: Decimal,
37 pub token_symbol_pair: String,
38 pub token_address_pair: Option<String>,
39 pub base_token_symbol: String,
40 pub quote_token_symbol: String,
41 pub base_token_address: Option<String>,
42 pub quote_token_address: Option<String>,
43 pub base_amount: Decimal,
44 pub quote_amount: Decimal,
45 pub usd_amount: Option<Decimal>,
46 pub market_type: String,
47 pub extra_data: Option<JsonValue>,
48 pub created_at: DateTime<Utc>,
49 pub updated_at: DateTime<Utc>,
50 pub created_by: Option<String>,
51 pub updated_by: Option<String>,
52}
53
54impl EnrichedTrade {
55 pub fn new(
57 pnl_usd: Decimal,
58 pnl: Decimal,
59 roi: Decimal,
60 holding_duration: chrono::Duration,
61 fee_json: JsonValue,
62 id: Uuid,
63 event_at: DateTime<Utc>,
64 txn_id: String,
65 wallet_address: String,
66 chain_id: i32,
67 exchange: String,
68 is_buy: bool,
69 is_taker: bool,
70 direction: String,
71 token_price: Decimal,
72 token_symbol_pair: String,
73 token_address_pair: String,
74 base_token_symbol: String,
75 quote_token_symbol: String,
76 base_token_address: String,
77 quote_token_address: String,
78 base_amount: Decimal,
79 quote_amount: Decimal,
80 usd_amount: Decimal,
81 market_type: String,
82 extra_data: JsonValue,
83 created_at: DateTime<Utc>,
84 updated_at: DateTime<Utc>,
85 created_by: String,
86 updated_by: String,
87 ) -> Self {
88 Self {
89 pnl_usd: Some(pnl_usd),
90 pnl: Some(pnl),
91 roi: Some(roi),
92 holding_duration: Some(holding_duration),
93 fee_json: Some(fee_json),
94 id,
95 event_at,
96 txn_id,
97 wallet_address,
98 chain_id,
99 exchange,
100 is_buy,
101 is_taker: Some(is_taker),
102 direction: Some(direction),
103 token_price,
104 token_symbol_pair,
105 token_address_pair: Some(token_address_pair),
106 base_token_symbol,
107 quote_token_symbol,
108 base_token_address: Some(base_token_address),
109 quote_token_address: Some(quote_token_address),
110 base_amount,
111 quote_amount,
112 usd_amount: Some(usd_amount),
113 market_type,
114 extra_data: Some(extra_data),
115 created_at,
116 updated_at,
117 created_by: Some(created_by),
118 updated_by: Some(updated_by),
119 }
120 }
121
122 pub fn to_json(&self) -> Result<String, serde_json::Error> {
124 serde_json::to_string(self)
125 }
126
127 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
129 serde_json::from_str(json)
130 }
131
132 pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
134 let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
135 if let serde_json::Value::Object(map) = json {
136 map
137 } else {
138 serde_json::Map::new()
139 }
140 }
141}