|
|
@@ -1,138 +1,138 @@
|
|
|
-use std::collections::{BTreeMap, HashMap};
|
|
|
-use std::str::FromStr;
|
|
|
-use std::sync::{Arc};
|
|
|
-use std::sync::atomic::AtomicBool;
|
|
|
-use lazy_static::lazy_static;
|
|
|
-use rust_decimal::Decimal;
|
|
|
-use rust_decimal_macros::dec;
|
|
|
-use tokio::sync::{Mutex};
|
|
|
-use tracing::info;
|
|
|
-use exchanges::okx_swap_rest::OkxSwapRest;
|
|
|
-use exchanges::okx_swap_ws::{OkxSwapSubscribeType, OkxSwapWs, OkxSwapWsType};
|
|
|
-use exchanges::response_base::ResponseData;
|
|
|
-use serde_json::json;
|
|
|
-use standard::exchange::ExchangeEnum;
|
|
|
-use standard::exchange_struct_handler::ExchangeStructHandler;
|
|
|
-use crate::listener_tools::{RecordMap, TradeMap, update_record, update_trade};
|
|
|
-
|
|
|
-const EXCHANGE_NAME: &str = "okx_usdt_swap";
|
|
|
-
|
|
|
-lazy_static! {
|
|
|
- // static ref DEPTH_MAP: Mutex<DepthMap> = Mutex::new(HashMap::new());
|
|
|
- static ref TRADES_MAP: Mutex<TradeMap> = Mutex::new(HashMap::new());
|
|
|
- static ref RECORD_MAP: Mutex<RecordMap> = Mutex::new(HashMap::new());
|
|
|
- static ref MUL_MAP: Mutex<HashMap<String, Decimal>> = Mutex::new(HashMap::new());
|
|
|
-}
|
|
|
-
|
|
|
-pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
- let name = "okx_usdt_swap_listener";
|
|
|
- // 订阅所有币种
|
|
|
- let login = BTreeMap::new();
|
|
|
- let mut okx_rest = OkxSwapRest::new(false, login);
|
|
|
- let params = json!({
|
|
|
- "instType":"SWAP"
|
|
|
- });
|
|
|
- let response = okx_rest.get_market(params).await;
|
|
|
- let mut symbols = vec![];
|
|
|
- if response.code == 200 {
|
|
|
- let symbol_infos = response.data.as_array().unwrap();
|
|
|
- let mut mul_map = MUL_MAP.lock().await;
|
|
|
- for symbol_info in symbol_infos {
|
|
|
- let ct_val_ccy = symbol_info["ctValCcy"].as_str().unwrap();
|
|
|
- let settle_ccy = symbol_info["settleCcy"].as_str().unwrap();
|
|
|
- if settle_ccy != "USDT" { continue; };
|
|
|
- let symbol = format!("{}_{}", ct_val_ccy, settle_ccy);
|
|
|
- let mul = Decimal::from_str(symbol_info["ctMult"].as_str().unwrap()).unwrap();
|
|
|
- mul_map.insert(symbol.clone(), mul);
|
|
|
-
|
|
|
- symbols.push(symbol)
|
|
|
- }
|
|
|
- }
|
|
|
- for chunk in symbols.chunks(20) {
|
|
|
- let bu_ws_name = name.to_string();
|
|
|
- let (bu_write_tx, bu_write_rx) = futures_channel::mpsc::unbounded();
|
|
|
- let bu_write_tx_am = Arc::new(Mutex::new(bu_write_tx));
|
|
|
- let bu_symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
- let bu_is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
- tokio::spawn(async move {
|
|
|
- let mut ws = OkxSwapWs::new_with_tag(bu_ws_name.clone(), false, None, OkxSwapWsType::Business);
|
|
|
- ws.set_subscribe(vec![
|
|
|
- OkxSwapSubscribeType::BuFuturesRecords,
|
|
|
- ]);
|
|
|
-
|
|
|
- // 建立链接
|
|
|
- ws.set_symbols(bu_symbols_chunk);
|
|
|
- ws.ws_connect_async(bu_is_shutdown_clone, data_listener, &bu_write_tx_am, bu_write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
- });
|
|
|
-
|
|
|
- let pub_ws_name = name.to_string();
|
|
|
- let (pub_write_tx, pub_write_rx) = futures_channel::mpsc::unbounded();
|
|
|
- let pub_write_tx_am = Arc::new(Mutex::new(pub_write_tx));
|
|
|
- let pub_symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
- let pub_is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
- tokio::spawn(async move {
|
|
|
- let mut ws = OkxSwapWs::new_with_tag(pub_ws_name.clone(), false, None, OkxSwapWsType::Public);
|
|
|
- ws.set_subscribe(vec![
|
|
|
- OkxSwapSubscribeType::PuFuturesTrades,
|
|
|
- ]);
|
|
|
-
|
|
|
- // 建立链接
|
|
|
- ws.set_symbols(pub_symbols_chunk);
|
|
|
- ws.ws_connect_async(pub_is_shutdown_clone, data_listener, &pub_write_tx_am, pub_write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
- });
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// 读取数据
|
|
|
-pub async fn data_listener(response: ResponseData) {
|
|
|
- if response.code != 200 {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- match response.channel.as_str() {
|
|
|
- // 深度数据
|
|
|
- "futures.order_book" => {
|
|
|
- // let depth = ExchangeStructHandler::order_book_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
- //
|
|
|
- // update_depth(&depth).await;
|
|
|
- }
|
|
|
- // 订单流数据
|
|
|
- "futures.trades" => {
|
|
|
- let mut trades = ExchangeStructHandler::trades_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
- let mul_map = MUL_MAP.lock().await;
|
|
|
-
|
|
|
- for trade in trades.iter_mut() {
|
|
|
- // 真实交易量处理,因为okx的量都是张数
|
|
|
- let mul = mul_map[trade.symbol.as_str()];
|
|
|
- let mut real_size = trade.size * mul * trade.price;
|
|
|
- real_size.rescale(2);
|
|
|
- trade.size = real_size;
|
|
|
-
|
|
|
- // 更新到本地数据库
|
|
|
- let trades_map = TRADES_MAP.lock().await;
|
|
|
- update_trade(trade, trades_map, EXCHANGE_NAME).await;
|
|
|
- }
|
|
|
- }
|
|
|
- // k线数据
|
|
|
- "futures.candlesticks" => {
|
|
|
- let mut records = ExchangeStructHandler::records_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
-
|
|
|
- let mul_map = MUL_MAP.lock().await;
|
|
|
- for record in records.iter_mut() {
|
|
|
- // 真实交易量处理,因为okx的量都是张数
|
|
|
- let mul = mul_map[record.symbol.as_str()];
|
|
|
- let mid_price = (record.high + record.low) * dec!(0.5);
|
|
|
- let mut real_volume = record.volume * mul * mid_price;
|
|
|
- real_volume.rescale(2);
|
|
|
- record.volume = real_volume;
|
|
|
-
|
|
|
- // 更新到本地数据库
|
|
|
- let record_map = RECORD_MAP.lock().await;
|
|
|
- update_record(record, record_map, EXCHANGE_NAME).await;
|
|
|
- }
|
|
|
- }
|
|
|
- _ => {
|
|
|
- info!("48 未知的数据类型: {:?}", response)
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+// use std::collections::{BTreeMap, HashMap};
|
|
|
+// use std::str::FromStr;
|
|
|
+// use std::sync::{Arc};
|
|
|
+// use std::sync::atomic::AtomicBool;
|
|
|
+// use lazy_static::lazy_static;
|
|
|
+// use rust_decimal::Decimal;
|
|
|
+// use rust_decimal_macros::dec;
|
|
|
+// use tokio::sync::{Mutex};
|
|
|
+// use tracing::info;
|
|
|
+// use exchanges::okx_swap_rest::OkxSwapRest;
|
|
|
+// use exchanges::okx_swap_ws::{OkxSwapSubscribeType, OkxSwapWs, OkxSwapWsType};
|
|
|
+// use exchanges::response_base::ResponseData;
|
|
|
+// use serde_json::json;
|
|
|
+// use standard::exchange::ExchangeEnum;
|
|
|
+// use standard::exchange_struct_handler::ExchangeStructHandler;
|
|
|
+// use crate::listener_tools::{RecordMap, TradeMap, update_record, update_trade};
|
|
|
+//
|
|
|
+// const EXCHANGE_NAME: &str = "okx_usdt_swap";
|
|
|
+//
|
|
|
+// lazy_static! {
|
|
|
+// // static ref DEPTH_MAP: Mutex<DepthMap> = Mutex::new(HashMap::new());
|
|
|
+// static ref TRADES_MAP: Mutex<TradeMap> = Mutex::new(HashMap::new());
|
|
|
+// static ref RECORD_MAP: Mutex<RecordMap> = Mutex::new(HashMap::new());
|
|
|
+// static ref MUL_MAP: Mutex<HashMap<String, Decimal>> = Mutex::new(HashMap::new());
|
|
|
+// }
|
|
|
+//
|
|
|
+// pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
+// let name = "okx_usdt_swap_listener";
|
|
|
+// // 订阅所有币种
|
|
|
+// let login = BTreeMap::new();
|
|
|
+// let mut okx_rest = OkxSwapRest::new(false, login);
|
|
|
+// let params = json!({
|
|
|
+// "instType":"SWAP"
|
|
|
+// });
|
|
|
+// let response = okx_rest.get_market(params).await;
|
|
|
+// let mut symbols = vec![];
|
|
|
+// if response.code == 200 {
|
|
|
+// let symbol_infos = response.data.as_array().unwrap();
|
|
|
+// let mut mul_map = MUL_MAP.lock().await;
|
|
|
+// for symbol_info in symbol_infos {
|
|
|
+// let ct_val_ccy = symbol_info["ctValCcy"].as_str().unwrap();
|
|
|
+// let settle_ccy = symbol_info["settleCcy"].as_str().unwrap();
|
|
|
+// if settle_ccy != "USDT" { continue; };
|
|
|
+// let symbol = format!("{}_{}", ct_val_ccy, settle_ccy);
|
|
|
+// let mul = Decimal::from_str(symbol_info["ctMult"].as_str().unwrap()).unwrap();
|
|
|
+// mul_map.insert(symbol.clone(), mul);
|
|
|
+//
|
|
|
+// symbols.push(symbol)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// for chunk in symbols.chunks(20) {
|
|
|
+// let bu_ws_name = name.to_string();
|
|
|
+// let (bu_write_tx, bu_write_rx) = futures_channel::mpsc::unbounded();
|
|
|
+// let bu_write_tx_am = Arc::new(Mutex::new(bu_write_tx));
|
|
|
+// let bu_symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
+// let bu_is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
+// tokio::spawn(async move {
|
|
|
+// let mut ws = OkxSwapWs::new_with_tag(bu_ws_name.clone(), false, None, OkxSwapWsType::Business);
|
|
|
+// ws.set_subscribe(vec![
|
|
|
+// OkxSwapSubscribeType::BuFuturesRecords,
|
|
|
+// ]);
|
|
|
+//
|
|
|
+// // 建立链接
|
|
|
+// ws.set_symbols(bu_symbols_chunk);
|
|
|
+// ws.ws_connect_async(bu_is_shutdown_clone, data_listener, &bu_write_tx_am, bu_write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+// });
|
|
|
+//
|
|
|
+// let pub_ws_name = name.to_string();
|
|
|
+// let (pub_write_tx, pub_write_rx) = futures_channel::mpsc::unbounded();
|
|
|
+// let pub_write_tx_am = Arc::new(Mutex::new(pub_write_tx));
|
|
|
+// let pub_symbols_chunk = chunk.iter().cloned().collect::<Vec<String>>();
|
|
|
+// let pub_is_shutdown_clone = Arc::clone(&is_shutdown_arc);
|
|
|
+// tokio::spawn(async move {
|
|
|
+// let mut ws = OkxSwapWs::new_with_tag(pub_ws_name.clone(), false, None, OkxSwapWsType::Public);
|
|
|
+// ws.set_subscribe(vec![
|
|
|
+// OkxSwapSubscribeType::PuFuturesTrades,
|
|
|
+// ]);
|
|
|
+//
|
|
|
+// // 建立链接
|
|
|
+// ws.set_symbols(pub_symbols_chunk);
|
|
|
+// ws.ws_connect_async(pub_is_shutdown_clone, data_listener, &pub_write_tx_am, pub_write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
|
|
|
+// });
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 读取数据
|
|
|
+// pub async fn data_listener(response: ResponseData) {
|
|
|
+// if response.code != 200 {
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// match response.channel.as_str() {
|
|
|
+// // 深度数据
|
|
|
+// "futures.order_book" => {
|
|
|
+// // let depth = ExchangeStructHandler::order_book_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
+// //
|
|
|
+// // update_depth(&depth).await;
|
|
|
+// }
|
|
|
+// // 订单流数据
|
|
|
+// "futures.trades" => {
|
|
|
+// let mut trades = ExchangeStructHandler::trades_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
+// let mul_map = MUL_MAP.lock().await;
|
|
|
+//
|
|
|
+// for trade in trades.iter_mut() {
|
|
|
+// // 真实交易量处理,因为okx的量都是张数
|
|
|
+// let mul = mul_map[trade.symbol.as_str()];
|
|
|
+// let mut real_size = trade.size * mul * trade.price;
|
|
|
+// real_size.rescale(2);
|
|
|
+// trade.size = real_size;
|
|
|
+//
|
|
|
+// // 更新到本地数据库
|
|
|
+// let trades_map = TRADES_MAP.lock().await;
|
|
|
+// update_trade(trade, trades_map, EXCHANGE_NAME).await;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// // k线数据
|
|
|
+// "futures.candlesticks" => {
|
|
|
+// let mut records = ExchangeStructHandler::records_handle(ExchangeEnum::OkxSwap, &response);
|
|
|
+//
|
|
|
+// let mul_map = MUL_MAP.lock().await;
|
|
|
+// for record in records.iter_mut() {
|
|
|
+// // 真实交易量处理,因为okx的量都是张数
|
|
|
+// let mul = mul_map[record.symbol.as_str()];
|
|
|
+// let mid_price = (record.high + record.low) * dec!(0.5);
|
|
|
+// let mut real_volume = record.volume * mul * mid_price;
|
|
|
+// real_volume.rescale(2);
|
|
|
+// record.volume = real_volume;
|
|
|
+//
|
|
|
+// // 更新到本地数据库
|
|
|
+// let record_map = RECORD_MAP.lock().await;
|
|
|
+// update_record(record, record_map, EXCHANGE_NAME).await;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// _ => {
|
|
|
+// info!("48 未知的数据类型: {:?}", response)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|