#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde_json::Value as JsonValue;
use uuid::Uuid;
use crate::enums::PositionTradeType;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionTrade {
pub event_at: DateTime<Utc>,
pub position_id: Uuid,
pub position_opened_at: DateTime<Utc>,
pub raw_trade_id: Uuid,
pub raw_trade_txn_id: String,
pub trade_type: String,
pub amount: Decimal,
pub price: Decimal,
pub fees_trading: Option<Decimal>,
pub fees_gas: Option<Decimal>,
pub fees_total: Option<Decimal>,
pub is_taker: Option<bool>,
pub extra_data: Option<JsonValue>,
pub unmatched_amount: Decimal,
pub is_fully_matched: bool,
pub id: Uuid,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_by: Option<String>,
pub updated_by: Option<String>,
}
impl PositionTrade {
pub fn new(
event_at: DateTime<Utc>,
position_id: Uuid,
position_opened_at: DateTime<Utc>,
raw_trade_id: Uuid,
raw_trade_txn_id: String,
trade_type: String,
amount: Decimal,
price: Decimal,
fees_trading: Decimal,
fees_gas: Decimal,
fees_total: Decimal,
is_taker: bool,
extra_data: JsonValue,
unmatched_amount: Decimal,
is_fully_matched: bool,
id: Uuid,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
created_by: String,
updated_by: String,
) -> Self {
Self {
event_at,
position_id,
position_opened_at,
raw_trade_id,
raw_trade_txn_id,
trade_type,
amount,
price,
fees_trading: Some(fees_trading),
fees_gas: Some(fees_gas),
fees_total: Some(fees_total),
is_taker: Some(is_taker),
extra_data: Some(extra_data),
unmatched_amount,
is_fully_matched,
id,
created_at,
updated_at,
created_by: Some(created_by),
updated_by: Some(updated_by),
}
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
if let serde_json::Value::Object(map) = json {
map
} else {
serde_json::Map::new()
}
}
}