|
|
@@ -1,11 +1,8 @@
|
|
|
use std::collections::{BTreeMap, HashMap};
|
|
|
use std::sync::{Arc};
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
-use std::time::Duration;
|
|
|
use lazy_static::lazy_static;
|
|
|
use rust_decimal::Decimal;
|
|
|
-use rust_decimal_macros::dec;
|
|
|
-use serde_json::{json, Value};
|
|
|
use tokio::sync::{Mutex, MutexGuard};
|
|
|
use tracing::info;
|
|
|
use exchanges::binance_swap_rest::BinanceSwapRest;
|
|
|
@@ -13,7 +10,7 @@ use exchanges::binance_swap_ws::{BinanceSwapSubscribeType, BinanceSwapWs, Binanc
|
|
|
use exchanges::response_base::ResponseData;
|
|
|
use standard::exchange::ExchangeEnum;
|
|
|
use standard::exchange_struct_handler::ExchangeStructHandler;
|
|
|
-use standard::{Depth, OrderBook, SpecialDepth};
|
|
|
+use standard::{Depth, OrderBook};
|
|
|
use crate::listener_tools::{DepthMap, RecordMap, TradeMap, update_depth, update_record, update_trade};
|
|
|
const EXCHANGE_NAME: &str = "binance_usdt_swap";
|
|
|
|
|
|
@@ -30,78 +27,40 @@ pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
// 订阅所有币种
|
|
|
let login = BTreeMap::new();
|
|
|
let mut binance_rest = BinanceSwapRest::new(false, login);
|
|
|
- // let response = binance_rest.get_exchange_info().await;
|
|
|
- // let mut symbols = vec![];
|
|
|
- // if response.code == 200 {
|
|
|
- // let data = response.data["symbols"].as_array().unwrap();
|
|
|
- // for info in data {
|
|
|
- // let s = info["symbol"].as_str().unwrap().to_string();
|
|
|
- // if !s.ends_with("USDT") {
|
|
|
- // continue
|
|
|
- // }
|
|
|
- // let symbol = s.replace("USDT", "_USDT");
|
|
|
- // symbols.push(symbol)
|
|
|
- // }
|
|
|
- // }
|
|
|
- // info!(?symbols);
|
|
|
- //
|
|
|
- // for chunk in symbols.chunks(20) {
|
|
|
- // let ws_name = name.to_string();
|
|
|
- // let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
|
|
|
- // let write_tx_am = Arc::new(Mutex::new(write_tx));
|
|
|
- // let symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
- // let is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
- //
|
|
|
- // tokio::spawn(async move {
|
|
|
- // let mut ws = BinanceSwapWs::new_with_tag(ws_name, false, None, BinanceSwapWsType::PublicAndPrivate);
|
|
|
- // ws.set_subscribe(vec![
|
|
|
- // BinanceSwapSubscribeType::PuAggTrade,
|
|
|
- // BinanceSwapSubscribeType::PuKline,
|
|
|
- // ]);
|
|
|
- //
|
|
|
- // // 建立链接
|
|
|
- // ws.set_symbols(symbols_chunk);
|
|
|
- // ws.ws_connect_async(is_shutdown_clone, data_listener, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
- // });
|
|
|
- // }
|
|
|
-
|
|
|
- // 2024-5-7任务 单独订阅ETH的全档位深度数据
|
|
|
- let depth_symbols = vec!["ETH_USDT".to_string()];
|
|
|
- // 每60s初始化一次深度信息
|
|
|
- let symbols_clone = depth_symbols.clone();
|
|
|
- tokio::spawn(async move {
|
|
|
- loop {
|
|
|
- for depth_symbol in &symbols_clone {
|
|
|
- let formated_str = depth_symbol.replace("_", "");
|
|
|
- let mut response = binance_rest.get_order_book(formated_str.as_str(), 1000).await;
|
|
|
-
|
|
|
- response.channel = "depthInit".to_string();
|
|
|
- response.data["a"] = response.data["asks"].clone();
|
|
|
- response.data["b"] = response.data["bids"].clone();
|
|
|
- response.data["s"] = json!(formated_str.as_str());
|
|
|
- response.data["asks"] = Value::Null;
|
|
|
- response.data["bids"] = Value::Null;
|
|
|
-
|
|
|
- data_listener(response).await;
|
|
|
+ let response = binance_rest.get_exchange_info().await;
|
|
|
+ let mut symbols = vec![];
|
|
|
+ if response.code == 200 {
|
|
|
+ let data = response.data["symbols"].as_array().unwrap();
|
|
|
+ for info in data {
|
|
|
+ let s = info["symbol"].as_str().unwrap().to_string();
|
|
|
+ if !s.ends_with("USDT") {
|
|
|
+ continue
|
|
|
}
|
|
|
-
|
|
|
- tokio::time::sleep(Duration::from_secs(10)).await;
|
|
|
+ let symbol = s.replace("USDT", "_USDT");
|
|
|
+ symbols.push(symbol)
|
|
|
}
|
|
|
- });
|
|
|
- let ws_name = name.to_string();
|
|
|
- let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
|
|
|
- let write_tx_am = Arc::new(Mutex::new(write_tx));
|
|
|
- let is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
- tokio::spawn(async move {
|
|
|
- let mut ws = BinanceSwapWs::new_with_tag(ws_name, false, None, BinanceSwapWsType::PublicAndPrivate);
|
|
|
- ws.set_subscribe(vec![
|
|
|
- BinanceSwapSubscribeType::PuDepthUpdate
|
|
|
- ]);
|
|
|
-
|
|
|
- // 建立链接
|
|
|
- ws.set_symbols(depth_symbols);
|
|
|
- ws.ws_connect_async(is_shutdown_clone, data_listener, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
- });
|
|
|
+ }
|
|
|
+ info!(?symbols);
|
|
|
+
|
|
|
+ for chunk in symbols.chunks(20) {
|
|
|
+ let ws_name = name.to_string();
|
|
|
+ let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
|
|
|
+ let write_tx_am = Arc::new(Mutex::new(write_tx));
|
|
|
+ let symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
+ let is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
+
|
|
|
+ tokio::spawn(async move {
|
|
|
+ let mut ws = BinanceSwapWs::new_with_tag(ws_name, false, None, BinanceSwapWsType::PublicAndPrivate);
|
|
|
+ ws.set_subscribe(vec![
|
|
|
+ BinanceSwapSubscribeType::PuAggTrade,
|
|
|
+ BinanceSwapSubscribeType::PuKline,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 建立链接
|
|
|
+ ws.set_symbols(symbols_chunk);
|
|
|
+ ws.ws_connect_async(is_shutdown_clone, data_listener, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 读取数据
|