bitmart_swap_ws.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. use std::io::Read;
  2. use std::str::from_utf8;
  3. use std::sync::Arc;
  4. use std::sync::atomic::AtomicBool;
  5. use std::time::Duration;
  6. use chrono::Utc;
  7. use flate2::bufread::GzDecoder;
  8. use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
  9. use once_cell::sync::Lazy;
  10. use ring::hmac;
  11. use serde_json::{json, Value};
  12. use tokio::sync::Mutex;
  13. use tokio::task;
  14. use tokio_tungstenite::tungstenite::{Error, Message};
  15. use tracing::{error, info, trace};
  16. use crate::response_base::ResponseData;
  17. use crate::socket_tool::{AbstractWsMode, HeartbeatType};
  18. pub(crate) static LOGIN_DATA: Lazy<Mutex<(bool, bool)>> = Lazy::new(|| {
  19. println!("初始化...");
  20. // 0: 需要登录, 1:是否已经登录
  21. Mutex::new((false, false))
  22. });
  23. pub enum BitMartSwapWsType {
  24. Public,
  25. Private,
  26. }
  27. //订阅频道
  28. #[derive(Clone)]
  29. pub enum BitMartSwapSubscribeType {
  30. // 深度
  31. PuFuturesDepth,
  32. // 公开成交
  33. PuFuturesTrades,
  34. // K线数据
  35. PuFuturesRecords,
  36. // // 深度
  37. // PuFuturesDepth,
  38. // // 公开成交
  39. // PuFuturesTrades,
  40. // // K线数据
  41. // PuFuturesRecords,
  42. //
  43. // // 订单
  44. // PrFuturesOrders,
  45. // // 仓位
  46. // PrFuturesPositions,
  47. // // 余额
  48. // PrFuturesBalances,
  49. }
  50. //账号信息
  51. #[derive(Clone)]
  52. #[allow(dead_code)]
  53. pub struct BitMartSwapLogin {
  54. pub api_key: String,
  55. pub secret: String,
  56. pub api_memo: String,
  57. }
  58. #[derive(Clone)]
  59. pub struct BitMartSwapWs {
  60. tag: String, // 类型
  61. address_url: String, // 地址
  62. login_param: Option<BitMartSwapLogin>, // 账号
  63. symbol_s: Vec<String>, // 币对
  64. subscribe_types: Vec<BitMartSwapSubscribeType>, // 订阅
  65. heartbeat_time: u64, // 心跳间隔
  66. }
  67. impl BitMartSwapWs {
  68. /*******************************************************************************************************/
  69. /*****************************************实例化一个对象****************************************************/
  70. /*******************************************************************************************************/
  71. pub fn new(is_colo: bool, login_param: Option<BitMartSwapLogin>, ws_type: BitMartSwapWsType) -> BitMartSwapWs {
  72. return Self::new_with_tag("default-BingxSwapWs".to_string(), is_colo, login_param, ws_type);
  73. }
  74. pub fn new_with_tag(tag: String, _is_colo: bool, login_param: Option<BitMartSwapLogin>, ws_type: BitMartSwapWsType) -> BitMartSwapWs {
  75. /*******公共频道-私有频道数据组装*/
  76. let address_url = match ws_type {
  77. BitMartSwapWsType::Public => {
  78. let url = "wss://openapi-ws.bitmart.com/api?protocol=1.1".to_string();
  79. info!("走普通通道(不支持colo通道):{}", url);
  80. url
  81. }
  82. BitMartSwapWsType::Private => {
  83. let url = "wss://openapi-ws.bitmart.com/user?protocol=1.1".to_string();
  84. info!("走普通通道(不支持colo通道):{}", url);
  85. url
  86. }
  87. };
  88. BitMartSwapWs {
  89. tag,
  90. address_url,
  91. login_param,
  92. symbol_s: vec![],
  93. subscribe_types: vec![],
  94. heartbeat_time: 1000 * 5,
  95. }
  96. }
  97. /*******************************************************************************************************/
  98. /*****************************************订阅函数********************************************************/
  99. /*******************************************************************************************************/
  100. //手动添加订阅信息
  101. pub fn set_subscribe(&mut self, subscribe_types: Vec<BitMartSwapSubscribeType>) {
  102. self.subscribe_types.extend(subscribe_types);
  103. }
  104. //手动添加币对
  105. pub fn set_symbols(&mut self, mut b_array: Vec<String>) {
  106. for symbol in b_array.iter_mut() {
  107. // 大写
  108. *symbol = symbol.to_uppercase();
  109. // 字符串替换
  110. *symbol = symbol.replace("_", "");
  111. }
  112. self.symbol_s = b_array;
  113. }
  114. //频道是否需要登录
  115. fn contains_pr(&self) -> bool {
  116. for t in self.subscribe_types.clone() {
  117. if match t {
  118. BitMartSwapSubscribeType::PuFuturesDepth => false,
  119. BitMartSwapSubscribeType::PuFuturesTrades => false,
  120. BitMartSwapSubscribeType::PuFuturesRecords => false,
  121. } {
  122. return true;
  123. }
  124. }
  125. false
  126. }
  127. /*******************************************************************************************************/
  128. /*****************************************工具函数********************************************************/
  129. /*******************************************************************************************************/
  130. //订阅枚举解析
  131. pub fn enum_to_string(symbol: String, subscribe_type: BitMartSwapSubscribeType, _login_param: Option<BitMartSwapLogin>) -> String {
  132. // let access_key;
  133. // let secret_key;
  134. // match login_param {
  135. // None => {
  136. // access_key = "".to_string();
  137. // secret_key = "".to_string();
  138. // }
  139. // Some(param) => {
  140. // access_key = param.api_key.clone();
  141. // secret_key = param.secret.clone();
  142. // }
  143. // }
  144. // let cid = "";
  145. match subscribe_type {
  146. BitMartSwapSubscribeType::PuFuturesDepth => {
  147. format!("futures/depth5:{}", symbol.to_uppercase())
  148. }
  149. BitMartSwapSubscribeType::PuFuturesTrades => {
  150. format!("futures/trade:{}", symbol.to_uppercase())
  151. }
  152. BitMartSwapSubscribeType::PuFuturesRecords => {
  153. format!("futures/klineBin1m:{}", symbol.to_uppercase())
  154. }
  155. }
  156. }
  157. //订阅信息生成
  158. pub fn get_subscription(&self) -> Value {
  159. let mut args = vec![];
  160. // 只获取第一个
  161. for symbol in &self.symbol_s {
  162. for subscribe_type in &self.subscribe_types {
  163. let ty_str = Self::enum_to_string(symbol.clone(),
  164. subscribe_type.clone(),
  165. self.login_param.clone(),
  166. );
  167. args.push(ty_str);
  168. }
  169. }
  170. json!({
  171. "action":"subscribe",
  172. "args":args
  173. })
  174. }
  175. /*******************************************************************************************************/
  176. /*****************************************socket基本*****************************************************/
  177. /*******************************************************************************************************/
  178. //链接
  179. pub async fn ws_connect_async<F, Future>(&mut self,
  180. is_shutdown_arc: Arc<AtomicBool>,
  181. handle_function: F,
  182. write_tx_am: &Arc<Mutex<UnboundedSender<Message>>>,
  183. write_to_socket_rx: UnboundedReceiver<Message>) -> Result<(), Error>
  184. where
  185. F: Fn(ResponseData) -> Future + Clone + Send + 'static + Sync,
  186. Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
  187. {
  188. let login_is = self.contains_pr();
  189. let login_param = self.login_param.clone();
  190. let subscription = self.get_subscription();
  191. let address_url = self.address_url.clone();
  192. let label = self.tag.clone();
  193. let heartbeat_time = self.heartbeat_time.clone();
  194. //心跳-- 方法内部线程启动
  195. let write_tx_clone1 = Arc::clone(write_tx_am);
  196. let write_tx_clone2 = Arc::clone(write_tx_am);
  197. tokio::spawn(async move {
  198. trace!("线程-异步心跳-开始");
  199. AbstractWsMode::ping_or_pong(write_tx_clone1, HeartbeatType::Ping, heartbeat_time).await;
  200. trace!("线程-异步心跳-结束");
  201. });
  202. //设置订阅
  203. let mut subscribe_array = vec![];
  204. subscribe_array.push(subscription.to_string());
  205. //链接
  206. let t2 = tokio::spawn(async move {
  207. let write_to_socket_rx_arc = Arc::new(Mutex::new(write_to_socket_rx));
  208. info!("启动连接");
  209. loop {
  210. info!("BitMart_usdt_swap socket 连接中……");
  211. // 需要登录
  212. if login_is {
  213. let mut login_data = LOGIN_DATA.lock().await;
  214. let login_param_real = login_param.clone().unwrap();
  215. login_data.0 = true;
  216. let timestamp = Utc::now().timestamp_millis().to_string();
  217. let api_key = login_param_real.api_key.clone();
  218. let secret_key = login_param_real.secret.clone();
  219. let api_memo = login_param_real.api_memo.clone();
  220. // let timestamp = "1589267764859".to_string();
  221. // let api_key = "80618e45710812162b04892c7ee5ead4a3cc3e56".to_string();
  222. // let secret_key = "6c6c98544461bbe71db2bca4c6d7fd0021e0ba9efc215f9c6ad41852df9d9df9".to_string();
  223. // let api_memo = "test001".to_string();
  224. let sign = {
  225. let message = format!("{}#{}#bitmart.WebSocket", timestamp.clone(), api_memo);
  226. trace!("组装数据:\n{}", message);
  227. let signed_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_ref());
  228. let sign = hex::encode(hmac::sign(&signed_key, message.as_bytes()).as_ref());
  229. sign
  230. };
  231. trace!("参考sign-3ceeb7e1b8cb165a975e28a2e2dfaca4d30b358873c0351c1a071d8c83314556",);
  232. trace!("自己的sign-{}",sign.clone());
  233. let mut args = vec![];
  234. args.push(api_key.clone());
  235. args.push(timestamp.clone());
  236. args.push(sign.clone());
  237. args.push(String::from("web"));
  238. // {"action":"access","args":["<API_KEY>","<timestamp>","<sign>","<dev>"]}
  239. let login_param = json!({
  240. "action": "access",
  241. "args": [
  242. api_key, timestamp.as_str(),sign.as_str(),"web"
  243. ]
  244. });
  245. let login_str = login_param.to_string();
  246. info!("发起ws登录: {}", login_str);
  247. let write_tx_c = Arc::clone(&write_tx_clone2);
  248. AbstractWsMode::send_subscribe(write_tx_c, Message::Text(login_str)).await;
  249. }
  250. AbstractWsMode::ws_connect_async(is_shutdown_arc.clone(), handle_function.clone(), address_url.clone(),
  251. login_is, label.clone(), subscribe_array.clone(), write_to_socket_rx_arc.clone(),
  252. Self::message_text_sync, Self::message_ping, Self::message_pong, Self::message_binary_sync,
  253. ).await;
  254. let mut login_data = LOGIN_DATA.lock().await;
  255. // 断联后 设置为没有登录
  256. login_data.1 = false;
  257. info!("BitMart_usdt_swap socket 断连,1s以后重连……");
  258. error!("BitMart_usdt_swap socket 断连,1s以后重连……");
  259. tokio::time::sleep(Duration::from_secs(1)).await;
  260. }
  261. });
  262. tokio::try_join!(t2).unwrap();
  263. trace!("线程-心跳与链接-结束");
  264. Ok(())
  265. }
  266. /*******************************************************************************************************/
  267. /*****************************************数据解析*****************************************************/
  268. /*******************************************************************************************************/
  269. //数据解析-Text
  270. pub async fn message_text(text: String) -> Option<ResponseData> {
  271. let response_data = Self::ok_text(text).await;
  272. Option::from(response_data)
  273. }
  274. pub fn message_text_sync(text: String) -> Option<ResponseData> {
  275. // 使用 tokio::task::block_in_place 来等待异步函数的结果
  276. task::block_in_place(|| {
  277. tokio::runtime::Handle::current().block_on(Self::message_text(text))
  278. })
  279. }
  280. //数据解析-ping
  281. pub fn message_ping(_pi: Vec<u8>) -> Option<ResponseData> {
  282. return Option::from(ResponseData::new("".to_string(), -300, "success".to_string(), Value::Null));
  283. }
  284. //数据解析-pong
  285. pub fn message_pong(_po: Vec<u8>) -> Option<ResponseData> {
  286. return Option::from(ResponseData::new("".to_string(), -301, "success".to_string(), Value::Null));
  287. }
  288. //数据解析-二进制
  289. pub async fn message_binary(binary: Vec<u8>) -> Option<ResponseData> {
  290. //二进制WebSocket消息
  291. let message_str = Self::parse_zip_data(binary);
  292. let response_data = Self::ok_text(message_str).await;
  293. Option::from(response_data)
  294. }
  295. pub fn message_binary_sync(binary: Vec<u8>) -> Option<ResponseData> {
  296. // 使用 tokio::task::block_in_place 来等待异步函数的结果
  297. task::block_in_place(|| {
  298. tokio::runtime::Handle::current().block_on(Self::message_binary(binary))
  299. })
  300. }
  301. //数据解析
  302. pub async fn ok_text(text: String) -> ResponseData
  303. {
  304. // info!("原始数据:{}", text);
  305. let mut res_data = ResponseData::new("".to_string(), 200, "success".to_string(), Value::Null);
  306. let json_value: Value = serde_json::from_str(&text).unwrap();
  307. // {"action":"access","success":true}
  308. let action = json_value["action"].as_str();
  309. match action {
  310. None => {}
  311. Some(r) => {
  312. match r {
  313. "access" => {
  314. /*登录响应*/
  315. let success = json_value["success"].as_bool();
  316. match success {
  317. None => {}
  318. Some(s) => {
  319. if s {
  320. res_data.code = -200;
  321. res_data.message = "登录成功".to_string();
  322. } else {
  323. res_data.code = 400;
  324. res_data.message = format!("登录失败:{}", json_value["error"].as_str().unwrap());
  325. }
  326. return res_data;
  327. }
  328. }
  329. }
  330. "subscribe" => {
  331. /*订阅响应*/
  332. let success = json_value["success"].as_bool();
  333. match success {
  334. None => {}
  335. Some(s) => {
  336. if s {
  337. res_data.code = -201;
  338. res_data.message = format!("订阅成功:{}", json_value["request"].clone().to_string());
  339. } else {
  340. res_data.code = 400;
  341. res_data.message = format!("订阅失败:{}", json_value["error"].as_str().unwrap());
  342. }
  343. return res_data;
  344. }
  345. }
  346. }
  347. "unsubscribe" => {
  348. /*取消订阅响应*/
  349. let success = json_value["success"].as_bool();
  350. match success {
  351. None => {}
  352. Some(s) => {
  353. if s {
  354. res_data.code = -201;
  355. res_data.message = format!("取消-订阅成功:{}", json_value["request"].clone().to_string());
  356. } else {
  357. res_data.code = 400;
  358. res_data.message = format!("取消-订阅失败:{}", json_value["error"].as_str().unwrap());
  359. }
  360. return res_data;
  361. }
  362. }
  363. }
  364. _ => {}
  365. }
  366. }
  367. }
  368. let group = json_value["group"].as_str();
  369. match group {
  370. Some(ch) => {
  371. res_data.code = 200;
  372. res_data.data = json_value["data"].clone();
  373. //订阅数据 甄别
  374. if ch.contains("futures/depth") {
  375. res_data.channel = "futures.order_book".to_string();
  376. } else if ch.contains("futures/trade") {
  377. res_data.channel = "futures.trades".to_string();
  378. } else if ch.contains("futures/klineBin") {
  379. res_data.channel = "futures.candlesticks".to_string();
  380. } else {
  381. res_data.channel = "未知推送数据".to_string();
  382. }
  383. return res_data;
  384. // match data {
  385. // Some(_) => {
  386. //
  387. // }
  388. // None => {
  389. // res_data.channel = format!("{}", ch);
  390. // res_data.code = 400;
  391. // res_data.data = data.clone();
  392. // return res_data;
  393. // }
  394. // }
  395. }
  396. None => {}
  397. }
  398. res_data.code = 400;
  399. res_data.message = format!("未知响应内容");
  400. res_data.data = text.parse().unwrap();
  401. trace!("--------------------------------");
  402. res_data
  403. }
  404. fn parse_zip_data(p0: Vec<u8>) -> String {
  405. // 创建一个GzDecoder的实例,将压缩数据作为输入
  406. let mut decoder = GzDecoder::new(&p0[..]);
  407. // 创建一个缓冲区来存放解压缩后的数据
  408. let mut decompressed_data = Vec::new();
  409. // 读取解压缩的数据到缓冲区中
  410. decoder.read_to_end(&mut decompressed_data).expect("解压缩失败");
  411. let result = from_utf8(&decompressed_data)
  412. .expect("解压缩后的数据不是有效的UTF-8");
  413. // info!("解压缩数据 {:?}", result);
  414. result.to_string()
  415. }
  416. }