| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- use std::borrow::Cow;
- use std::collections::BTreeMap;
- use std::sync::Arc;
- use std::sync::atomic::AtomicBool;
- use std::time::Duration;
- use chrono::Utc;
- use futures_util::StreamExt;
- use serde_json::json;
- use tokio::sync::Mutex;
- use tokio_tungstenite::tungstenite::Message;
- use tokio_tungstenite::tungstenite::protocol::CloseFrame;
- use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
- use tracing::{error, info, trace};
- use exchanges::gate_swap_rest::GateSwapRest;
- use exchanges::gate_swap_ws::{GateSwapLogin, GateSwapSubscribeType, GateSwapWs, GateSwapWsType};
- use exchanges::response_base::ResponseData;
- use exchanges::socket_tool::{AbstractWsMode, HeartbeatType};
- 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::<ResponseData>();
- 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 param = GateSwapLogin {
- api_key: "".to_string(),
- secret: "".to_string(),
- };
- let mut ws = get_ws(Option::from(param));
- ws.set_symbols(vec!["BTC_USDT".to_string()]);
- ws.set_subscribe(vec![
- // GateSwapSubscribeType::PuFuturesTrades,
- ]);
- //写数据
- // 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_restart = tokio::spawn(async move {
- // 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));
- //
- // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await;
- // }
- // });
- // let write_tx_clone2222 = Arc::clone(&write_tx_am);
- // let tw_ping = tokio::spawn(async move {
- // trace!("线程-数据写入-开始");
- // loop {
- // tokio::time::sleep(Duration::from_millis(1 * 1000)).await;
- // let timestamp = Utc::now().timestamp();
- // let ping_str = json!({
- // "time" : timestamp,
- // "channel" : "futures.ping",
- // });
- // write_tx_clone2222.lock().await.unbounded_send(Message::Text(ping_str.to_string())).expect("发送失败1");
- // // AbstractWsMode::ping_or_pong(write_tx_clone.clone(), HeartbeatType::Custom(ping_str.to_string()), 1000).await;
- // trace!("---------tw_ping");
- // }
- // trace!("线程-数据写入-结束");
- // });
- // let write_tx_clone3333333 = Arc::clone(&write_tx_am);
- // let tw_close = tokio::spawn(async move {
- // loop {
- // tokio::time::sleep(Duration::from_millis(3 * 1000)).await;
- // write_tx_clone3333333.lock().await.close_channel();
- // trace!("---------tw_close");
- // }
- // });
- let fun = move |data: ResponseData| {
- async move {
- trace!("---传入的方法~~~~{:?}", data);
- }
- };
- let t1 = tokio::spawn(async move {
- //链接
- 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;
- }
- //ws-订阅公共频道信息
- #[tokio::test(flavor = "multi_thread", worker_threads = 3)]
- async fn ws_custom_subscribe2() {
- global::log_utils::init_log_with_trace();
- let (write_tx, mut write_rx) = futures_channel::mpsc::unbounded();
- let write_tx_am = Arc::new(Mutex::new(write_tx));
- let write_tx_clone = Arc::clone(&write_tx_am);
- let write_tx_clone2 = Arc::clone(&write_tx_am);
- let tw = tokio::spawn(async move {
- loop {
- tokio::time::sleep(Duration::from_millis(2 * 1000)).await;
- let _ = write_tx_clone.lock().await.unbounded_send(Message::Ping(Vec::from("Ping"))).expect("发送失败!!!");
- }
- });
- let tw2 = tokio::spawn(async move {
- loop {
- tokio::time::sleep(Duration::from_millis(10 * 1000)).await;
- let _ = write_tx_clone2.lock().await.close_channel();
- }
- });
- let t2 = tokio::spawn(async move {
- let mut z = 0;
- while let Some(message) = write_rx.next().await {
- trace!("输出:{:?}",message);
- z = z + 1;
- if z > 3 {
- write_rx.close();
- }
- }
- });
- tokio::try_join!(t2,tw).unwrap();
- }
- //rest-设置持仓模式
- #[tokio::test]
- async fn rest_cancel_order_all_test() {
- global::log_utils::init_log_with_trace();
- let mut ret = get_rest();
- let req_data = ret.cancel_order_all().await;
- println!("okx--设置持仓模式--{:?}", req_data);
- }
- //rest-下一个自动单
- #[tokio::test]
- async fn price_order_test() {
- global::log_utils::init_log_with_info();
- // let mut rest = get_rest();
- // let mut params = json!({});
- //
- // params["initial"] = json!({
- // "contract": "XRP_USDT",
- // "price": "0",
- // "tif": "ioc",
- // "reduce_only": true,
- // // [平多:close_long, 平空:close_short]
- // "auto_size": "close_long"
- // });
- //
- // params["trigger"] = json!({
- // // [平多:close-long-position, 平空:close-short-position]
- // "order_type": "close-long-position",
- // // 一般都默认用0
- // "strategy_type": 0,
- // // [0 - 最新成交价,1 - 标记价格,2 - 指数价格]
- // "price_type": 0,
- // // [1: 引用价格大于等于我们传的价格,2:引用价格小于等于我们传的价格]
- // // 在止损的情况下:
- // // 1 可以理解为向上突破触发价(一般是给空单用)
- // // 2 可以理解为向下突破触发价(一般是给多单用)
- // "rule": 2,
- // // 订单触发价格
- // "price": "0.5600",
- // });
- //
- // let response_data = rest.place_price_order("usdt".to_string(), params).await;
- // if response_data.code == "200" {
- // let response_obj: serde_json::Value = serde_json::from_str(response_data.data.as_str()).unwrap();
- //
- // info!("resp={:?}", response_obj.as_object().unwrap());
- // } else {
- // error!(?response_data);
- // }
- }
- #[tokio::test]
- async fn price_order_cancel_test() {
- global::log_utils::init_log_with_info();
- let mut rest = get_rest();
- // 这边取消订单只能使用系统返回的
- let rst = rest.cancel_price_order("usdt".to_string(), "58002898".to_string()).await;
- info!(?rst);
- }
- //rest-查询合约账户变更历史
- #[tokio::test]
- async fn rest_account_book_test() {
- global::log_utils::init_log_with_trace();
- let mut ret = get_rest();
- let req_data = ret.account_book("usdt".to_string()).await;
- println!("okx--查询合约账户变更历史--{:?}", req_data);
- }
- fn get_ws(btree_map: Option<GateSwapLogin>) -> GateSwapWs {
- let binance_ws = GateSwapWs::new(false,
- btree_map,
- GateSwapWsType::PublicAndPrivate("usdt".to_string()));
- binance_ws
- }
- fn get_rest() -> GateSwapRest {
- let mut btree_map: BTreeMap<String, String> = 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 = GateSwapRest::new(false, btree_map);
- ba_exc
- }
|