use std::collections::BTreeMap; use std::sync::Arc; use std::sync::atomic::AtomicBool; use serde_json::json; use tokio::sync::Mutex; use tracing::trace; use exchanges::bitmart_swap_rest::BitMartSwapRest; use exchanges::bitmart_swap_ws::{BitMartSwapLogin, BitMartSwapSubscribeType, BitMartSwapWs, BitMartSwapWsType}; use exchanges::proxy; use exchanges::response_base::ResponseData; const ACCESS_KEY: &str = "cf821f946b2ff9af69dd4a9fa80e862018c99bf4"; const SECRET_KEY: &str = "7148eeec5c7234c5a54db446dc4b8cb6bacad3d54c0cdddf8ba9af85c6580600"; const API_MEMO: &str = "local\ "; //ws-订阅公共频道信息 #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn ws_custom_subscribe() { global::log_utils::init_log_with_trace(); let (write_tx, write_rx) = futures_channel::mpsc::unbounded(); let (_, mut read_rx) = futures_channel::mpsc::unbounded::(); // let (write_tx, write_rx) = tokio::sync::broadcast::channel::(10); // let (read_tx, mut read_rx) = tokio::sync::broadcast::channel::(10); let write_tx_am = Arc::new(Mutex::new(write_tx)); let is_shutdown_arc = Arc::new(AtomicBool::new(true)); //读取 let _is_shutdown_arc_clone = Arc::clone(&is_shutdown_arc); let _tr = tokio::spawn(async move { trace!("线程-数据读取-开启"); loop { // 从通道中接收并丢弃所有的消息,直到通道为空 while let Ok(Some(_)) = read_rx.try_next() { // 从通道中接收并丢弃所有的消息,直到通道为空 while let Ok(Some(_)) = read_rx.try_next() { // 消息被忽略 } } } // trace!("线程-数据读取-结束"); }); //写数据 // let bool_v2_clone = Arc::clone(&is_shutdown_arc); // let write_tx_clone = Arc::clone(&write_tx_am); // let su = ws.get_subscription(); // let tw = tokio::spawn(async move { // trace!("线程-数据写入-开始"); // loop { // tokio::time::sleep(Duration::from_millis(20 * 1000)).await; // // let close_frame = CloseFrame { // // code: CloseCode::Normal, // // reason: Cow::Borrowed("Bye bye"), // // }; // // let message = Message::Close(Some(close_frame)); // // // let message = Message::Text(su.clone()); // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await; // trace!("发送指令成功"); // } // trace!("线程-数据写入-结束"); // }); let fun = move |data: ResponseData| { async move { trace!("---传入的方法~~~~{:?}", data); } }; let param = BitMartSwapLogin { api_key: ACCESS_KEY.to_string(), secret: SECRET_KEY.to_string(), api_memo: API_MEMO.to_string(), }; let t1 = tokio::spawn(async move { let mut ws = get_ws(Option::from(param), BitMartSwapWsType::Private); ws.set_symbols(vec!["BTC_USDT".to_string(), "ETC_USDT".to_string()]); ws.set_subscribe(vec![ // BitMartSwapSubscribeType::PuFuturesDepth, BitMartSwapSubscribeType::PrFuturesOrders, BitMartSwapSubscribeType::PrFuturesPositions, BitMartSwapSubscribeType::PrFuturesBalances, ]); //链接 let bool_v3_clone = Arc::clone(&is_shutdown_arc); ws.ws_connect_async(bool_v3_clone, fun, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)"); trace!("test 唯一线程结束--"); }); tokio::try_join!(t1).unwrap(); trace!("当此结束"); trace!("重启!"); trace!("参考交易所关闭"); return; } fn get_ws(btree_map: Option, ws_type: BitMartSwapWsType) -> BitMartSwapWs { let bitmart_ws = BitMartSwapWs::new(btree_map, ws_type); bitmart_ws } /*获取服务器时间*/ #[tokio::test] async fn rest_get_server_time_test() { global::log_utils::init_log_with_trace(); proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_server_time().await; trace!("bitmart--获取服务器时间--{:?}", req_data); trace!("bitmart--获取服务器时间--{}", req_data.data); } /*深度*/ #[tokio::test] async fn rest_get_depth_test() { global::log_utils::init_log_with_trace(); proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_depth(json!({ })).await; trace!("bitmart--深度--{:?}", req_data); trace!("bitmart--深度--{}", req_data.data); } /*合约信息*/ #[tokio::test] async fn rest_get_market_test() { global::log_utils::init_log_with_trace(); proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_market(json!({ })).await; trace!("bitmart--合约信息--{:?}", req_data); trace!("bitmart--合约信息--{}", req_data.data); } /*查询合约账户*/ #[tokio::test] async fn rest_get_account_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_account(json!({ }) ).await; trace!("bitmart--查询合约账户--{:?}", req_data); trace!("bitmart--查询合约账户--{}", req_data.data); } /*用户仓位列表*/ #[tokio::test] async fn rest_get_user_position_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_user_position(json!({ })).await; trace!("bitmart--用户仓位列表--{:?}", req_data); trace!("bitmart--用户仓位列表--{}", req_data.data); } /*查询合约订单列表*/ #[tokio::test] async fn rest_get_orders_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_orders(json!({ "symbol":"BTCUSDT" })).await; trace!("bitmart--查询合约订单列表--{:?}", req_data); trace!("bitmart--查询合约订单列表--{}", req_data.data); } /*查询单个订单详情*/ #[tokio::test] async fn rest_get_order_details_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.get_order_details(json!({ "symbol":"BTCUSDT", "order_id":"123" })).await; trace!("bitmart--查询单个订单详情--{:?}", req_data); trace!("bitmart--查询单个订单详情--{}", req_data.data); } /*合约交易下单*/ #[tokio::test] async fn rest_swap_order_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.swap_order(json!({ "symbol":"SHIB-USDT", "type":"limit", "side":"limit", "leverage":"open", "direction":"buy", "price":"0.000001359", "volume":1, "lever_rate":1, })).await; trace!("bitmart--合约交易下单--{:?}", req_data); trace!("bitmart--合约交易下单--{}", req_data.data); } /*全部撤单*/ #[tokio::test] async fn rest_cancel_price_order_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.cancel_price_order(json!({ })).await; trace!("bitmart--全部撤单--{:?}", req_data); trace!("bitmart--全部撤单--{}", req_data.data); } /*撤单*/ #[tokio::test] async fn rest_cancel_order_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.cancel_order(json!({ "order_id":"123" })).await; trace!("bitmart--撤单--{:?}", req_data); trace!("bitmart--撤单--{}", req_data.data); } // /*设置持仓模式*/ // #[tokio::test] // async fn rest_setting_dual_mode_test() { // global::log_utils::init_log_with_trace(); // // proxy_handle(); // // let mut ret = get_rest(); // // let req_data = ret.setting_dual_mode(json!({ // "margin_account":"USDT", // "position_mode":"dual_side", // })).await; // trace!("bitmart--设置持仓模式--{:?}", req_data); // trace!("bitmart--设置持仓模式--{}", req_data.data); // } /*设置杠杆*/ #[tokio::test] async fn rest_setting_dual_leverage_test() { global::log_utils::init_log_with_trace(); // proxy_handle(); let mut ret = get_rest(); let req_data = ret.setting_dual_leverage(json!({ "symbol":"BTCUSDT", "open_type":"cross", })).await; trace!("bitmart--设置持仓模式--{:?}", req_data); trace!("bitmart--设置持仓模式--{}", req_data.data); } fn get_rest() -> BitMartSwapRest { let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string()); btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string()); btree_map.insert("api_memo".to_string(), API_MEMO.to_string()); let bitmart_exc = BitMartSwapRest::new(false, btree_map); bitmart_exc } // 检测是否走代理 pub fn proxy_handle() { if proxy::ParsingDetail::http_enable_proxy() { trace!("检测有代理配置,配置走代理"); } }