gate_swap_test.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use std::collections::BTreeMap;
  2. use std::sync::Arc;
  3. use std::sync::atomic::AtomicBool;
  4. use tokio::sync::mpsc::{channel, Sender};
  5. use tokio::try_join;
  6. use tracing::trace;
  7. use exchanges::binance_swap_rest::BinanceSwapRest;
  8. use exchanges::binance_swap_ws::{BinanceSubscribeType, BinanceSwapWs, BinanceWsType};
  9. use exchanges::gate_swap_ws::{GateSubscribeType, GateSwapWs, GateWsType};
  10. use exchanges::proxy;
  11. use exchanges::response_base::ResponseData;
  12. const ACCESS_KEY: &str = "";
  13. const SECRET_KEY: &str = "";
  14. //ws-订阅公共频道信息
  15. #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
  16. async fn ws_custom_subscribe() {
  17. // global::log_utils::init_log_with_trace();
  18. //
  19. // let mut bool_v1 = Arc::new(AtomicBool::new(true));
  20. // let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
  21. // btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
  22. // btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
  23. // let (tx, mut rx) = channel(1024);
  24. // let mut ws = get_ws(btree_map, tx);
  25. // ws.set_subscribe(vec![
  26. // GateSubscribeType::PuFuturesCandlesticks,
  27. // GateSubscribeType::PuFuturesTrades,
  28. // GateSubscribeType::PuFuturesOrderBook,
  29. //
  30. // GateSubscribeType::PrFuturesPositions("".to_string()),
  31. // ]);
  32. // let t1 = tokio::spawn(async move {
  33. // ws.custom_subscribe(bool_v1, vec!["BTC_USDT".to_string()]).await;
  34. // });
  35. // let t2 = tokio::spawn(async move {
  36. // loop {
  37. // if let Ok(received) = rx.try_recv() {
  38. // trace!( "age: {:?}", received);
  39. // }
  40. // }
  41. // });
  42. // try_join!(t1,t2).unwrap();
  43. let json_str = r#"{"name": "Alice", "age": 30}"#;
  44. // 解析 JSON 字符串并将其转换为一个 Rust 结构体
  45. let parsed_data: Result<serde_json::Value, serde_json::Error> = serde_json::from_str(json_str);
  46. match parsed_data {
  47. Ok(data) => {
  48. println!("Name: {}", data["name"]);
  49. println!("Age: {}", data["age"]);
  50. }
  51. Err(e) => {
  52. println!("Failed to parse JSON: {}", e);
  53. }
  54. }
  55. }
  56. fn get_ws(btree_map: BTreeMap<String, String>, tx: Sender<ResponseData>) -> GateSwapWs {
  57. let mut binance_ws = GateSwapWs::new(false,
  58. btree_map,
  59. GateWsType::PublicAndPrivate("usdt".to_string()),
  60. tx);
  61. binance_ws
  62. }
  63. fn get_rest() -> BinanceSwapRest {
  64. let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
  65. btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
  66. btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
  67. let mut ba_exc = BinanceSwapRest::new(false, btree_map);
  68. ba_exc
  69. }