nomy_data_models/models/
position_trade.rs

1#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
2//! PositionTrade model definition.
3//!
4//! This file is generated automatically from the Python SQLAlchemy model.
5//! Do not edit this file manually.
6
7use serde::{Deserialize, Serialize};
8
9// Standard imports that may be needed
10use chrono::{DateTime, Utc};
11use rust_decimal::Decimal;
12use serde_json::Value as JsonValue;
13use uuid::Uuid;
14
15// Additional imports specific to this model
16use crate::enums::PositionTradeType;
17
18/// Model linking a trade event to a position's lifecycle.
19#[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    /// Create a new PositionTrade.
45    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    /// Convert to a JSON string.
92    pub fn to_json(&self) -> Result<String, serde_json::Error> {
93        serde_json::to_string(self)
94    }
95
96    /// Convert from a JSON string.
97    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
98        serde_json::from_str(json)
99    }
100
101    /// Convert to a dictionary-like structure.
102    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}