|
@@ -54,13 +54,12 @@ impl CoinexSwapRest {
|
|
|
/*******************************************************************************************************/
|
|
/*******************************************************************************************************/
|
|
|
//获取服务器当前时间
|
|
//获取服务器当前时间
|
|
|
pub async fn get_server_time(&mut self) -> ResponseData {
|
|
pub async fn get_server_time(&mut self) -> ResponseData {
|
|
|
-
|
|
|
|
|
let data = self.request("GET".to_string(),
|
|
let data = self.request("GET".to_string(),
|
|
|
"/v2".to_string(),
|
|
"/v2".to_string(),
|
|
|
"/time".to_string(),
|
|
"/time".to_string(),
|
|
|
false,
|
|
false,
|
|
|
None,
|
|
None,
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -71,7 +70,7 @@ impl CoinexSwapRest {
|
|
|
"/account/trade-fee-rate".to_string(),
|
|
"/account/trade-fee-rate".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -82,7 +81,7 @@ impl CoinexSwapRest {
|
|
|
"/assets/futures/balance".to_string(),
|
|
"/assets/futures/balance".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -94,7 +93,7 @@ impl CoinexSwapRest {
|
|
|
"/assets/spot/balance".to_string(),
|
|
"/assets/spot/balance".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -110,7 +109,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/pending-position".to_string(),
|
|
"/futures/pending-position".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -125,7 +124,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/pending-position".to_string(),
|
|
"/futures/pending-position".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -140,7 +139,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/ticker".to_string(),
|
|
"/futures/ticker".to_string(),
|
|
|
false,
|
|
false,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -154,7 +153,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/market".to_string(),
|
|
"/futures/market".to_string(),
|
|
|
false,
|
|
false,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -169,7 +168,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/order-status".to_string(),
|
|
"/futures/order-status".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -185,7 +184,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/pending-order".to_string(),
|
|
"/futures/pending-order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -200,7 +199,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/pending-order".to_string(),
|
|
"/futures/pending-order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -215,7 +214,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/finished-order".to_string(),
|
|
"/futures/finished-order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
Some(params.to_string()),
|
|
Some(params.to_string()),
|
|
|
- None
|
|
|
|
|
|
|
+ None,
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -228,7 +227,7 @@ impl CoinexSwapRest {
|
|
|
side: String,
|
|
side: String,
|
|
|
size: Decimal,
|
|
size: Decimal,
|
|
|
price: Decimal,
|
|
price: Decimal,
|
|
|
- client_id: String
|
|
|
|
|
|
|
+ client_id: String,
|
|
|
) -> ResponseData
|
|
) -> ResponseData
|
|
|
{
|
|
{
|
|
|
// 默认为限价单
|
|
// 默认为限价单
|
|
@@ -245,7 +244,7 @@ impl CoinexSwapRest {
|
|
|
data = self.swap_order(market, side, type_y, size, price, client_id, false).await;
|
|
data = self.swap_order(market, side, type_y, size, price, client_id, false).await;
|
|
|
}
|
|
}
|
|
|
"long_sell" => {//平多
|
|
"long_sell" => {//平多
|
|
|
- data = self.close_position(market, type_y, price, client_id, false).await;
|
|
|
|
|
|
|
+ data = self.close_position(market, type_y, price, client_id, false).await;
|
|
|
}
|
|
}
|
|
|
"short_buy" => {//平空
|
|
"short_buy" => {//平空
|
|
|
data = self.close_position(market, type_y, price, client_id, false).await;
|
|
data = self.close_position(market, type_y, price, client_id, false).await;
|
|
@@ -253,7 +252,8 @@ impl CoinexSwapRest {
|
|
|
"short_sell" => {//开空
|
|
"short_sell" => {//开空
|
|
|
data = self.swap_order(market, side, type_y, size, price, client_id, false).await;
|
|
data = self.swap_order(market, side, type_y, size, price, client_id, false).await;
|
|
|
}
|
|
}
|
|
|
- _ => {// 处理未知请求类型
|
|
|
|
|
|
|
+ _ => {
|
|
|
|
|
+ // 处理未知请求类型
|
|
|
error!("下单失败,数量异常! size: {}", size);
|
|
error!("下单失败,数量异常! size: {}", size);
|
|
|
data = ResponseData::error(self.label.clone(), format!("下单失败, 下单参数: <market: {:?}, pos_side: {:?}, side: {:?}, size: {}, price: {:?}, client_id: {:?}>", market, pos_side, side, size, price, client_id));
|
|
data = ResponseData::error(self.label.clone(), format!("下单失败, 下单参数: <market: {:?}, pos_side: {:?}, side: {:?}, size: {}, price: {:?}, client_id: {:?}>", market, pos_side, side, size, price, client_id));
|
|
|
}
|
|
}
|
|
@@ -262,7 +262,7 @@ impl CoinexSwapRest {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 平仓下单
|
|
// 平仓下单
|
|
|
- pub async fn close_position(&mut self, market: String, type_y : String, price: Decimal, client_id: String, is_hide: bool) -> ResponseData {
|
|
|
|
|
|
|
+ pub async fn close_position(&mut self, market: String, type_y: String, price: Decimal, client_id: String, is_hide: bool) -> ResponseData {
|
|
|
// 数量不传为全平
|
|
// 数量不传为全平
|
|
|
let param = serde_json::json!({
|
|
let param = serde_json::json!({
|
|
|
"market":market,
|
|
"market":market,
|
|
@@ -278,13 +278,13 @@ impl CoinexSwapRest {
|
|
|
"/futures/close-position".to_string(),
|
|
"/futures/close-position".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- Some(param.to_string())
|
|
|
|
|
|
|
+ Some(param.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//合约交易开仓下单
|
|
//合约交易开仓下单
|
|
|
- pub async fn swap_order(&mut self, market: String, side: String, type_y : String, amount: Decimal, price: Decimal, client_id: String, is_hide: bool) -> ResponseData {
|
|
|
|
|
|
|
+ pub async fn swap_order(&mut self, market: String, side: String, type_y: String, amount: Decimal, price: Decimal, client_id: String, is_hide: bool) -> ResponseData {
|
|
|
let param = serde_json::json!({
|
|
let param = serde_json::json!({
|
|
|
"market":market,
|
|
"market":market,
|
|
|
"market_type": "FUTURES",
|
|
"market_type": "FUTURES",
|
|
@@ -301,7 +301,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/order".to_string(),
|
|
"/futures/order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- Some(param.to_string())
|
|
|
|
|
|
|
+ Some(param.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -325,7 +325,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/adjust-position-leverage".to_string(),
|
|
"/futures/adjust-position-leverage".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- Some(params.to_string())
|
|
|
|
|
|
|
+ Some(params.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -344,7 +344,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/cancel-order".to_string(),
|
|
"/futures/cancel-order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- Some(params.to_string())
|
|
|
|
|
|
|
+ Some(params.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
} else if client_id != "" { // 如果客户端id不为空,则用客户端id取消订单
|
|
} else if client_id != "" { // 如果客户端id不为空,则用客户端id取消订单
|
|
@@ -355,18 +355,19 @@ impl CoinexSwapRest {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
let mut data = self.request("POST".to_string(),
|
|
let mut data = self.request("POST".to_string(),
|
|
|
- "/v2".to_string(),
|
|
|
|
|
- "/futures/cancel-order-by-client-id".to_string(),
|
|
|
|
|
- true,
|
|
|
|
|
- None,
|
|
|
|
|
- Some(params.to_string())
|
|
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/futures/cancel-order-by-client-id".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ None,
|
|
|
|
|
+ Some(params.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
// 非空的
|
|
// 非空的
|
|
|
if data.code == 200 && !data.data.is_null() {
|
|
if data.code == 200 && !data.data.is_null() {
|
|
|
data.data = data.data.as_array().unwrap()[0]["data"].clone();
|
|
data.data = data.data.as_array().unwrap()[0]["data"].clone();
|
|
|
}
|
|
}
|
|
|
data
|
|
data
|
|
|
- } else { // 否则返回错误
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 否则返回错误
|
|
|
error!("取消订单失败失败,id异常");
|
|
error!("取消订单失败失败,id异常");
|
|
|
ResponseData::error(self.label.clone(), format!("取消订单失败失败, orderId:{:?}, clientId: {:?} ", order_id, client_id))
|
|
ResponseData::error(self.label.clone(), format!("取消订单失败失败, orderId:{:?}, clientId: {:?} ", order_id, client_id))
|
|
|
}
|
|
}
|
|
@@ -383,7 +384,7 @@ impl CoinexSwapRest {
|
|
|
"/futures/cancel-all-order".to_string(),
|
|
"/futures/cancel-all-order".to_string(),
|
|
|
true,
|
|
true,
|
|
|
None,
|
|
None,
|
|
|
- Some(params.to_string())
|
|
|
|
|
|
|
+ Some(params.to_string()),
|
|
|
).await;
|
|
).await;
|
|
|
data
|
|
data
|
|
|
}
|
|
}
|
|
@@ -414,6 +415,74 @@ impl CoinexSwapRest {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ //查询子账号列表
|
|
|
|
|
+ pub async fn account_get(&mut self) -> ResponseData {
|
|
|
|
|
+ let mut params = serde_json::json!({
|
|
|
|
|
+ "is_frozen":false,
|
|
|
|
|
+ "page":1,
|
|
|
|
|
+ "limit":100,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ let data = self.request("GET".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ Some(params.to_string()), None).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //根据子账号,生成子账号 APIKEY
|
|
|
|
|
+ pub async fn account_subs_api(&mut self, params: Value) -> ResponseData {
|
|
|
|
|
+ let data = self.request("POST".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs/api".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ None,
|
|
|
|
|
+ Some(params.to_string())).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //获取子账号 APIKEY 列表
|
|
|
|
|
+ pub async fn account_get_apikey(&mut self, params: Value) -> ResponseData {
|
|
|
|
|
+ let data = self.request("GET".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs/api".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ Some(params.to_string()), None).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //获取子账号 APIKEY 详情
|
|
|
|
|
+ pub async fn account_get_detail(&mut self, params: Value) -> ResponseData {
|
|
|
|
|
+ let data = self.request("GET".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs/api-detail".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ Some(params.to_string()), None).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //编辑子账号 APIKEY
|
|
|
|
|
+ pub async fn account_get_update(&mut self, params: Value) -> ResponseData {
|
|
|
|
|
+ let data = self.request("POST".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs/edit-api".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ None, Some(params.to_string())).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //删除子账号 APIKEY
|
|
|
|
|
+ pub async fn account_del_apikey(&mut self, params: Value) -> ResponseData {
|
|
|
|
|
+ let data = self.request("POST".to_string(),
|
|
|
|
|
+ "/v2".to_string(),
|
|
|
|
|
+ "/account/subs/delete-api".to_string(),
|
|
|
|
|
+ true,
|
|
|
|
|
+ None,
|
|
|
|
|
+ Some(params.to_string())).await;
|
|
|
|
|
+ data
|
|
|
|
|
+ }
|
|
|
/*******************************************************************************************************/
|
|
/*******************************************************************************************************/
|
|
|
/*****************************************工具函数********************************************************/
|
|
/*****************************************工具函数********************************************************/
|
|
|
/*******************************************************************************************************/
|
|
/*******************************************************************************************************/
|
|
@@ -505,7 +574,7 @@ impl CoinexSwapRest {
|
|
|
&url_and_query,
|
|
&url_and_query,
|
|
|
&body_s,
|
|
&body_s,
|
|
|
timestamp.clone(),
|
|
timestamp.clone(),
|
|
|
- &secret_key
|
|
|
|
|
|
|
+ &secret_key,
|
|
|
);
|
|
);
|
|
|
// trace!("sing:{}", sing);
|
|
// trace!("sing:{}", sing);
|
|
|
//组装header
|
|
//组装header
|
|
@@ -532,7 +601,7 @@ impl CoinexSwapRest {
|
|
|
path: &String,
|
|
path: &String,
|
|
|
body: &String,
|
|
body: &String,
|
|
|
timestamp: String,
|
|
timestamp: String,
|
|
|
- secret_key: &String
|
|
|
|
|
|
|
+ secret_key: &String,
|
|
|
) -> Result<String, Box<dyn Error>> {
|
|
) -> Result<String, Box<dyn Error>> {
|
|
|
let prepared_str = format!(
|
|
let prepared_str = format!(
|
|
|
"{}{}{}{}{}",
|
|
"{}{}{}{}{}",
|
|
@@ -569,26 +638,26 @@ impl CoinexSwapRest {
|
|
|
let data_json_str: Result<Value, serde_json::Error> = serde_json::from_str(text.as_str());
|
|
let data_json_str: Result<Value, serde_json::Error> = serde_json::from_str(text.as_str());
|
|
|
match data_json_str {
|
|
match data_json_str {
|
|
|
Ok(data_json) => {
|
|
Ok(data_json) => {
|
|
|
- return if is_success && data_json["code"].to_string() == "0"{
|
|
|
|
|
|
|
+ return if is_success && data_json["code"].to_string() == "0" {
|
|
|
self.on_success_data(data_json["data"].clone())
|
|
self.on_success_data(data_json["data"].clone())
|
|
|
} else {
|
|
} else {
|
|
|
self.on_error_data(&text, &url, &body)
|
|
self.on_error_data(&text, &url, &body)
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
Err(e) => {
|
|
Err(e) => {
|
|
|
error!("{} 请求完成,解析响应内容JSON失败 {} {}", url, text.as_str(), e);
|
|
error!("{} 请求完成,解析响应内容JSON失败 {} {}", url, text.as_str(), e);
|
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ }
|
|
|
Err(e) => {
|
|
Err(e) => {
|
|
|
error!("{} 请求完成,解析响应内容失败 {}", url, e);
|
|
error!("{} 请求完成,解析响应内容失败 {}", url, e);
|
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- },
|
|
|
|
|
- Err(e) => {// 异常情况
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ Err(e) => {
|
|
|
|
|
+ // 异常情况
|
|
|
error!("{} 请求失败,网络错误 {}", url, e);
|
|
error!("{} 请求失败,网络错误 {}", url, e);
|
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
self.on_error_data(&e.to_string(), &url, &body)
|
|
|
}
|
|
}
|
|
@@ -619,7 +688,7 @@ impl CoinexSwapRest {
|
|
|
}
|
|
}
|
|
|
Err(e) => {
|
|
Err(e) => {
|
|
|
error!("解析错误:{:?}", e);
|
|
error!("解析错误:{:?}", e);
|
|
|
- let error = ResponseData::error("".to_string(),format!("json 解析失败:{},相关参数:{}", e, text));
|
|
|
|
|
|
|
+ let error = ResponseData::error("".to_string(), format!("json 解析失败:{},相关参数:{}", e, text));
|
|
|
error
|
|
error
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|