|
|
@@ -1,21 +1,89 @@
|
|
|
-// use std::collections::BTreeMap;
|
|
|
-// use std::sync::Arc;
|
|
|
-// use std::sync::atomic::AtomicBool;
|
|
|
+use std::collections::BTreeMap;
|
|
|
+use std::sync::Arc;
|
|
|
+use std::sync::atomic::AtomicBool;
|
|
|
+use futures_util::StreamExt;
|
|
|
// use rust_decimal::Decimal;
|
|
|
-// use tokio::sync::Mutex;
|
|
|
+use tokio::sync::Mutex;
|
|
|
+use tracing::info;
|
|
|
+use exchanges::okx_swap_ws::{OkxSwapLogin, OkxSwapSubscribeType, OkxSwapWs, OkxSwapWsType};
|
|
|
// use exchanges::response_base::ResponseData;
|
|
|
-// use crate::quant::Quant;
|
|
|
-
|
|
|
-// pub async fn okex_swap_run(bool_v1: Arc<AtomicBool>,
|
|
|
-// is_trade: bool,
|
|
|
-// quant_arc: Arc<Mutex<Quant>>,
|
|
|
-// name: String,
|
|
|
-// symbols: Vec<String>,
|
|
|
-// is_colo: bool,
|
|
|
-// exchange_params: BTreeMap<String, String>) {
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
+use crate::quant::Quant;
|
|
|
+
|
|
|
+pub async fn okex_swap_run(bool_v1: Arc<AtomicBool>,
|
|
|
+ is_trade: bool,
|
|
|
+ _quant_arc: Arc<Mutex<Quant>>,
|
|
|
+ name: String,
|
|
|
+ symbols: Vec<String>,
|
|
|
+ is_colo: bool,
|
|
|
+ exchange_params: BTreeMap<String, String>) {
|
|
|
+ // 启动公共频道
|
|
|
+ let (write_tx_public, write_rx_public) = futures_channel::mpsc::unbounded();
|
|
|
+ let (read_tx_public, mut read_rx_public) = futures_channel::mpsc::unbounded();
|
|
|
+
|
|
|
+ let mut ws_public = OkxSwapWs::new_label(name.clone(), is_colo, None, OkxSwapWsType::Public);
|
|
|
+ ws_public.set_symbols(symbols.clone());
|
|
|
+ if is_trade {
|
|
|
+ ws_public.set_subscribe(vec![
|
|
|
+ OkxSwapSubscribeType::PuBooks5
|
|
|
+ ])
|
|
|
+ } else {
|
|
|
+ ws_public.set_subscribe(vec![
|
|
|
+ OkxSwapSubscribeType::PuBooks50L2tbt
|
|
|
+ ])
|
|
|
+ }
|
|
|
+ // 挂起公共ws
|
|
|
+ let write_tx_am_public = Arc::new(Mutex::new(write_tx_public));
|
|
|
+ let bool_clone_public = Arc::clone(&bool_v1);
|
|
|
+ tokio::spawn(async move {
|
|
|
+ ws_public.ws_connect_async(bool_clone_public,
|
|
|
+ &write_tx_am_public,
|
|
|
+ write_rx_public,
|
|
|
+ read_tx_public).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+ });
|
|
|
+ // 接收public数据
|
|
|
+ tokio::spawn(async move {
|
|
|
+ loop {
|
|
|
+ if let Some(public_data) = read_rx_public.next().await {
|
|
|
+ info!(?public_data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if is_trade {
|
|
|
+ let (write_tx_private, write_rx_private) = futures_channel::mpsc::unbounded();
|
|
|
+ let (read_tx_private, mut read_rx_private) = futures_channel::mpsc::unbounded();
|
|
|
+ let auth = Some(parse_btree_map_to_okx_swap_login(exchange_params));
|
|
|
+
|
|
|
+ let mut ws_private = OkxSwapWs::new_label(name.clone(), is_colo, auth, OkxSwapWsType::Private);
|
|
|
+ ws_private.set_symbols(symbols.clone());
|
|
|
+ ws_private.set_subscribe(vec![
|
|
|
+ OkxSwapSubscribeType::PrBalanceAndPosition,
|
|
|
+ OkxSwapSubscribeType::PrAccount("USDT".to_string()),
|
|
|
+ OkxSwapSubscribeType::PrOrders
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+ // 挂起私有ws
|
|
|
+ let write_tx_am_private = Arc::new(Mutex::new(write_tx_private));
|
|
|
+ let bool_clone_private = Arc::clone(&bool_v1);
|
|
|
+ tokio::spawn(async move {
|
|
|
+ ws_private.ws_connect_async(bool_clone_private,
|
|
|
+ &write_tx_am_private,
|
|
|
+ write_rx_private,
|
|
|
+ read_tx_private).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+ });
|
|
|
+
|
|
|
+ // 接收private信息
|
|
|
+ tokio::spawn(async move {
|
|
|
+ loop {
|
|
|
+ if let Some(private_data) = read_rx_private.next().await {
|
|
|
+ info!(?private_data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// async fn on_data(bot_arc_clone: Arc<Mutex<Quant>>,
|
|
|
// update_flag_u: &mut Decimal,
|
|
|
// multiplier: Decimal,
|
|
|
@@ -25,3 +93,11 @@
|
|
|
// data: ResponseData) {
|
|
|
//
|
|
|
// }
|
|
|
+
|
|
|
+fn parse_btree_map_to_okx_swap_login(exchange_params: BTreeMap<String, String>) -> OkxSwapLogin {
|
|
|
+ OkxSwapLogin {
|
|
|
+ api_key: exchange_params.get("access_key").unwrap().clone(),
|
|
|
+ secret_key: exchange_params.get("secret_key").unwrap().clone(),
|
|
|
+ passphrase: exchange_params.get("pass_key").unwrap().clone(),
|
|
|
+ }
|
|
|
+}
|