|
|
@@ -13,7 +13,7 @@ use reqwest;
|
|
|
use reqwest::header::{HeaderMap, HeaderValue};
|
|
|
use hex;
|
|
|
use ring::hmac;
|
|
|
-use serde_json::{json, Map, Value};
|
|
|
+use serde_json::{json};
|
|
|
use tungstenite::client::{connect_with_proxy};
|
|
|
use tungstenite::{connect, Message};
|
|
|
use tungstenite::protocol::WebSocketConfig;
|
|
|
@@ -22,16 +22,21 @@ use url::Url;
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
|
+ use std::env;
|
|
|
use crate::exchange_libs::{BinanceExc, http_enable_proxy, OkxExc, ResponseData};
|
|
|
|
|
|
#[tokio::test]//测试获取ok 账号信息
|
|
|
async fn test_okx_get_acc() {
|
|
|
+ let okx_access_key = env::var("okx_access_key").unwrap();
|
|
|
+ let okx_secret_key = env::var("okx_secret_key").unwrap();
|
|
|
+ let okx_passphrase = env::var("okx_passphrase").unwrap();
|
|
|
+
|
|
|
//开启代理
|
|
|
http_enable_proxy();
|
|
|
let okx_exc = OkxExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
- "rust_Test123".to_string(),
|
|
|
+ okx_access_key.to_string(),
|
|
|
+ okx_secret_key.to_string(),
|
|
|
+ okx_passphrase.to_string(),
|
|
|
);
|
|
|
let req: ResponseData = okx_exc.okx_acc("USDT").await;
|
|
|
print!("---响应:code:{}", req.code);
|
|
|
@@ -41,12 +46,16 @@ mod tests {
|
|
|
|
|
|
#[tokio::test]//测试okx 下单
|
|
|
async fn test_okx_order() {
|
|
|
+ let okx_access_key = env::var("okx_access_key").unwrap();
|
|
|
+ let okx_secret_key = env::var("okx_secret_key").unwrap();
|
|
|
+ let okx_passphrase = env::var("okx_passphrase").unwrap();
|
|
|
+
|
|
|
//开启代理
|
|
|
http_enable_proxy();
|
|
|
let okx_exc = OkxExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
- "rust_Test123".to_string(),
|
|
|
+ okx_access_key.to_string(),
|
|
|
+ okx_secret_key.to_string(),
|
|
|
+ okx_passphrase.to_string(),
|
|
|
);
|
|
|
let req: ResponseData = okx_exc.okx_order("CORE-USDT", "cash", "buy", "limit", "0.8555", "1").await;
|
|
|
print!("---响应:code:{}", req.code);
|
|
|
@@ -56,12 +65,16 @@ mod tests {
|
|
|
|
|
|
#[tokio::test]//测试okx获取订单信息
|
|
|
async fn test_okx_get_order() {
|
|
|
+ let okx_access_key = env::var("okx_access_key").unwrap();
|
|
|
+ let okx_secret_key = env::var("okx_secret_key").unwrap();
|
|
|
+ let okx_passphrase = env::var("okx_passphrase").unwrap();
|
|
|
+
|
|
|
//开启代理
|
|
|
http_enable_proxy();
|
|
|
let okx_exc = OkxExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
- "rust_Test123".to_string(),
|
|
|
+ okx_access_key.to_string(),
|
|
|
+ okx_secret_key.to_string(),
|
|
|
+ okx_passphrase.to_string(),
|
|
|
);
|
|
|
let req2: ResponseData = okx_exc.okx_get_order("CORE-USDT", "611949361383612427").await;
|
|
|
print!("---响应:code:{}", req2.code);
|
|
|
@@ -71,12 +84,16 @@ mod tests {
|
|
|
|
|
|
#[tokio::test]//测试okx获取订单信息
|
|
|
async fn test_okx_revocation_order() {
|
|
|
+ let okx_access_key = env::var("okx_access_key").unwrap();
|
|
|
+ let okx_secret_key = env::var("okx_secret_key").unwrap();
|
|
|
+ let okx_passphrase = env::var("okx_passphrase").unwrap();
|
|
|
+
|
|
|
//开启代理
|
|
|
http_enable_proxy();
|
|
|
let okx_exc = OkxExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
- "rust_Test123".to_string(),
|
|
|
+ okx_access_key.to_string(),
|
|
|
+ okx_secret_key.to_string(),
|
|
|
+ okx_passphrase.to_string(),
|
|
|
);
|
|
|
let req3: ResponseData = okx_exc.okx_revocation_order("CORE-USDT", "611950727669751811").await;
|
|
|
print!("---响应:code:{}", req3.code);
|
|
|
@@ -89,8 +106,8 @@ mod tests {
|
|
|
async fn test_binance_k() {
|
|
|
http_enable_proxy();
|
|
|
let binance_exc = BinanceExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
+ "test_apikey".to_string(),
|
|
|
+ "test_secret_key".to_string(),
|
|
|
);
|
|
|
let req3: ResponseData = binance_exc.binance_k("BTCUSDT", "5m", "20").await;
|
|
|
print!("---响应:code:{}", req3.code);
|
|
|
@@ -102,8 +119,8 @@ mod tests {
|
|
|
async fn test_binance_depth() {
|
|
|
http_enable_proxy();
|
|
|
let binance_exc = BinanceExc::new(
|
|
|
- "a4cf4f54-f4d3-447d-a57c-166fd1ead2e0".to_string(),
|
|
|
- "556DAB6773CA26DDAAA114F7044138CA".to_string(),
|
|
|
+ "test_apikey".to_string(),
|
|
|
+ "test_secret_key".to_string(),
|
|
|
);
|
|
|
let req3: ResponseData = binance_exc.binance_depth("BTCUSDT", "20").await;
|
|
|
//println!("---响应:code:{}", req3.code);
|
|
|
@@ -192,9 +209,9 @@ impl OkxExc {
|
|
|
pub fn new(access_key: String, secret_key: String, passphrase: String) -> OkxExc {
|
|
|
OkxExc {
|
|
|
base_url: "https://www.okx.com".to_string(),
|
|
|
- access_key: access_key,
|
|
|
- secret_key: secret_key,
|
|
|
- passphrase: passphrase,
|
|
|
+ access_key,
|
|
|
+ secret_key,
|
|
|
+ passphrase,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -387,8 +404,8 @@ impl OkxExc {
|
|
|
//okx 签名生成
|
|
|
fn okx_sign(&self, secret_key: String, message: String) -> String {
|
|
|
// 做签名
|
|
|
- let hmac_key = ring::hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
|
|
|
- let result = ring::hmac::sign(&hmac_key, &message.as_bytes());
|
|
|
+ let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
|
|
|
+ let result = hmac::sign(&hmac_key, &message.as_bytes());
|
|
|
base64::encode(result)
|
|
|
}
|
|
|
|
|
|
@@ -479,7 +496,7 @@ impl SocketTool {
|
|
|
{
|
|
|
let mut ip_v = "";
|
|
|
let mut port_v = 8080;
|
|
|
- let mut v_str = String::from("");
|
|
|
+ // let mut v_str = String::from("");
|
|
|
|
|
|
/*******读取环境变量-判定初始化代理地址*******/
|
|
|
let parsing_detail = parsing_environment_variables();
|
|
|
@@ -492,9 +509,9 @@ impl SocketTool {
|
|
|
request_url: request_url.to_string(),
|
|
|
ip: ip_v.clone().to_string(),
|
|
|
port: port_v,
|
|
|
- is_login: is_login,
|
|
|
- login_param: login_param,
|
|
|
- subscription: subscription,
|
|
|
+ is_login,
|
|
|
+ login_param,
|
|
|
+ subscription,
|
|
|
}
|
|
|
}
|
|
|
pub(crate) fn run<F, Fut>(&self, parse_fn: F)
|
|
|
@@ -529,7 +546,7 @@ impl SocketTool {
|
|
|
accept_unmasked_frames: false,
|
|
|
});
|
|
|
let max_redirects = 5;
|
|
|
- let (mut socket, response) =
|
|
|
+ let (mut socket, _) =
|
|
|
connect_with_proxy(request_url.clone(), proxy_address, websocket_config, max_redirects)
|
|
|
.expect("Can't connect(无法连接)");
|
|
|
|
|
|
@@ -577,7 +594,7 @@ impl SocketTool {
|
|
|
if lable.to_string() == "okx".to_string() {
|
|
|
if (Utc::now().timestamp() - okx_ping_time) > 2 {
|
|
|
socket.write_message(Message::Ping(Vec::from("ping"))).expect("TODO: panic message");
|
|
|
- writeln!(stdout, "主动------:{:?}---ping", lable).unwrap();
|
|
|
+ // writeln!(stdout, "主动------:{:?}---ping", lable).unwrap();
|
|
|
okx_ping_time = Utc::now().timestamp();
|
|
|
okx_ping_off = true;
|
|
|
}
|
|
|
@@ -589,11 +606,11 @@ impl SocketTool {
|
|
|
if lable.to_string() == "binance".to_string() {
|
|
|
op_str = "pong".to_string();
|
|
|
socket.write_message(Message::Pong(Vec::from(op_str.clone()))).expect("TODO: panic message");
|
|
|
- writeln!(stdout, "ping----------:{:?}---{:?}", lable, op_str).unwrap();
|
|
|
+ // writeln!(stdout, "ping----------:{:?}---{:?}", lable, op_str).unwrap();
|
|
|
}
|
|
|
}
|
|
|
Ok(Message::Pong(s)) => {
|
|
|
- writeln!(stdout, "Pong-响应--{:?}-{:?}", String::from_utf8(s),lable.to_string()).unwrap();
|
|
|
+ // writeln!(stdout, "Pong-响应--{:?}-{:?}", String::from_utf8(s),lable.to_string()).unwrap();
|
|
|
if lable.to_string() == "okx".to_string() {
|
|
|
okx_ping_off = false;
|
|
|
okx_ping_time = Utc::now().timestamp();
|
|
|
@@ -618,7 +635,7 @@ impl SocketTool {
|
|
|
} else {
|
|
|
// 提示,并未找到好的优化方式,
|
|
|
//println!("----socket-没代理");
|
|
|
- let (mut socket, response) =
|
|
|
+ let (mut socket, _) =
|
|
|
connect(request_url.clone())
|
|
|
.expect("Can't connect(无法连接)");
|
|
|
|
|
|
@@ -688,23 +705,23 @@ impl SocketTool {
|
|
|
if lable.to_string() == "okx".to_string() {
|
|
|
if (Utc::now().timestamp() - okx_ping_time) > 2 {
|
|
|
socket.write_message(Message::Ping(Vec::from("ping"))).expect("TODO: panic message");
|
|
|
- writeln!(stdout, "主动------:{:?}---ping", lable).unwrap();
|
|
|
+ // writeln!(stdout, "主动------:{:?}---ping", lable).unwrap();
|
|
|
okx_ping_time = Utc::now().timestamp();
|
|
|
okx_ping_off = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
Ok(Message::Ping(s)) => {
|
|
|
- writeln!(stdout, "Ping-响应--{:?}", String::from_utf8(s)).unwrap();
|
|
|
+ // writeln!(stdout, "Ping-响应--{:?}", String::from_utf8(s)).unwrap();
|
|
|
let mut op_str = "".to_string();
|
|
|
if lable.to_string() == "binance".to_string() {
|
|
|
op_str = "pong".to_string();
|
|
|
socket.write_message(Message::Pong(Vec::from(op_str.clone()))).expect("TODO: panic message");
|
|
|
- writeln!(stdout, "ping----------:{:?}---{:?}", lable, op_str).unwrap();
|
|
|
+ // writeln!(stdout, "ping----------:{:?}---{:?}", lable, op_str).unwrap();
|
|
|
}
|
|
|
}
|
|
|
Ok(Message::Pong(s)) => {
|
|
|
- writeln!(stdout, "Pong-响应--{:?}", String::from_utf8(s)).unwrap();
|
|
|
+ // writeln!(stdout, "Pong-响应--{:?}", String::from_utf8(s)).unwrap();
|
|
|
if lable.to_string() == "okx".to_string() {
|
|
|
okx_ping_off = false;
|
|
|
okx_ping_time = Utc::now().timestamp();
|
|
|
@@ -759,7 +776,7 @@ impl SocketTool {
|
|
|
// //println!("---message:{:?}", message);
|
|
|
let hmac_key = ring::hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
|
|
|
let result = ring::hmac::sign(&hmac_key, &message.as_bytes());
|
|
|
- let mut sign = base64::encode(result);
|
|
|
+ let sign = base64::encode(result);
|
|
|
|
|
|
let login_json = json!({
|
|
|
"op": "login",
|
|
|
@@ -813,10 +830,10 @@ impl SocketTool {
|
|
|
|
|
|
|
|
|
//币安--自定义-订阅
|
|
|
- pub fn binance_run_custom(b_array: Vec<&str>, parse_fn: impl Fn(ResponseData)) {}
|
|
|
+ // pub fn binance_run_custom(b_array: Vec<&str>, parse_fn: impl Fn(ResponseData)) {}
|
|
|
|
|
|
|
|
|
- //币安--深度信息
|
|
|
+ //币安--k线信息
|
|
|
pub fn binance_run_kline<F, Fut>(b_array: Vec<&str>, parse_fn: F)
|
|
|
where
|
|
|
F: Fn(ResponseData) -> Fut + 'static + Send + Sync,
|
|
|
@@ -992,7 +1009,7 @@ fn parse_params_to_str(parameters: BTreeMap<&str, &str>) -> String {
|
|
|
|
|
|
//获取时时间
|
|
|
fn get_timestamp() -> String {
|
|
|
- chrono::Utc::now()
|
|
|
+ Utc::now()
|
|
|
.format("%Y-%m-%dT%H:%M:%S%.3fZ")
|
|
|
.to_string()
|
|
|
}
|