|
|
@@ -1,3 +1,4 @@
|
|
|
+use std::collections::BTreeMap;
|
|
|
use std::str::FromStr;
|
|
|
use std::sync::Arc;
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
@@ -8,6 +9,7 @@ use serde_json::{json, Value};
|
|
|
use tokio::sync::Mutex;
|
|
|
use tokio_tungstenite::tungstenite::{Error, Message};
|
|
|
use tracing::{error, info, trace};
|
|
|
+use crate::binance_swap_rest::BinanceSwapRest;
|
|
|
|
|
|
use crate::response_base::ResponseData;
|
|
|
use crate::socket_tool::AbstractWsMode;
|
|
|
@@ -23,6 +25,9 @@ pub enum BinanceSwapSubscribeType {
|
|
|
PuBookTicker,
|
|
|
PuAggTrade,
|
|
|
PuDepth20levels100ms,
|
|
|
+
|
|
|
+ PrAccount,
|
|
|
+ PrBalance
|
|
|
}
|
|
|
|
|
|
//账号信息
|
|
|
@@ -107,6 +112,9 @@ impl BinanceSwapWs {
|
|
|
BinanceSwapSubscribeType::PuBookTicker => false,
|
|
|
BinanceSwapSubscribeType::PuAggTrade => false,
|
|
|
BinanceSwapSubscribeType::PuDepth20levels100ms => false,
|
|
|
+
|
|
|
+ BinanceSwapSubscribeType::PrAccount => {true}
|
|
|
+ BinanceSwapSubscribeType::PrBalance => {true}
|
|
|
} {
|
|
|
return true;
|
|
|
}
|
|
|
@@ -117,7 +125,7 @@ impl BinanceSwapWs {
|
|
|
/*****************************************工具函数********************************************************/
|
|
|
/*******************************************************************************************************/
|
|
|
//订阅枚举解析
|
|
|
- pub fn enum_to_string(symbol: String, subscribe_type: BinanceSwapSubscribeType) -> String {
|
|
|
+ pub fn enum_to_string_pu(symbol: String, subscribe_type: BinanceSwapSubscribeType) -> String {
|
|
|
match subscribe_type {
|
|
|
BinanceSwapSubscribeType::PuAggTrade => {
|
|
|
format!("{}@aggTrade", symbol)
|
|
|
@@ -128,14 +136,40 @@ impl BinanceSwapWs {
|
|
|
BinanceSwapSubscribeType::PuBookTicker => {
|
|
|
format!("{}@bookTicker", symbol)
|
|
|
}
|
|
|
+
|
|
|
+ BinanceSwapSubscribeType::PrAccount => {
|
|
|
+ "".to_string()
|
|
|
+ }
|
|
|
+ BinanceSwapSubscribeType::PrBalance => {
|
|
|
+ "".to_string()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pub fn enum_to_string_pr(listenKey: String, subscribe_type: BinanceSwapSubscribeType) -> String {
|
|
|
+ match subscribe_type {
|
|
|
+ BinanceSwapSubscribeType::PuAggTrade => {
|
|
|
+ "".to_string()
|
|
|
+ }
|
|
|
+ BinanceSwapSubscribeType::PuDepth20levels100ms => {
|
|
|
+ "".to_string()
|
|
|
+ }
|
|
|
+ BinanceSwapSubscribeType::PuBookTicker => {
|
|
|
+ "".to_string()
|
|
|
+ }
|
|
|
+ BinanceSwapSubscribeType::PrAccount => {
|
|
|
+ format!("{}@account", listenKey)
|
|
|
+ }
|
|
|
+ BinanceSwapSubscribeType::PrBalance => {
|
|
|
+ format!("{}@balance", listenKey)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
//订阅信息生成
|
|
|
- pub fn get_subscription(&self) -> String {
|
|
|
+ pub fn get_subscription_pu(&self) -> String {
|
|
|
let mut params = vec![];
|
|
|
for symbol in &self.symbol_s {
|
|
|
for subscribe_type in &self.subscribe_types {
|
|
|
- let ty_str = Self::enum_to_string(symbol.clone(), subscribe_type.clone());
|
|
|
+ let ty_str = Self::enum_to_string_pu(symbol.clone(), subscribe_type.clone());
|
|
|
params.push(ty_str);
|
|
|
}
|
|
|
}
|
|
|
@@ -147,6 +181,21 @@ impl BinanceSwapWs {
|
|
|
});
|
|
|
str.to_string()
|
|
|
}
|
|
|
+ pub fn get_subscription_pr(&self,listenKey:String) -> String {
|
|
|
+ let mut params = vec![];
|
|
|
+ for symbol in &self.symbol_s {
|
|
|
+ for subscribe_type in &self.subscribe_types {
|
|
|
+ let ty_str = Self::enum_to_string_pr(listenKey.clone(), subscribe_type.clone());
|
|
|
+ params.push(ty_str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let str = json!({
|
|
|
+ "method": "REQUEST",
|
|
|
+ "params":params
|
|
|
+ "id": 12 // request ID.
|
|
|
+ });
|
|
|
+ str.to_string()
|
|
|
+ }
|
|
|
/*******************************************************************************************************/
|
|
|
/*****************************************socket基本*****************************************************/
|
|
|
/*******************************************************************************************************/
|
|
|
@@ -156,15 +205,16 @@ impl BinanceSwapWs {
|
|
|
handle_function: F,
|
|
|
_write_tx_am: &Arc<Mutex<UnboundedSender<Message>>>,
|
|
|
write_to_socket_rx: UnboundedReceiver<Message>) -> Result<(), Error>
|
|
|
- where
|
|
|
- F: Fn(ResponseData) -> Future + Clone + Send + 'static + Sync,
|
|
|
- Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
|
+ where
|
|
|
+ F: Fn(ResponseData) -> Future + Clone + Send + 'static + Sync,
|
|
|
+ Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
|
{
|
|
|
let login_is = self.contains_pr();
|
|
|
- let subscription = self.get_subscription();
|
|
|
+ let subscription_pu = self.get_subscription_pu();
|
|
|
let address_url = self.address_url.clone();
|
|
|
let label = self.label.clone();
|
|
|
- // let heartbeat_time = self.heartbeat_time.clone();
|
|
|
+ let heartbeat_time = self.heartbeat_time.clone();
|
|
|
+ let login_param_dto = self.login_param.clone();
|
|
|
|
|
|
|
|
|
//心跳-- 方法内部线程启动
|
|
|
@@ -179,8 +229,26 @@ impl BinanceSwapWs {
|
|
|
let mut subscribe_array = vec![];
|
|
|
if login_is {
|
|
|
//登录相关
|
|
|
+ match login_param_dto {
|
|
|
+ None => {
|
|
|
+ error!("binance_usdt_swap socket 账号信息……");
|
|
|
+ }
|
|
|
+ Some(dto) => {
|
|
|
+ let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
|
|
|
+ btree_map.insert("access_key".to_string(), dto.api_key.clone());
|
|
|
+ btree_map.insert("secret_key".to_string(), dto.api_secret.clone());
|
|
|
+
|
|
|
+ let mut ba_exc = BinanceSwapRest::new(false, btree_map);
|
|
|
+ let rep_data = ba_exc.close_listen_key(json!({ })).await;
|
|
|
+ if (rep_data.code == 200) {
|
|
|
+ let listenKey = rep_data.data["listenKey"].as_str().unwrap();
|
|
|
+ trace!("拿到-listenKey:{}",listenKey.clone());
|
|
|
+ let subscription_pr = self.get_subscription_pr(listenKey.to_string());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- subscribe_array.push(subscription.to_string());
|
|
|
+ // subscribe_array.push(subscription_pu.to_string());
|
|
|
|
|
|
//链接
|
|
|
let t2 = tokio::spawn(async move {
|