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