|
|
@@ -4,6 +4,9 @@ use ring::{digest};
|
|
|
use hex;
|
|
|
use hmac::{Hmac, Mac, NewMac};
|
|
|
use reqwest::Client;
|
|
|
+use rust_decimal::Decimal;
|
|
|
+use rust_decimal::prelude::FromPrimitive;
|
|
|
+use rust_decimal_macros::dec;
|
|
|
use crate::http_tool::RestTool;
|
|
|
use crate::response_base::ResponseData;
|
|
|
use sha2::Sha512;
|
|
|
@@ -11,14 +14,15 @@ use tracing::trace;
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
pub struct GateSwapRest {
|
|
|
- pub label: String,
|
|
|
+ label: String,
|
|
|
base_url: String,
|
|
|
client: reqwest::Client,
|
|
|
/*******参数*/
|
|
|
//登陆所需参数
|
|
|
login_param: BTreeMap<String, String>,
|
|
|
- response_time: i64,
|
|
|
-
|
|
|
+ delays: Vec<i64>,
|
|
|
+ max_delay: i64,
|
|
|
+ avg_delay: Decimal,
|
|
|
}
|
|
|
|
|
|
impl GateSwapRest {
|
|
|
@@ -44,7 +48,9 @@ impl GateSwapRest {
|
|
|
base_url: base_url.to_string(),
|
|
|
client: Client::new(),
|
|
|
login_param,
|
|
|
- response_time: 0,
|
|
|
+ delays: vec![],
|
|
|
+ max_delay: 0,
|
|
|
+ avg_delay: dec!(0.0),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -52,7 +58,7 @@ impl GateSwapRest {
|
|
|
/*****************************************rest请求函数********************************************************/
|
|
|
/*******************************************************************************************************/
|
|
|
//获取服务器当前时间
|
|
|
- pub async fn get_server_time(&self) -> ResponseData {
|
|
|
+ pub async fn get_server_time(&mut self) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -64,7 +70,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//查询个人交易费率
|
|
|
- pub async fn wallet_fee(&self) -> ResponseData {
|
|
|
+ pub async fn wallet_fee(&mut self) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -76,7 +82,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//查询合约账户
|
|
|
- pub async fn get_account(&self, settle: String) -> ResponseData {
|
|
|
+ pub async fn get_account(&mut self, settle: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -88,7 +94,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//用户仓位列表
|
|
|
- pub async fn get_user_position(&self, settle: String) -> ResponseData {
|
|
|
+ pub async fn get_user_position(&mut self, settle: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -100,7 +106,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//双仓模式下的持仓信息
|
|
|
- pub async fn get_position(&self, settle: String, contract: String) -> ResponseData {
|
|
|
+ pub async fn get_position(&mut self, settle: String, contract: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -112,7 +118,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//获取所有合约交易行情统计
|
|
|
- pub async fn get_ticker(&self, settle: String) -> ResponseData {
|
|
|
+ pub async fn get_ticker(&mut self, settle: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -124,7 +130,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//查询所有的合约信息
|
|
|
- pub async fn get_market_details(&self, settle: String) -> ResponseData {
|
|
|
+ pub async fn get_market_details(&mut self, settle: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -136,7 +142,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//查询单个订单详情
|
|
|
- pub async fn get_order_details(&self, settle: String, order_id: String) -> ResponseData {
|
|
|
+ pub async fn get_order_details(&mut self, settle: String, order_id: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
let data = self.request("GET".to_string(),
|
|
|
@@ -148,7 +154,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//查询合约订单列表
|
|
|
- pub async fn get_orders(&self, settle: String, status: String) -> ResponseData {
|
|
|
+ pub async fn get_orders(&mut self, settle: String, status: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
"status":status
|
|
|
});
|
|
|
@@ -162,7 +168,7 @@ impl GateSwapRest {
|
|
|
}
|
|
|
|
|
|
//下单:side-下单方向,pos_side-持仓方向
|
|
|
- pub async fn order(&self,
|
|
|
+ pub async fn order(&mut self,
|
|
|
settle: String,
|
|
|
pos_side: String,
|
|
|
side: String,
|
|
|
@@ -209,7 +215,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//合约交易下单
|
|
|
- pub async fn swap_order(&self, settle: String, params: serde_json::Value) -> ResponseData {
|
|
|
+ pub async fn swap_order(&mut self, settle: String, params: serde_json::Value) -> ResponseData {
|
|
|
let data = self.request("POST".to_string(),
|
|
|
"/api/v4".to_string(),
|
|
|
format!("/futures/{}/orders", settle),
|
|
|
@@ -219,7 +225,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//设置持仓模式
|
|
|
- pub async fn setting_dual_mode(&self, settle: String, dual_mode: bool) -> ResponseData {
|
|
|
+ pub async fn setting_dual_mode(&mut self, settle: String, dual_mode: bool) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
"dual_mode":dual_mode,
|
|
|
});
|
|
|
@@ -232,7 +238,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//更新双仓模式下的杠杆
|
|
|
- pub async fn setting_dual_leverage(&self, settle: String, symbol: String, leverage: String) -> ResponseData {
|
|
|
+ pub async fn setting_dual_leverage(&mut self, settle: String, symbol: String, leverage: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
"leverage":leverage,
|
|
|
});
|
|
|
@@ -245,7 +251,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//交易账户互转
|
|
|
- pub async fn wallet_transfers(&self, currency: String, from: String, to: String, amount: String, settle: String) -> ResponseData {
|
|
|
+ pub async fn wallet_transfers(&mut self, currency: String, from: String, to: String, amount: String, settle: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
"currency":currency,
|
|
|
"from":from,
|
|
|
@@ -262,7 +268,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//撤销单个订单
|
|
|
- pub async fn cancel_order(&self, settle: String, order_id: String) -> ResponseData {
|
|
|
+ pub async fn cancel_order(&mut self, settle: String, order_id: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
});
|
|
|
|
|
|
@@ -275,7 +281,7 @@ impl GateSwapRest {
|
|
|
data
|
|
|
}
|
|
|
//弃用
|
|
|
- pub async fn swap_bazaar_order(&self, text: String, origin_side: String, settle: String, contract: String, size: i64) -> ResponseData {
|
|
|
+ pub async fn swap_bazaar_order(&mut self, text: String, origin_side: String, settle: String, contract: String, size: i64) -> ResponseData {
|
|
|
let mut reduce_only = false;
|
|
|
let mut param = serde_json::json!({
|
|
|
"text":text,
|
|
|
@@ -312,7 +318,7 @@ impl GateSwapRest {
|
|
|
}
|
|
|
|
|
|
//批量取消状态为 open 的订单
|
|
|
- pub async fn cancel_orders(&self, settle: String, contract: String) -> ResponseData {
|
|
|
+ pub async fn cancel_orders(&mut self, settle: String, contract: String) -> ResponseData {
|
|
|
let params = serde_json::json!({
|
|
|
"contract":contract
|
|
|
});
|
|
|
@@ -330,11 +336,33 @@ impl GateSwapRest {
|
|
|
/*******************************************************************************************************/
|
|
|
/*****************************************工具函数********************************************************/
|
|
|
/*******************************************************************************************************/
|
|
|
- fn get_response_time(&self) -> i64 {
|
|
|
- self.response_time
|
|
|
+ pub fn get_delays(&self) -> Vec<i64> {
|
|
|
+ self.delays.clone()
|
|
|
+ }
|
|
|
+ pub fn get_avg_delay(&self) -> Decimal {
|
|
|
+ self.avg_delay
|
|
|
+ }
|
|
|
+ fn get_delay_info(&mut self) {
|
|
|
+ let last_100 = if self.delays.len() > 100 {
|
|
|
+ self.delays[self.delays.len() - 100..].to_vec()
|
|
|
+ } else {
|
|
|
+ self.delays.clone()
|
|
|
+ };
|
|
|
+
|
|
|
+ let max_value = last_100.iter().max().unwrap();
|
|
|
+ if max_value.clone().to_owned() > self.max_delay {
|
|
|
+ self.max_delay = max_value.clone().to_owned();
|
|
|
+ }
|
|
|
+
|
|
|
+ let sum: i64 = last_100.iter().sum();
|
|
|
+ let sum_v = Decimal::from_i64(sum).unwrap();
|
|
|
+ let len_v = Decimal::from_u64(last_100.len() as u64).unwrap();
|
|
|
+ self.avg_delay = (sum_v / len_v).round_dp(1);
|
|
|
+ self.delays = last_100.clone().into_iter().collect();
|
|
|
}
|
|
|
+
|
|
|
//调用请求
|
|
|
- async fn request(&self,
|
|
|
+ async fn request(&mut self,
|
|
|
requesst_type: String,
|
|
|
prefix_url: String,
|
|
|
request_url: String,
|
|
|
@@ -446,7 +474,7 @@ impl GateSwapRest {
|
|
|
}
|
|
|
|
|
|
|
|
|
- async fn http_toll(&self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
|
|
|
+ async fn http_toll(&mut self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
|
|
|
let res_data: ResponseData;
|
|
|
/****请求接口与 地址*/
|
|
|
let url = format!("{}{}", self.base_url.to_string(), request_path);
|
|
|
@@ -458,6 +486,8 @@ impl GateSwapRest {
|
|
|
// trace!("params_json:{}",params_json);
|
|
|
// trace!("headers:{:?}",headers);
|
|
|
|
|
|
+
|
|
|
+ let start_time = chrono::Utc::now().timestamp_millis();
|
|
|
let req = match request_type.as_str() {
|
|
|
"GET" => self.client.get(addrs_url.clone()).headers(headers),
|
|
|
"POST" => self.client.post(addrs_url.clone()).body(params).headers(headers),
|
|
|
@@ -477,6 +507,9 @@ impl GateSwapRest {
|
|
|
// trace!("error-----{}", body);
|
|
|
res_data = ResponseData::error(self.label.clone(), body.to_string())
|
|
|
}
|
|
|
+ let time_array = chrono::Utc::now().timestamp_millis() - start_time;
|
|
|
+ self.delays.push(time_array);
|
|
|
+ self.get_delay_info();
|
|
|
Ok(res_data)
|
|
|
}
|
|
|
|