|
|
@@ -0,0 +1,276 @@
|
|
|
+mod exchange_test;
|
|
|
+
|
|
|
+use std::collections::BTreeMap;
|
|
|
+use std::env;
|
|
|
+use std::io::Error;
|
|
|
+use rust_decimal_macros::dec;
|
|
|
+use tokio::sync::mpsc;
|
|
|
+use tracing::{instrument, trace};
|
|
|
+use standard::exchange::{Exchange, ExchangeEnum};
|
|
|
+use standard::{Order, OrderCommand, Platform};
|
|
|
+use crate::exchange_test::test_new_exchange;
|
|
|
+
|
|
|
+const SYMBOL: &str = "BLZ_USDT";
|
|
|
+
|
|
|
+// 测试获取Exchange实体
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_self_exchange() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_self_exchange = gate_swap_exchange.get_self_exchange();
|
|
|
+ trace!(?gate_get_self_exchange);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取交易对信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_self_symbol() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_self_symbol = gate_swap_exchange.get_self_symbol();
|
|
|
+ trace!(?gate_get_self_symbol);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取是否使用高速通道
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_self_is_colo() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_self_is_colo = gate_swap_exchange.get_self_is_colo();
|
|
|
+ trace!(?gate_get_self_is_colo);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取登录params信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_self_params() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_self_params = gate_swap_exchange.get_self_params();
|
|
|
+ trace!("gate_get_self_params={:?}",gate_get_self_params);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取Market信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_self_market() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_self_market = gate_swap_exchange.get_self_market();
|
|
|
+ trace!(?gate_get_self_market);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取请求时间信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_request_delays() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_request_delays = gate_swap_exchange.get_request_delays();
|
|
|
+ trace!(?gate_get_request_delays);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取请求平均时间信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_request_avg_delay() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_request_avg_delay = gate_swap_exchange.get_request_avg_delay();
|
|
|
+ trace!(?gate_get_request_avg_delay);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取最大请求时间信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_request_max_delay() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_request_max_delay = gate_swap_exchange.get_request_max_delay();
|
|
|
+ trace!(?gate_get_request_max_delay);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取服务器时间
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_server_time() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_server_time = gate_swap_exchange.get_server_time().await;
|
|
|
+ trace!(?gate_get_server_time);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取账号信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_account() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_account = gate_swap_exchange.get_account().await;
|
|
|
+ trace!(?gate_get_account);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取持仓信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_position() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_position = gate_swap_exchange.get_position().await;
|
|
|
+ trace!(?gate_get_position);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取所有持仓信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_positions() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_positions = gate_swap_exchange.get_positions().await;
|
|
|
+ trace!(?gate_get_positions);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取Ticker信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_ticker() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_ticker = gate_swap_exchange.get_ticker().await;
|
|
|
+ trace!(?gate_get_ticker);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取Market信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_market() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_market = gate_swap_exchange.get_market().await;
|
|
|
+ trace!(?gate_get_market);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取Order详情信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_order_detail() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_order_detail = gate_swap_exchange.get_order_detail("", "999999").await;
|
|
|
+ trace!(?gate_get_order_detail);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试获取Order列表信息
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_get_orders_list() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_get_orders_list = gate_swap_exchange.get_orders_list("finished").await;
|
|
|
+ trace!(?gate_get_orders_list);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试下单
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_take_order() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_take_order = gate_swap_exchange.take_order("999999", "kk", dec!(0.27), dec!(100)).await;
|
|
|
+ trace!(?gate_take_order);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试撤销订单
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_cancel_order() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_cancel_order = gate_swap_exchange.cancel_order("", "999999").await;
|
|
|
+ trace!(?gate_cancel_order);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试批量撤销订单
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_cancel_orders() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_cancel_orders = gate_swap_exchange.cancel_orders().await;
|
|
|
+ trace!(?gate_cancel_orders);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试设置持仓模式
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_set_dual_mode() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_set_dual_mode = gate_swap_exchange.set_dual_mode("usdt", true).await;
|
|
|
+ trace!(?gate_set_dual_mode);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试设置杠杆
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_set_dual_leverage() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = test_new_exchange(ExchangeEnum::GateSwap, SYMBOL).await;
|
|
|
+ let gate_set_dual_leverage = gate_swap_exchange.set_dual_leverage("10").await;
|
|
|
+ trace!(?gate_set_dual_leverage);
|
|
|
+}
|
|
|
+
|
|
|
+// 测试指令下单
|
|
|
+#[tokio::test]
|
|
|
+#[instrument(level = "TRACE")]
|
|
|
+async fn test_command_order() {
|
|
|
+ global::log_utils::init_log_with_trace();
|
|
|
+ let (order_sender, mut order_receiver): (mpsc::Sender<Order>, mpsc::Receiver<Order>) = mpsc::channel(1024);
|
|
|
+ let (error_sender, mut error_receiver): (mpsc::Sender<Error>, mpsc::Receiver<Error>) = mpsc::channel(1024);
|
|
|
+
|
|
|
+ let mut params: BTreeMap<String, String> = BTreeMap::new();
|
|
|
+ let access_key = env::var("gate_access_key").unwrap_or("".to_string());
|
|
|
+ let secret_key = env::var("gate_secret_key").unwrap_or("".to_string());
|
|
|
+ params.insert("access_key".to_string(), access_key);
|
|
|
+ params.insert("secret_key".to_string(), secret_key);
|
|
|
+
|
|
|
+ let mut gate_swap_exchange: Box<dyn Platform> = Exchange::new(ExchangeEnum::GateSwap, SYMBOL.to_string(), false, params, order_sender, error_sender).await;
|
|
|
+
|
|
|
+ let mut command = OrderCommand::new();
|
|
|
+ command.cancel.insert("888888".to_string(), vec!["888888".to_string(), "".to_string()]);
|
|
|
+ command.limits_open.insert("888888".to_string(), vec!["100".to_string(), "kd".to_string(), "0.18".to_string(), "888888".to_string()]);
|
|
|
+ command.limits_close.insert("999999".to_string(), vec!["100".to_string(), "kk".to_string(), "0.25".to_string(), "999999".to_string()]);
|
|
|
+ command.check.insert("888888".to_string(), vec!["999999".to_string(), "".to_string()]);
|
|
|
+ gate_swap_exchange.command_order(command).await;
|
|
|
+
|
|
|
+
|
|
|
+ if let Ok(order) = order_receiver.try_recv() {
|
|
|
+ trace!(?order);
|
|
|
+ }
|
|
|
+ if let Ok(error) = error_receiver.try_recv() {
|
|
|
+ trace!(?error);
|
|
|
+ }
|
|
|
+}
|