mod exchange_test; use std::collections::BTreeMap; use std::env; use std::io::Error; use chrono::Utc; use rust_decimal_macros::dec; use tokio::sync::mpsc; use tokio::time::Instant; use tracing::{instrument, trace}; use global::trace_stack::TraceStack; use standard::exchange::{Exchange, ExchangeEnum}; use standard::{Order, OrderCommand, Platform, utils}; use crate::exchange_test::{test_new_exchange}; const SYMBOL: &str = "USTC_USDT"; // 测试获取Exchange实体 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_self_exchange() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_self_exchange = htx_swap_exchange.get_self_exchange(); trace!(?htx_get_self_exchange); } // 测试获取交易对信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_self_symbol() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_self_symbol = htx_swap_exchange.get_self_symbol(); trace!(?htx_get_self_symbol); } // 测试获取是否使用高速通道 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_self_is_colo() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_self_is_colo = htx_swap_exchange.get_self_is_colo(); trace!(?htx_get_self_is_colo); } // 测试获取登录params信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_self_params() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_self_params = htx_swap_exchange.get_self_params(); trace!("htx_get_self_params={:?}",htx_get_self_params); } // 测试获取Market信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_self_market() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_self_market = htx_swap_exchange.get_self_market(); trace!(?htx_get_self_market); } // 测试获取请求时间信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_request_delays() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_request_delays = htx_swap_exchange.get_request_delays(); trace!(?htx_get_request_delays); } // 测试获取请求平均时间信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_request_avg_delay() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_request_avg_delay = htx_swap_exchange.get_request_avg_delay(); trace!(?htx_get_request_avg_delay); } // 测试获取最大请求时间信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_request_max_delay() { global::log_utils::init_log_with_trace(); let htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_request_max_delay = htx_swap_exchange.get_request_max_delay(); trace!(?htx_get_request_max_delay); } // 测试获取服务器时间 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_server_time() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_server_time = htx_swap_exchange.get_server_time().await; trace!(?htx_get_server_time); } // 测试获取账号信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_account() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_account = htx_swap_exchange.get_account().await; trace!(?htx_get_account); } // 测试获取持仓信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_position() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_position = htx_swap_exchange.get_position().await; trace!(?htx_get_position); } // 测试获取所有持仓信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_positions() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_positions = htx_swap_exchange.get_positions().await; trace!(?htx_get_positions); } // 测试获取Ticker信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_ticker() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_ticker = htx_swap_exchange.get_ticker().await; trace!(?htx_get_ticker); } // 测试获取Market信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_market() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_market = htx_swap_exchange.get_market().await; trace!(?htx_get_market); } // 测试获取Order详情信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_order_detail() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_order_detail = htx_swap_exchange.get_order_detail("1234925996822429696", "9999992").await; trace!(?htx_get_order_detail); } // 测试获取Order列表信息 #[tokio::test] #[instrument(level = "TRACE")] async fn test_get_orders_list() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_get_orders_list = htx_swap_exchange.get_orders_list("finished").await; trace!(?htx_get_orders_list); } // 测试下单 #[tokio::test] #[instrument(level = "TRACE")] async fn test_take_order() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_take_order = htx_swap_exchange.take_order("999999914", "kd", dec!(0.02320), dec!(20)).await; trace!(?htx_take_order); } // 测试撤销订单 #[tokio::test] #[instrument(level = "TRACE")] async fn test_cancel_order() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_cancel_order = htx_swap_exchange.cancel_order("", "999999900").await; trace!(?htx_cancel_order); } // 测试批量撤销订单 #[tokio::test] #[instrument(level = "TRACE")] async fn test_cancel_orders() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_cancel_orders = htx_swap_exchange.cancel_orders_all().await; trace!(?htx_cancel_orders); } // 测试设置持仓模式 #[tokio::test] #[instrument(level = "TRACE")] async fn test_set_dual_mode() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_set_dual_mode = htx_swap_exchange.set_dual_mode("usdt", true).await; trace!(?htx_set_dual_mode); } // 测试设置杠杆 #[tokio::test] #[instrument(level = "TRACE")] async fn test_set_dual_leverage() { global::log_utils::init_log_with_trace(); let mut htx_swap_exchange: Box = test_new_exchange(ExchangeEnum::HtxSwap, SYMBOL).await; let htx_set_dual_leverage = htx_swap_exchange.set_dual_leverage("10").await; trace!(?htx_set_dual_leverage); } // 测试指令下单 #[tokio::test] #[instrument(level = "TRACE")] async fn test_command_order() { global::log_utils::init_log_with_trace(); utils::proxy_handle(); let (order_sender, mut order_receiver): (mpsc::Sender, mpsc::Receiver) = mpsc::channel(1024); let (error_sender, mut error_receiver): (mpsc::Sender, mpsc::Receiver) = mpsc::channel(1024); let mut params: BTreeMap = BTreeMap::new(); let access_key = env::var("htx_access_key").unwrap_or("".to_string()); let secret_key = env::var("htx_secret_key").unwrap_or("".to_string()); params.insert("access_key".to_string(), access_key); params.insert("secret_key".to_string(), secret_key); let mut htx_swap_exchange: Box = Exchange::new(ExchangeEnum::HtxSwap, 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()]); htx_swap_exchange.command_order(&mut command, &TraceStack::new(Utc::now().timestamp_micros(), Instant::now())).await; loop { if let Ok(order) = order_receiver.try_recv() { trace!(?order); } if let Ok(error) = error_receiver.try_recv() { trace!(?error); } } }