Skip to main content

cripsy_binary_api/broker/
session.rs

1//! Sesión activa de un bróker.
2
3use serde::{Deserialize, Serialize};
4
5use crate::domain::AccountType;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct Session {
9    pub broker_name: String,
10    /// Epoch segundos de conexión.
11    pub connected_at: i64,
12    pub account_id: Option<String>,
13    /// Balance activo: obligatorio para enviar órdenes (IQ Option lo exige).
14    pub balance_id: Option<i64>,
15    pub account_type: AccountType,
16    /// Offset (ms) entre el reloj local y el del bróker (timeSync).
17    pub server_time_offset_ms: i64,
18}
19
20impl Session {
21    /// Hora actual según el reloj del bróker (epoch ms).
22    pub fn broker_time_ms(&self, now_ms: i64) -> i64 {
23        now_ms + self.server_time_offset_ms
24    }
25
26    /// Hora actual según el reloj del bróker (epoch segundos).
27    pub fn broker_time_secs(&self, now_ms: i64) -> i64 {
28        self.broker_time_ms(now_ms) / 1000
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn offset_de_tiempo() {
38        let s = Session {
39            broker_name: "iq_option".into(),
40            connected_at: 0,
41            account_id: None,
42            balance_id: Some(7),
43            account_type: AccountType::Practice,
44            server_time_offset_ms: 250,
45        };
46        assert_eq!(s.broker_time_ms(1000), 1250);
47        assert_eq!(s.broker_time_secs(1000), 1);
48    }
49}