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::binance_swap_rest::BinanceSwapRest; use exchanges::binance_swap_ws::{BinanceSwapLogin, BinanceSwapSubscribeType, BinanceSwapWs, BinanceSwapWsType}; use exchanges::response_base::ResponseData; const ACCESS_KEY: &str = ""; const SECRET_KEY: &str = ""; //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 = BinanceSwapLogin { api_key: ACCESS_KEY.to_string(), api_secret: SECRET_KEY.to_string(), }; let t1 = tokio::spawn(async move { let mut ws = get_ws(Option::from(param), BinanceSwapWsType::Private).await; ws.set_symbols(vec!["BTC_USDT".to_string(), "ETC_USDT".to_string()]); ws.set_subscribe(vec![ // BinanceSwapSubscribeType::PuBookTicker, // BinanceSwapSubscribeType::PuAggTrade, // BinanceSwapSubscribeType::PuDepth20levels100ms, BinanceSwapSubscribeType::PrAccount, BinanceSwapSubscribeType::PrBalance, BinanceSwapSubscribeType::PrPosition ]); //链接 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; // // tokio::time::sleep(Duration::from_millis(10 * 1000)).await; // let mut rest = get_rest(); // let ttt = chrono::Utc::now().timestamp_millis() + 6000000; // let rep_data = rest.swap_order(json!({ // "symbol":"CFXUSDT", // "side":"BUY", // "positionSide":"LONG", // "type":"LIMIT", // "quantity":50, // "price":0.11, // "timeInForce":"GTD", // "goodtilldate":ttt // })).await; // trace!(?rep_data); // trace!("1111111111"); } //rest-获取服务器时间 #[tokio::test] async fn rest_get_server_time_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_server_time().await; trace!(?rep_data) } //rest-获取交易规则和交易对 #[tokio::test] async fn rest_get_exchange_info_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_exchange_info().await; trace!(?rep_data) } //--------------------------------------------------------------------- //rest-账户信息 #[tokio::test] async fn rest_get_account_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_account().await; trace!(?rep_data) } //rest-持仓模式 #[tokio::test] async fn rest_get_pos_side_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_pos_side().await; trace!(?rep_data) } //rest-更改持仓模式 #[tokio::test] async fn rest_change_pos_side_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.change_pos_side(true).await; trace!(?rep_data) } //rest-调整开仓杠杆 #[tokio::test] async fn rest_setting_dual_leverage_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.setting_dual_leverage(json!({ "symbol":"symbol", "leverage":"leverage" })).await; trace!(?rep_data) } //rest-查询订单 #[tokio::test] async fn rest_get_order_test() { global::log_utils::init_log_with_trace(); // symbol STRING YES 交易对 // orderId LONG NO 系统订单号 // origClientOrderId STRING NO 用户自定义的订单号 // recvWindow LONG NO // timestamp LONG YES let mut rest = get_rest(); let rep_data = rest.get_order(json!({ "symbol":"CFXUSDT", "orderId":"111", })).await; trace!(?rep_data) } //rest-查询当前挂单 #[tokio::test] async fn rest_get_open_order_test() { global::log_utils::init_log_with_trace(); // symbol STRING YES 交易对 // orderId LONG NO 系统订单号 // origClientOrderId STRING NO 用户自定义的订单号 // recvWindow LONG NO // timestamp LONG YES let mut rest = get_rest(); let rep_data = rest.get_open_order(json!({ "symbol":"CFXUSDT", "orderId":"111", })).await; trace!(?rep_data) } //rest-查看当前全部挂单 #[tokio::test] async fn rest_get_open_orders_test() { global::log_utils::init_log_with_trace(); // symbol STRING YES 交易对 // orderId LONG NO 系统订单号 // origClientOrderId STRING NO 用户自定义的订单号 // recvWindow LONG NO // timestamp LONG YES let mut rest = get_rest(); let rep_data = rest.get_open_orders(json!({ "symbol":"CFXUSDT", "orderId":"111", })).await; trace!(?rep_data) } //rest-查询所有订单(包括历史订单) #[tokio::test] async fn rest_get_all_orders_test() { global::log_utils::init_log_with_trace(); // symbol STRING YES 交易对 // orderId LONG NO 只返回此orderID及之后的订单,缺省返回最近的订单 // startTime LONG NO 起始时间 // endTime LONG NO 结束时间 // limit INT NO 返回的结果集数量 默认值:500 最大值:1000 // recvWindow LONG NO // timestamp LONG YES let mut rest = get_rest(); let rep_data = rest.get_all_orders(json!({ "symbol":"CFXUSDT", "limit":100 })).await; trace!(?rep_data) } //rest-下单 #[tokio::test] async fn rest_swap_order_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let ttt = chrono::Utc::now().timestamp_millis() + 6000000; let rep_data = rest.swap_order(json!({ "symbol":"CFXUSDT", "side":"BUY", "positionSide":"LONG", "type":"LIMIT", "quantity":50, "price":0.11, "timeInForce":"GTD", "goodtilldate":ttt })).await; trace!(?rep_data) } //rest-根据币对 撤销全部订单 #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn rest_cancel_order_all_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.cancel_order_all(json!({ "symbol":"CFXUSDT"})).await; trace!(?rep_data); } //rest-账户成交历史 #[tokio::test] async fn rest_get_user_trades_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_user_trades("BTCUSDT".to_string(), -1, -1, 500).await; trace!(?rep_data) } //rest-生成listenKey #[tokio::test] async fn rest_get_listen_key_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.get_listen_key(json!({ })).await; trace!(?rep_data) } //rest-生成listenKey #[tokio::test] async fn rest_up_listen_key_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.up_listen_key(json!({ })).await; trace!(?rep_data) } //rest-生成listenKey #[tokio::test] async fn rest_close_listen_key_test() { global::log_utils::init_log_with_trace(); let mut rest = get_rest(); let rep_data = rest.close_listen_key(json!({ })).await; trace!(?rep_data) } async fn get_ws(btree_map: Option, ws_type: BinanceSwapWsType) -> BinanceSwapWs { let binance_ws = BinanceSwapWs::new(false, btree_map, ws_type).await; binance_ws } fn get_rest() -> BinanceSwapRest { 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()); let ba_exc = BinanceSwapRest::new(false, btree_map); ba_exc }