use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use serde_json::{json, Value};
use tokio::sync::{broadcast, mpsc, Notify, RwLock};
use crate::broker::config::{BrokerParams, IqOptionConfig};
use crate::broker::error::BrokerError;
use crate::broker::iq_option::endpoints::{self, msg};
use crate::broker::iq_option::mapper;
use crate::broker::session::Session;
use crate::domain::*;
use crate::transport::http::HttpClient;
use crate::transport::router::RequestRouter;
use crate::transport::websocket::WsClient;
pub(crate) struct Saliente {
name: String,
msg: Value,
request_id: String,
}
#[derive(Debug, Clone)]
pub(crate) struct Suscripcion {
pub(crate) clave: String,
pub(crate) name: String,
pub(crate) msg: Value,
}
const MOTIVO_CIERRE: &str = "cierre solicitado";
fn registrar_suscripcion(
lista: &mut Vec<Suscripcion>,
clave: &str,
name: &str,
msg: &Value,
) -> bool {
if lista.iter().any(|s| s.clave == clave) {
return false;
}
lista.push(Suscripcion {
clave: clave.to_string(),
name: name.to_string(),
msg: msg.clone(),
});
true
}
fn siguiente_espera(actual: u64, maximo: u64) -> u64 {
let techo = maximo.max(1);
actual.saturating_mul(2).clamp(1, techo)
}
fn es_fatal(e: &BrokerError) -> bool {
matches!(e, BrokerError::Authentication(_))
}
#[derive(Default)]
pub(crate) struct Estado {
pub(crate) session: Option<Session>,
pub(crate) balances: Vec<AccountBalance>,
pub(crate) conectado: bool,
}
pub(crate) struct SubVelas {
pub(crate) symbol: String,
pub(crate) interval: u64,
pub(crate) tx: mpsc::UnboundedSender<Candle>,
}
pub struct IqOptionBroker {
config: IqOptionConfig,
salida: Option<mpsc::Sender<Saliente>>,
pub(crate) suscripciones: Arc<RwLock<Vec<Suscripcion>>>,
pub(crate) vivo: Arc<AtomicBool>,
pub(crate) cortar: Arc<Notify>,
pub(crate) abiertas: Arc<RwLock<HashSet<OrderId>>>,
pub(crate) router: Arc<RequestRouter>,
pub(crate) estado: Arc<RwLock<Estado>>,
pub(crate) catalogo: Arc<RwLock<Vec<Asset>>>,
pub(crate) sub_velas: Arc<RwLock<Vec<SubVelas>>>,
pub(crate) eventos: broadcast::Sender<Event>,
pub(crate) precios: broadcast::Sender<Value>,
pub(crate) resultados: Arc<RwLock<HashMap<String, OperationResult>>>,
pub(crate) digitales: Arc<RwLock<HashMap<String, OrderId>>>,
}
impl IqOptionBroker {
pub async fn nuevo(params: BrokerParams) -> Result<Self, BrokerError> {
let (config, cfg_trafico) = match params {
BrokerParams::IqOption(c) => (c.clone(), c.transport.clone()),
};
let http = HttpClient::new(
&endpoints::api_base_url(&config.auth_host),
cfg_trafico.clone(),
)?;
let (cuerpo, headers) = http
.post_json_completo(
endpoints::LOGIN_PATH.trim_start_matches('/'),
&json!({ "identifier": config.email, "password": config.password }),
)
.await?;
let ssid = extraer_ssid(&headers, &cuerpo).ok_or_else(|| {
BrokerError::Authentication("el login no devolvió cookie ssid".into())
})?;
let ws = WsClient::connect_con_reintentos(
&endpoints::ws_url(&config.ws_host),
vec![("Cookie".to_string(), format!("ssid={ssid}"))],
cfg_trafico.clone(),
)
.await?;
let (tx_salida, rx_salida) = mpsc::channel::<Saliente>(config.transport.cola_salida.max(1));
let (tx_eventos, _rx_eventos) = broadcast::channel::<Event>(1024);
let (tx_precios, _rx_precios) = broadcast::channel::<Value>(512);
let router = Arc::new(RequestRouter::new());
let estado = Arc::new(RwLock::new(Estado::default()));
let catalogo = Arc::new(RwLock::new(Vec::new()));
let sub_velas: Arc<RwLock<Vec<SubVelas>>> = Arc::new(RwLock::new(Vec::new()));
let resultados: Arc<RwLock<HashMap<String, OperationResult>>> =
Arc::new(RwLock::new(HashMap::new()));
let digitales: Arc<RwLock<HashMap<String, OrderId>>> =
Arc::new(RwLock::new(HashMap::new()));
let suscripciones: Arc<RwLock<Vec<Suscripcion>>> = Arc::new(RwLock::new(Vec::new()));
let vivo = Arc::new(AtomicBool::new(true));
let cortar = Arc::new(Notify::new());
let abiertas: Arc<RwLock<HashSet<OrderId>>> = Arc::new(RwLock::new(HashSet::new()));
let broker = Self {
config,
salida: Some(tx_salida),
suscripciones: suscripciones.clone(),
vivo: vivo.clone(),
cortar: cortar.clone(),
abiertas: abiertas.clone(),
router: router.clone(),
estado: estado.clone(),
catalogo: catalogo.clone(),
sub_velas: sub_velas.clone(),
eventos: tx_eventos.clone(),
precios: tx_precios.clone(),
resultados: resultados.clone(),
digitales: digitales.clone(),
};
tokio::spawn(tarea_socket(
ws,
rx_salida,
Ctx {
config: broker.config.clone(),
suscripciones: suscripciones.clone(),
vivo: vivo.clone(),
cortar: cortar.clone(),
abiertas: abiertas.clone(),
router: router.clone(),
estado: estado.clone(),
catalogo: catalogo.clone(),
sub_velas: sub_velas.clone(),
eventos: tx_eventos,
precios: tx_precios,
resultados: resultados.clone(),
digitales: digitales.clone(),
},
));
let perfil = broker
.solicitar(msg::SSID, &json!(ssid), msg::PROFILE)
.await?;
let (session, balances) = mapper::sesion_desde_perfil(&perfil, &broker.config.account)
.ok_or_else(|| BrokerError::Rejected("profile sin balances utilizables".into()))?;
{
let mut e = estado.write().await;
e.session = Some(session);
e.balances = balances;
e.conectado = true;
}
let init = broker
.solicitar(
msg::SEND_MESSAGE,
&json!({ "name": msg::GET_INITIALIZATION_DATA, "version": "3.0", "body": {} }),
msg::INITIALIZATION_DATA,
)
.await?;
let activos = mapper::catalogo_desde_init(&init);
{
let mut c = catalogo.write().await;
*c = activos;
}
for instrumento in ["digital-option", "binary-option", "trading"] {
broker
.suscribir(
&format!("posicion:{instrumento}"),
msg::SUBSCRIBE_MESSAGE,
&json!({
"name": msg::POSITION_CHANGED,
"version": "2.0",
"params": { "routingFilters": { "instrument_type": instrumento } }
}),
)
.await?;
}
Ok(broker)
}
pub(crate) async fn solicitar(
&self,
name: &str,
msg: &Value,
_respuesta: &str,
) -> Result<Value, BrokerError> {
if !self.vivo.load(Ordering::Acquire) {
return Err(BrokerError::NotConnected);
}
let (rid, rx) = self.router.register("iq");
self.salida
.as_ref()
.ok_or_else(|| BrokerError::Internal("canal de salida cerrado".into()))?
.send(Saliente {
name: name.into(),
msg: msg.clone(),
request_id: rid.clone(),
})
.await
.map_err(|_| BrokerError::Internal("canal de salida cerrado".into()))?;
let plazo = std::time::Duration::from_secs(self.config.timeout_secs);
match tokio::time::timeout(plazo, rx).await {
Ok(Ok(msg)) => Ok(msg),
Ok(Err(_)) => Err(BrokerError::Internal("respuesta descartada".into())),
Err(_) => {
self.router.discard(&rid);
Err(BrokerError::Timeout)
}
}
}
pub(crate) async fn enviar_sin_espera(
&self,
name: &str,
msg: &Value,
) -> Result<(), BrokerError> {
if !self.vivo.load(Ordering::Acquire) {
return Err(BrokerError::NotConnected);
}
let rid = self.router.next_id("iq");
self.salida
.as_ref()
.ok_or_else(|| BrokerError::Internal("canal de salida cerrado".into()))?
.send(Saliente {
name: name.into(),
msg: msg.clone(),
request_id: rid,
})
.await
.map_err(|_| BrokerError::Internal("canal de salida cerrado".into()))?;
Ok(())
}
pub(crate) async fn suscribir(
&self,
clave: &str,
name: &str,
msg: &Value,
) -> Result<(), BrokerError> {
let nueva = {
let reg = self.suscripciones.read().await;
!reg.iter().any(|s| s.clave == clave)
};
if !nueva {
return Ok(());
}
self.enviar_sin_espera(name, msg).await?;
let mut reg = self.suscripciones.write().await;
registrar_suscripcion(&mut reg, clave, name, msg);
Ok(())
}
pub(crate) async fn estado_lectura(&self) -> tokio::sync::RwLockReadGuard<'_, Estado> {
self.estado.read().await
}
pub(crate) async fn hora_servidor_segs(&self) -> i64 {
let offset = self
.estado_lectura()
.await
.session
.as_ref()
.map(|s| s.server_time_offset_ms)
.unwrap_or(0);
let ahora_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or_default();
(ahora_ms + offset) / 1000
}
pub(crate) fn cerrar_salida(&mut self) {
self.salida = None;
}
#[doc(hidden)]
pub async fn simular_caida_de_conexion(&self) {
self.cortar.notify_one();
}
}
fn extraer_ssid(headers: &reqwest::header::HeaderMap, cuerpo: &Value) -> Option<String> {
for header in headers.get_all("set-cookie") {
if let Ok(raw) = header.to_str() {
if let Some(rest) = raw.strip_prefix("ssid=") {
return Some(rest.split(';').next().unwrap_or(rest).trim().to_string());
}
}
}
cuerpo
.get("ssid")
.and_then(|s| s.as_str())
.map(str::to_string)
}
pub(crate) struct Ctx {
pub(crate) config: IqOptionConfig,
pub(crate) suscripciones: Arc<RwLock<Vec<Suscripcion>>>,
pub(crate) vivo: Arc<AtomicBool>,
pub(crate) cortar: Arc<Notify>,
pub(crate) abiertas: Arc<RwLock<HashSet<OrderId>>>,
pub(crate) router: Arc<RequestRouter>,
pub(crate) estado: Arc<RwLock<Estado>>,
pub(crate) catalogo: Arc<RwLock<Vec<Asset>>>,
pub(crate) sub_velas: Arc<RwLock<Vec<SubVelas>>>,
pub(crate) eventos: broadcast::Sender<Event>,
pub(crate) precios: broadcast::Sender<Value>,
pub(crate) resultados: Arc<RwLock<HashMap<String, OperationResult>>>,
pub(crate) digitales: Arc<RwLock<HashMap<String, OrderId>>>,
}
async fn tarea_socket(mut ws: WsClient, mut rx_salida: mpsc::Receiver<Saliente>, ctx: Ctx) {
loop {
let motivo = bucle_socket(&mut ws, &mut rx_salida, &ctx).await;
ctx.router.fail_all();
ctx.vivo.store(false, Ordering::Release);
ctx.estado.write().await.conectado = false;
let _ = ctx.eventos.send(Event::Disconnected {
motivo: motivo.to_string(),
});
if motivo == MOTIVO_CIERRE {
break;
}
tracing::warn!(motivo, "conexión perdida; reintentando");
let inicio = tokio::time::Instant::now();
let mut intentos = 0u32;
match reconectar(&ctx, &mut intentos).await {
Ok(nuevo) => {
ws = nuevo;
ctx.vivo.store(true, Ordering::Release);
ctx.estado.write().await.conectado = true;
let _ = ctx.eventos.send(Event::Reconnected {
intentos,
downtime_ms: inicio.elapsed().as_millis() as u64,
});
tracing::info!(intentos, "sesión restablecida");
}
Err(e) => {
tracing::error!(error = %e, "reconexión abandonada");
break;
}
}
}
ctx.sub_velas.write().await.clear();
ctx.suscripciones.write().await.clear();
tracing::warn!("sesión cerrada; no habrá más reconexiones");
}
async fn bucle_socket(
ws: &mut WsClient,
rx_salida: &mut mpsc::Receiver<Saliente>,
ctx: &Ctx,
) -> &'static str {
loop {
tokio::select! {
frame = ws.siguiente() => {
match frame {
Some(Ok(f)) => {
if let Some(rid) = f.request_id.as_deref() {
if !ctx.router.deliver(Some(rid), f.msg.clone()) {
tracing::debug!(rid, "request_id tardío o desconocido");
}
}
enrutar_evento(&f, ctx).await;
}
Some(Err(e)) => {
tracing::warn!(error = %e, "error de lectura del websocket");
return "error de lectura";
}
None => return "el bróker cerró el socket",
}
}
saliente = rx_salida.recv() => {
match saliente {
Some(s) => {
if let Err(e) = ws.enviar(&s.name, &s.msg, &s.request_id).await {
tracing::warn!(error = %e, "error de escritura del websocket");
return "error de escritura";
}
}
None => {
let _ = ws.cerrar().await;
return MOTIVO_CIERRE;
}
}
}
_ = ctx.cortar.notified() => {
tracing::warn!("caída simulada: cerrando el socket");
let _ = ws.cerrar().await;
return "caída simulada";
}
}
}
}
async fn iniciar_sesion(ctx: &Ctx) -> Result<String, BrokerError> {
let http = HttpClient::new(
&endpoints::api_base_url(&ctx.config.auth_host),
ctx.config.transport.clone(),
)?;
let (cuerpo, headers) = http
.post_json_completo(
endpoints::LOGIN_PATH.trim_start_matches('/'),
&json!({ "identifier": ctx.config.email, "password": ctx.config.password }),
)
.await?;
extraer_ssid(&headers, &cuerpo)
.ok_or_else(|| BrokerError::Authentication("el login no devolvió cookie ssid".into()))
}
async fn reconectar(ctx: &Ctx, intentos: &mut u32) -> Result<WsClient, BrokerError> {
let cfg = &ctx.config.transport;
let mut espera = siguiente_espera(0, cfg.backoff_base_ms.max(1));
let mut ssid: Option<String> = None;
loop {
tokio::time::sleep(std::time::Duration::from_millis(espera)).await;
*intentos += 1;
let credencial = match ssid.clone() {
Some(s) => Ok(s),
None => iniciar_sesion(ctx).await,
};
match credencial {
Ok(sesion) => match abrir_y_restaurar(ctx, &sesion).await {
Ok(ws) => return Ok(ws),
Err(e) if es_fatal(&e) => return Err(e),
Err(e) => {
tracing::warn!(error = %e, "no se pudo restaurar la sesión");
ssid = None;
}
},
Err(e) if es_fatal(&e) => return Err(e),
Err(e) => tracing::warn!(error = %e, "login fallido durante la reconexión"),
}
espera = siguiente_espera(espera, cfg.backoff_max_ms);
}
}
async fn abrir_y_restaurar(ctx: &Ctx, ssid: &str) -> Result<WsClient, BrokerError> {
let mut ws = WsClient::connect_con_reintentos(
&endpoints::ws_url(&ctx.config.ws_host),
vec![("Cookie".to_string(), format!("ssid={ssid}"))],
ctx.config.transport.clone(),
)
.await?;
let plazo = std::time::Duration::from_secs(ctx.config.timeout_secs);
let perfil = solicitar_directo(&mut ws, ctx, msg::SSID, &json!(ssid), plazo).await?;
let (session, balances) = mapper::sesion_desde_perfil(&perfil, &ctx.config.account)
.ok_or_else(|| BrokerError::Rejected("profile sin balances utilizables".into()))?;
{
let mut e = ctx.estado.write().await;
e.session = Some(session);
e.balances = balances;
}
let init = solicitar_directo(
&mut ws,
ctx,
msg::SEND_MESSAGE,
&json!({ "name": msg::GET_INITIALIZATION_DATA, "version": "3.0", "body": {} }),
plazo,
)
.await?;
{
let mut c = ctx.catalogo.write().await;
*c = mapper::catalogo_desde_init(&init);
}
let pendientes = ctx.suscripciones.read().await.clone();
for sub in pendientes {
let rid = ctx.router.next_id("iq");
ws.enviar(&sub.name, &sub.msg, &rid).await?;
}
reconciliar_pendientes(&mut ws, ctx, plazo).await;
Ok(ws)
}
const PÁGINAS_RECONCILIACION: usize = 10;
const POSICIONES_POR_PAGINA: usize = 100;
async fn reconciliar_pendientes(ws: &mut WsClient, ctx: &Ctx, plazo: std::time::Duration) {
let mut faltan = ctx.abiertas.read().await.clone();
if faltan.is_empty() {
return;
}
for pagina in 0..PÁGINAS_RECONCILIACION {
let offset = pagina * POSICIONES_POR_PAGINA;
let historial = match pedir_historial(ws, ctx, plazo, offset).await {
Ok(h) => h,
Err(e) => {
tracing::warn!(error = %e, offset, "sin historial: reconciliación incompleta");
return;
}
};
if historial.positions.is_empty() {
return;
}
let ultima_pagina = historial.positions.len() < POSICIONES_POR_PAGINA;
for (id, result) in reconciliar(&faltan, &historial) {
let nuevo = {
let mut cache = ctx.resultados.write().await;
if cache.contains_key(&id) {
false
} else {
cache.insert(id.clone(), result);
true
}
};
ctx.abiertas.write().await.remove(&id);
faltan.remove(&id);
if nuevo {
let _ = ctx.eventos.send(Event::OrderClosed { id, result });
}
}
if faltan.is_empty() {
return;
}
if ultima_pagina {
tracing::warn!(
pendientes = faltan.len(),
"cierres no encontrados en el historial"
);
return;
}
}
tracing::warn!(
pendientes = faltan.len(),
"reconciliación agotada sin coincidencias"
);
}
async fn pedir_historial(
ws: &mut WsClient,
ctx: &Ctx,
plazo: std::time::Duration,
offset: usize,
) -> Result<PositionHistory, BrokerError> {
let balance_id = ctx
.estado
.read()
.await
.session
.as_ref()
.and_then(|s| s.balance_id);
let respuesta = solicitar_directo(
ws,
ctx,
msg::SEND_MESSAGE,
&json!({
"name": msg::PORTFOLIO_GET_HISTORY,
"body": {
"instrument_types": ["turbo-option", "binary-option", "digital-option"],
"limit": POSICIONES_POR_PAGINA,
"offset": offset,
"start": 0,
"end": 0,
"user_balance_id": balance_id
}
}),
plazo,
)
.await?;
mapper::historial_desde_positions(&respuesta, offset)
.ok_or_else(|| BrokerError::Rejected("historial sin positions".into()))
}
fn resultado_desde_posicion(p: &Position) -> Option<OperationResult> {
let pnl = p.pnl?;
Some(if pnl > 0.0 {
OperationResult::Win(pnl)
} else if pnl < 0.0 {
OperationResult::Loss(pnl.abs())
} else {
OperationResult::Equal(p.amount)
})
}
fn reconciliar(
pendientes: &HashSet<OrderId>,
historial: &PositionHistory,
) -> Vec<(OrderId, OperationResult)> {
let mut vistos = HashSet::new();
historial
.positions
.iter()
.filter(|p| pendientes.contains(&p.id) && vistos.insert(p.id.clone()))
.filter_map(|p| resultado_desde_posicion(p).map(|r| (p.id.clone(), r)))
.collect()
}
async fn solicitar_directo(
ws: &mut WsClient,
ctx: &Ctx,
name: &str,
msg: &Value,
plazo: std::time::Duration,
) -> Result<Value, BrokerError> {
let (rid, mut rx) = ctx.router.register("iq");
ws.enviar(name, msg, &rid).await?;
let limite = tokio::time::Instant::now() + plazo;
loop {
tokio::select! {
respuesta = &mut rx => {
return respuesta.map_err(|_| BrokerError::Internal("respuesta descartada".into()));
}
frame = ws.siguiente() => {
match frame {
Some(Ok(f)) => {
if f.request_id.as_deref() == Some(rid.as_str()) {
return Ok(f.msg);
}
if let Some(otro) = f.request_id.as_deref() {
let _ = ctx.router.deliver(Some(otro), f.msg.clone());
}
enrutar_evento(&f, ctx).await;
}
Some(Err(e)) => return Err(BrokerError::Transport(e)),
None => {
return Err(BrokerError::Connection(
"el socket se cerró durante la reautenticación".into(),
))
}
}
}
_ = tokio::time::sleep_until(limite) => {
ctx.router.discard(&rid);
return Err(BrokerError::Timeout);
}
}
}
}
async fn enrutar_evento(frame: &crate::transport::websocket::WsFrame, ctx: &Ctx) {
let Ctx {
config: _,
suscripciones: _,
vivo: _,
cortar: _,
abiertas,
router: _,
estado,
catalogo,
sub_velas,
eventos,
precios,
resultados,
digitales,
} = ctx;
match frame.name.as_str() {
msg::TIME_SYNC => {
if let Some(ms) = frame.msg.as_i64() {
let ahora = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(ms);
let mut e = estado.write().await;
if let Some(s) = e.session.as_mut() {
s.server_time_offset_ms = ms - ahora;
}
}
}
msg::PROFILE | msg::BALANCES => {
let balances = if frame.msg.is_array() {
mapper::balances_desde_lista(&frame.msg)
} else {
mapper::balances_desde_perfil(&frame.msg)
};
let mut e = estado.write().await;
if !balances.is_empty() {
e.balances = balances;
}
}
msg::BALANCE_CHANGED => {
if let Some(bal) = mapper::balance_desde_cambio(&frame.msg) {
let mut e = estado.write().await;
e.balances.retain(|b| b.id != bal.id);
e.balances.push(bal.clone());
let _ = eventos.send(Event::BalanceChanged(bal));
}
}
msg::CANDLE_GENERATED => {
let active_id = frame
.msg
.get("active_id")
.and_then(|a| a.as_u64())
.unwrap_or(0) as u32;
let catalogo = catalogo.read().await;
if let Some(symbol) = catalogo
.iter()
.find(|a| a.opcode == active_id)
.map(|a| a.symbol.clone())
{
if let Some(tick) = mapper::tick_desde_frame(&frame.msg) {
let _ = eventos.send(Event::PriceTick {
symbol: symbol.clone(),
tick,
});
}
if let Some((_, candle)) = mapper::vela_viva_unica(&frame.msg) {
let mut subs = sub_velas.write().await;
subs.retain(|s| !s.tx.is_closed());
for sub in subs.iter() {
if sub.symbol == symbol
&& (sub.interval == 0 || sub.interval as i64 == candle.to - candle.from)
{
let _ = sub.tx.send(candle);
}
}
}
}
}
msg::CANDLES_GENERATED => {
let active_id = frame
.msg
.get("active_id")
.and_then(|a| a.as_u64())
.unwrap_or(0) as u32;
if active_id > 0 {
let catalogo = catalogo.read().await;
if let Some(symbol) = catalogo
.iter()
.find(|a| a.opcode == active_id)
.map(|a| a.symbol.clone())
{
if let Some(tick) = mapper::tick_desde_frame(&frame.msg) {
let _ = eventos.send(Event::PriceTick {
symbol: symbol.clone(),
tick,
});
}
let mut subs = sub_velas.write().await;
subs.retain(|s| !s.tx.is_closed());
for (size, candle) in mapper::velas_vivas_multi(&frame.msg) {
for sub in subs.iter() {
if sub.symbol == symbol && (sub.interval == 0 || sub.interval == size) {
let _ = sub.tx.send(candle);
}
}
}
}
}
}
msg::CLIENT_PRICE_GENERATED => {
let _ = precios.send(frame.msg.clone());
if let Some((active_id, payout)) = mapper::payout_price_splitter_con_id(&frame.msg) {
let catalogo = catalogo.read().await;
if let Some(asset) = catalogo.iter().find(|a| a.opcode == active_id) {
let payout = Payout {
symbol: asset.symbol.clone(),
..payout
};
let _ = eventos.send(Event::PayoutChanged(payout));
}
}
}
msg::OPTION_OPENED => {
if let Some(operacion) = mapper::operacion_desde_apertura(&frame.msg) {
let _ = eventos.send(Event::OrderOpened(operacion));
}
}
msg::OPTION_CLOSED | msg::SOCKET_OPTION_CLOSED => {
let (id, result) = if frame.name == msg::OPTION_CLOSED {
(
frame.msg.get("option_id").map(|i| i.to_string()),
mapper::resultado_desde_cierre(&frame.msg),
)
} else {
(
frame.msg.get("id").map(|i| i.to_string()),
mapper::resultado_desde_socket_cerrado(&frame.msg),
)
};
if let (Some(id), Some(result)) = (id, result) {
let mut cache = resultados.write().await;
if !cache.contains_key(&id) {
cache.insert(id.clone(), result);
abiertas.write().await.remove(&id);
drop(cache);
let _ = eventos.send(Event::OrderClosed { id, result });
}
}
}
msg::POSITION_CHANGED => {
if let Some((instrument_id, result)) = mapper::cierre_digital_desde_position(&frame.msg)
{
let order_id = digitales.write().await.remove(&instrument_id);
if let Some(id) = order_id {
let mut cache = resultados.write().await;
if !cache.contains_key(&id) {
cache.insert(id.clone(), result);
abiertas.write().await.remove(&id);
drop(cache);
let _ = eventos.send(Event::OrderClosed { id, result });
}
}
}
}
msg::LIVE_DEAL | msg::LIVE_DEAL_BINARY => {
let active_id = frame
.msg
.get("active_id")
.or_else(|| frame.msg.get("instrument_active_id"))
.and_then(|a| a.as_i64())
.unwrap_or(0) as u32;
let catalogo = catalogo.read().await;
if let Some(symbol) = catalogo
.iter()
.find(|a| a.opcode == active_id)
.map(|a| a.symbol.clone())
{
if let Some(deal) = mapper::live_deal_desde_frame(&symbol, &frame.msg) {
let _ = eventos.send(Event::LiveDeal(deal));
}
}
}
msg::COMMISSION_CHANGED => {
let active_id = frame
.msg
.get("active_id")
.and_then(|a| a.as_i64())
.unwrap_or(0) as u32;
let catalogo = catalogo.read().await;
if let Some(symbol) = catalogo
.iter()
.find(|a| a.opcode == active_id)
.map(|a| a.symbol.clone())
{
if let Some(cambio) = mapper::comision_desde_frame(&symbol, &frame.msg) {
let _ = eventos.send(Event::CommissionChanged(cambio));
}
}
}
_ => {
if let Some(evento) = mapper::evento_desde_frame(frame) {
let _ = eventos.send(evento);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transport::websocket::WsFrame;
fn frame(name: &str, msg: Value) -> WsFrame {
WsFrame {
name: name.into(),
msg,
request_id: None,
}
}
struct CtxPrueba {
ctx: Ctx,
rx: broadcast::Receiver<Event>,
resultados: Arc<RwLock<HashMap<String, OperationResult>>>,
}
fn ctx() -> CtxPrueba {
let (tx, rx) = broadcast::channel(64);
let resultados = Arc::new(RwLock::new(HashMap::new()));
CtxPrueba {
ctx: Ctx {
config: IqOptionConfig::default(),
suscripciones: Arc::new(RwLock::new(Vec::new())),
vivo: Arc::new(AtomicBool::new(true)),
cortar: Arc::new(Notify::new()),
abiertas: Arc::new(RwLock::new(HashSet::new())),
router: Arc::new(RequestRouter::new()),
estado: Arc::new(RwLock::new(Estado::default())),
catalogo: Arc::new(RwLock::new(Vec::new())),
sub_velas: Arc::new(RwLock::new(Vec::new())),
eventos: tx,
precios: broadcast::channel(64).0,
resultados: resultados.clone(),
digitales: Arc::new(RwLock::new(HashMap::new())),
},
rx,
resultados,
}
}
#[tokio::test]
async fn cache_no_se_pisa_con_el_segundo_canal() {
let prueba = ctx();
let ctx = prueba.ctx;
let mut rx = prueba.rx;
let resultados = prueba.resultados;
enrutar_evento(
&frame(
"option-closed",
json!({"option_id": 14405260361i64, "result": "win", "amount": 1, "win_enrolled_amount": 1.83}),
),
&ctx,
)
.await;
enrutar_evento(
&frame(
"socket-option-closed",
json!({"id": 14405260361i64, "win": "win", "amount": 1000000}),
),
&ctx,
)
.await;
let cache = resultados.read().await;
let guardado = cache.get("14405260361").copied();
assert_eq!(guardado, Some(OperationResult::Win(0.83)));
drop(cache);
let ev = rx.try_recv().expect("debe haberse emitido un evento");
match ev {
Event::OrderClosed { id, result } => {
assert_eq!(id, "14405260361");
assert_eq!(result, OperationResult::Win(0.83));
}
otra => panic!("evento inesperado: {otra:?}"),
}
assert!(
rx.try_recv().is_err(),
"no debe emitirse un segundo OrderClosed"
);
}
#[test]
fn registro_de_suscripciones_no_duplica_claves() {
let mut lista = Vec::new();
assert!(registrar_suscripcion(
&mut lista,
"velas:EURUSD",
"subscribeMessage",
&json!({"name": "candles-generated"})
));
assert!(!registrar_suscripcion(
&mut lista,
"velas:EURUSD",
"subscribeMessage",
&json!({"name": "candles-generated"})
));
assert_eq!(lista.len(), 1);
assert_eq!(lista[0].clave, "velas:EURUSD");
}
#[test]
fn backoff_duplica_y_satura() {
assert_eq!(siguiente_espera(0, 8_000), 1);
assert_eq!(siguiente_espera(1, 8_000), 2);
assert_eq!(siguiente_espera(4_000, 8_000), 8_000);
assert_eq!(siguiente_espera(8_000, 8_000), 8_000);
assert_eq!(siguiente_espera(8_000, 0), 1);
}
#[test]
fn reconciliacion_solo_rellena_ordenes_pendientes() {
let posicion = |id: &str, pnl: Option<f64>| Position {
id: id.into(),
instrument_type: "turbo-option".into(),
symbol: "EURUSD".into(),
side: Side::Buy,
amount: 1.0,
open_price: Some(1.1),
pnl,
invested: Some(1.0),
open_at: 1,
close_at: Some(2),
leverage: None,
};
let historial = PositionHistory {
positions: vec![
posicion("111", Some(0.83)),
posicion("222", Some(-1.0)),
posicion("333", Some(0.0)),
posicion("999", Some(5.0)),
posicion("111", Some(0.83)),
],
offset: 0,
total: Some(5),
};
let pendientes: HashSet<OrderId> = ["111", "222", "333"]
.into_iter()
.map(str::to_string)
.collect();
let mut filas = reconciliar(&pendientes, &historial);
filas.sort_by(|a, b| a.0.cmp(&b.0));
assert_eq!(
filas,
vec![
("111".to_string(), OperationResult::Win(0.83)),
("222".to_string(), OperationResult::Loss(1.0)),
("333".to_string(), OperationResult::Equal(1.0)),
]
);
}
#[test]
fn solo_la_autenticacion_es_fatal() {
assert!(es_fatal(&BrokerError::Authentication("rechazado".into())));
assert!(!es_fatal(&BrokerError::Connection("cortado".into())));
assert!(!es_fatal(&BrokerError::Timeout));
}
}