ig_client/application/models/
transaction.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 12/5/25
5******************************************************************************/
6use chrono::{DateTime, NaiveDate, Utc};
7use serde::{Deserialize, Serialize};
8use std::fmt;
9
10/// Raw JSON coming from IG’s transactions endpoint
11#[derive(Debug, Serialize, Deserialize, Clone)]
12pub struct RawTransaction {
13    #[serde(rename = "date")]
14    pub(crate) date: String,
15
16    #[serde(rename = "dateUtc")]
17    pub(crate) date_utc: String,
18
19    #[serde(rename = "openDateUtc")]
20    pub(crate) open_date_utc: String,
21
22    #[serde(rename = "instrumentName")]
23    pub(crate) instrument_name: String,
24
25    #[serde(rename = "period")]
26    pub(crate) period: String,
27
28    #[serde(rename = "profitAndLoss")]
29    pub(crate) pnl_raw: String,
30
31    #[serde(rename = "transactionType")]
32    pub(crate) transaction_type: String,
33
34    pub(crate) reference: String,
35
36    #[serde(rename = "openLevel")]
37    pub(crate) open_level: String,
38
39    #[serde(rename = "closeLevel")]
40    pub(crate) close_level: String,
41
42    #[serde(rename = "size")]
43    pub(crate) size: String,
44
45    #[serde(rename = "currency")]
46    pub(crate) currency: String,
47
48    #[serde(rename = "cashTransaction")]
49    pub(crate) cash_transaction: bool,
50}
51
52impl fmt::Display for RawTransaction {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        let s = serde_json::to_string(self).map_err(|_| fmt::Error)?;
55        write!(f, "{}", s)
56    }
57}
58
59/// Represents a processed transaction from IG Markets with parsed fields
60#[derive(Debug)]
61pub struct Transaction {
62    /// Date and time when the transaction was executed
63    pub(crate) deal_date: DateTime<Utc>,
64    /// Underlying asset or instrument (e.g., "GOLD", "US500")
65    pub(crate) underlying: Option<String>,
66    /// Strike price for options
67    pub(crate) strike: Option<f64>,
68    /// Type of option ("CALL" or "PUT")
69    pub(crate) option_type: Option<String>,
70    /// Expiration date for options
71    pub(crate) expiry: Option<NaiveDate>,
72    /// Type of transaction (e.g., "DEAL", "WITH")
73    pub(crate) transaction_type: String,
74    /// Profit and loss in EUR
75    pub(crate) pnl_eur: f64,
76    /// Unique reference for the transaction
77    pub(crate) reference: String,
78    /// Whether this transaction is a fee
79    pub(crate) is_fee: bool,
80    /// Original JSON string of the transaction
81    pub(crate) raw_json: String,
82}