Forráskód Böngészése

完成中间件架构

gepangpang 2 éve
szülő
commit
390e65db6e

+ 1 - 0
standard/Cargo.toml

@@ -6,4 +6,5 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+exchanges = { path = "../exchanges" }
 tokio = { version = "1.31.0", features = ["full"] }

+ 59 - 0
standard/src/binance_spot.rs

@@ -0,0 +1,59 @@
+use std::collections::BTreeMap;
+// use crate::utils;
+use crate::{Account, Platform, ExchangeEnum};
+use exchanges::binance_spot_rest::BinanceSpotRest;
+use exchanges::binance_spot_ws::BinanceSpotWs;
+// use exchanges::response_base::ResponseData;
+
+#[allow(dead_code)]
+pub struct BinanceSpot {
+    exchange: ExchangeEnum,
+    is_colo: bool,
+    is_login: bool,
+    params: BTreeMap<String, String>,
+    request: BinanceSpotRest,
+    wss: BinanceSpotWs,
+}
+
+impl BinanceSpot {
+    pub fn new(is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> BinanceSpot {
+        BinanceSpot {
+            exchange: ExchangeEnum::BinanceSpot,
+            is_colo,
+            is_login,
+            params: params.clone(),
+            request: BinanceSpotRest::new(is_colo, is_login, params.clone()),
+            wss: BinanceSpotWs::new(is_colo, is_login, params.clone()),
+        }
+    }
+}
+
+impl Platform for BinanceSpot {
+    // 获取params信息
+    fn get_params_info(&self) -> BTreeMap<String, String> {
+        self.params.clone()
+    }
+    // 获取期货账号信息
+    fn get_account(&self) -> Account {
+        todo!()
+    }
+
+
+    fn take_order(&self) {
+        todo!()
+    }
+
+    fn get_order_list(&self) {
+        todo!()
+    }
+
+    fn cancel_order(&self) {
+        todo!()
+    }
+
+    fn subscribe_account(&self) {
+        // self.wss.subscribe_account();
+    }
+
+    fn subscribe_kline(&self) {}
+}

+ 67 - 0
standard/src/binance_swap.rs

@@ -0,0 +1,67 @@
+use std::collections::BTreeMap;
+// use crate::utils;
+use crate::{Account, Platform, ExchangeEnum};
+use exchanges::binance_usdt_swap_rest::BinanceUsdtSwapRest;
+use exchanges::binance_usdt_swap_ws::BinanceUsdtSwapWs;
+use exchanges::response_base::ResponseData;
+
+#[allow(dead_code)]
+pub struct BinanceSwap {
+    exchange: ExchangeEnum,
+    is_colo: bool,
+    is_login: bool,
+    params: BTreeMap<String, String>,
+    request: BinanceUsdtSwapRest,
+    wss: BinanceUsdtSwapWs,
+}
+
+impl BinanceSwap {
+    pub fn new(is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> BinanceSwap {
+        BinanceSwap {
+            exchange: ExchangeEnum::BinanceSwap,
+            is_colo,
+            is_login,
+            params: params.clone(),
+            request: BinanceUsdtSwapRest::new(is_colo, is_login, params.clone()),
+            wss: BinanceUsdtSwapWs::new(is_colo, is_login, params.clone()),
+        }
+    }
+}
+
+impl Platform for BinanceSwap {
+    // 获取params信息
+    fn get_params_info(&self) -> BTreeMap<String, String> {
+        self.params.clone()
+    }
+    // 获取期货账号信息
+    fn get_account(&self) -> Account {
+        todo!()
+    }
+
+
+    fn take_order(&self) {
+        todo!()
+    }
+
+    fn get_order_list(&self) {
+        todo!()
+    }
+
+    fn cancel_order(&self) {
+        todo!()
+    }
+
+    fn subscribe_account(&self) {
+        // self.wss.subscribe_account();
+    }
+
+    fn subscribe_kline(&self) {
+        let get_res_data = move |res_data: ResponseData| {
+            async move {
+                println!("?????{:?}", res_data);
+            }
+        };
+
+        self.wss.kline(vec![&"BTCUSDT"], get_res_data);
+    }
+}

+ 60 - 0
standard/src/gate_spot.rs

@@ -0,0 +1,60 @@
+use std::collections::BTreeMap;
+// use crate::utils;
+use crate::{Account, Platform, ExchangeEnum};
+use exchanges::gate_spot_rest::GateSpotRest;
+use exchanges::gate_spot_ws::GateSpotWs;
+// use exchanges::response_base::ResponseData;
+
+#[allow(dead_code)]
+pub struct GateSpot {
+    exchange: ExchangeEnum,
+    is_colo: bool,
+    is_login: bool,
+    params: BTreeMap<String, String>,
+    request: GateSpotRest,
+    wss: GateSpotWs,
+}
+
+impl GateSpot {
+    pub fn new(is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> GateSpot {
+        GateSpot {
+            exchange: ExchangeEnum::GateSpot,
+            is_colo,
+            is_login,
+            params: params.clone(),
+            request: GateSpotRest::new(is_colo, is_login, params.clone()),
+            wss: GateSpotWs::new(is_colo, is_login, params.clone()),
+        }
+    }
+}
+
+impl Platform for GateSpot {
+    fn get_params_info(&self) -> BTreeMap<String, String> {
+        self.params.clone()
+    }
+
+    fn get_account(&self) -> Account {
+        todo!()
+    }
+
+
+    fn take_order(&self) {
+        todo!()
+    }
+
+    fn get_order_list(&self) {
+        todo!()
+    }
+
+    fn cancel_order(&self) {
+        todo!()
+    }
+
+    fn subscribe_account(&self) {
+        // self.wss.subscribe_account();
+    }
+
+    fn subscribe_kline(&self) {
+        todo!()
+    }
+}

+ 59 - 0
standard/src/gate_swap.rs

@@ -0,0 +1,59 @@
+use std::collections::BTreeMap;
+// use crate::utils;
+use crate::{Account, Platform, ExchangeEnum};
+use exchanges::gate_usdt_swap_rest::GateUsdtSwapRest;
+use exchanges::gate_usdt_swap_ws::GateUsdtSwapWs;
+// use exchanges::response_base::ResponseData;
+
+#[allow(dead_code)]
+pub struct GateSwap {
+    exchange: ExchangeEnum,
+    is_colo: bool,
+    is_login: bool,
+    params: BTreeMap<String, String>,
+    request: GateUsdtSwapRest,
+    wss: GateUsdtSwapWs,
+}
+
+impl GateSwap {
+    pub fn new(is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> GateSwap {
+        GateSwap {
+            exchange: ExchangeEnum::GateSwap,
+            is_colo,
+            is_login,
+            params: params.clone(),
+            request: GateUsdtSwapRest::new(is_colo, is_login, params.clone()),
+            wss: GateUsdtSwapWs::new(is_colo, is_login, params.clone()),
+        }
+    }
+}
+
+impl Platform for GateSwap {
+    fn get_params_info(&self) -> BTreeMap<String, String> {
+        self.params.clone()
+    }
+
+    fn get_account(&self) -> Account {
+        todo!()
+    }
+
+    fn take_order(&self) {
+        todo!()
+    }
+
+    fn get_order_list(&self) {
+        todo!()
+    }
+
+    fn cancel_order(&self) {
+        todo!()
+    }
+
+    fn subscribe_account(&self) {
+        // self.wss.subscribe_account();
+    }
+
+    fn subscribe_kline(&self) {
+        todo!()
+    }
+}

+ 149 - 2
standard/src/lib.rs

@@ -1,3 +1,150 @@
-pub fn test_standard() {
-    println!("Hello Standard!");
+use std::collections::BTreeMap;
+use crate::binance_swap::BinanceSwap;
+use crate::binance_spot::BinanceSpot;
+use crate::gate_spot::GateSpot;
+use crate::gate_swap::GateSwap;
+
+// 引入binance模块
+mod binance_swap;
+mod binance_spot;
+// 引入gate模块
+mod gate_swap;
+mod gate_spot;
+// 引入工具模块
+pub mod utils;
+
+
+/// Account结构体
+/// - balance(f64): 可用计价币数量;
+/// - frozen_balance(f64): balance挂单的冻结数量
+/// - stocks(f64): 可用交易币数量
+/// - frozen_stocks(f64): stocks挂单的冻结数量
+#[derive(Debug)]
+pub struct Account {
+    pub balance: f64,
+    pub frozen_balance: f64,
+    pub stocks: f64,
+    pub frozen_stocks: f64,
 }
+
+/// 交易所统一方法接口
+///
+/// 使用方法前需实例化
+/// ```rust
+/// use std::collections::BTreeMap;
+/// use standard::{Exchange, ExchangeEnum};
+///
+/// let mut params:BTreeMap<String,String> = BTreeMap::new();
+/// params.insert("access_key".to_string(), "your_access_key".to_string());
+/// params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
+/// ```
+/// 获取登录params信息
+/// - fn get_params_info(&self) -> BTreeMap<String,String>;
+/// ```rust
+/// # use std::collections::BTreeMap;
+/// # use standard::{Exchange, ExchangeEnum};
+/// # let mut params:BTreeMap<String,String> = BTreeMap::new();
+/// # params.insert("access_key".to_string(), "your_access_key".to_string());
+/// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
+///
+/// exchange.get_params_info();
+/// ```
+/// 获取账号信息
+/// ```rust
+/// # use std::collections::BTreeMap;
+/// # use standard::{Exchange, ExchangeEnum};
+/// # let mut params:BTreeMap<String,String> = BTreeMap::new();
+/// # params.insert("access_key".to_string(), "your_access_key".to_string());
+/// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
+///
+/// exchange.get_account();
+/// ```
+/// 订阅账号信息
+/// ```rust
+/// # use std::collections::BTreeMap;
+/// # use standard::{Exchange, ExchangeEnum};
+/// # let mut params:BTreeMap<String,String> = BTreeMap::new();
+/// # params.insert("access_key".to_string(), "your_access_key".to_string());
+/// # params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// # let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
+///
+/// exchange.subscribe_account();
+/// ```
+pub trait Platform {
+    fn get_params_info(&self) -> BTreeMap<String, String>;
+    fn get_account(&self) -> Account;
+    // 下单接口
+    fn take_order(&self);
+    // 获取订单列表
+    fn get_order_list(&self);
+    // 取消订单
+    fn cancel_order(&self);
+    fn subscribe_account(&self);
+    fn subscribe_kline(&self);
+}
+
+/// 交易所交易模式枚举
+/// - `BinanceSwap`: Binance交易所;
+/// - `BinanceSpot`: Binance交易所;
+/// - `GateSwap`: Gate交易所;
+/// - `GateSpot`: Gate交易所;
+#[derive(Debug)]
+pub enum ExchangeEnum {
+    BinanceSwap,
+    BinanceSpot,
+    GateSwap,
+    GateSpot,
+}
+
+/// Exchange结构体
+///
+/// 方法:
+/// - 创建Exchange:
+///
+/// new(platform: [PlatformEnum], is_colo: bool, is_login:bool, params: BTreeMap<String, String>) -> Box\<dyn Platform\>
+/// - platform([PlatformEnum]): 交易所平台枚举
+/// - is_colo(bool): 是否开始告诉模式
+/// - is_login(bool): 是否需要登录
+/// - params(BTreeMap\<String, String\>): 登录所需参数
+///
+/// 示例参数值:
+///
+/// | 交易所枚举 | params参数示例 BTreeMap<String,String> |
+/// | --- | --- |
+/// | BinanceSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
+/// | BinanceSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
+/// | GateSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
+/// | GateSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
+///  ```rust
+/// use std::collections::BTreeMap;
+/// use standard::{Exchange, ExchangeEnum};
+///
+/// let mut params:BTreeMap<String,String> = BTreeMap::new();
+/// params.insert("access_key".to_string(), "your_access_key".to_string());
+/// params.insert("access_key".to_string(), "your_secret_key".to_string());
+/// let exchange = Exchange::new(ExchangeEnum::BinanceSwap, true, true, params);
+pub struct Exchange {}
+
+impl Exchange {
+    pub fn new(exchange: ExchangeEnum, is_colo: bool, is_login: bool, params: BTreeMap<String, String>) -> Box<dyn Platform> {
+        match exchange {
+            ExchangeEnum::BinanceSwap => {
+                Box::new(BinanceSwap::new(is_colo, is_login, params))
+            }
+            ExchangeEnum::BinanceSpot => {
+                Box::new(BinanceSpot::new(is_colo, is_login, params))
+            }
+            ExchangeEnum::GateSwap => {
+                Box::new(GateSpot::new(is_colo, is_login, params))
+            }
+            ExchangeEnum::GateSpot => {
+                Box::new(GateSwap::new(is_colo, is_login, params))
+            }
+        }
+    }
+}
+
+

+ 6 - 0
standard/src/utils.rs

@@ -0,0 +1,6 @@
+/// 修改交易对连接符号
+/// - `symbol`(str): 交易对, "BTC_USDT", 默认以下划线传递
+/// - `pat`(str): 替换字符, "-", 把 “_” 替换为 "-"
+pub fn replace_symbol(symbol: &str, pat: &str) -> String {
+    return symbol.to_uppercase().replace("_", pat);
+}

+ 77 - 4
standard/tests/libs_test.rs

@@ -1,6 +1,79 @@
-use standard;
+use std::collections::BTreeMap;
+use standard::{Platform, ExchangeEnum, Exchange};
+use standard::utils;
 
+// 创建交易所实体方法
+fn test_new_exchange(platform: ExchangeEnum) -> Box<dyn Platform> {
+    match platform {
+        ExchangeEnum::BinanceSwap => {
+            let mut params: BTreeMap<String, String> = BTreeMap::new();
+            params.insert("access_key".to_string(), "binance_swap_access_key".to_string());
+            params.insert("secret_key".to_string(), "binance_swap_secret_key".to_string());
+            Exchange::new(platform, false, false, params)
+        }
+        ExchangeEnum::BinanceSpot => {
+            let mut params: BTreeMap<String, String> = BTreeMap::new();
+            params.insert("access_key".to_string(), "binance_spot_access_key".to_string());
+            params.insert("secret_key".to_string(), "binance_spot_secret_key".to_string());
+            Exchange::new(platform, false, false, params)
+        }
+        ExchangeEnum::GateSwap => {
+            let mut params: BTreeMap<String, String> = BTreeMap::new();
+            params.insert("access_key".to_string(), "gate_swap_access_key".to_string());
+            params.insert("secret_key".to_string(), "gate_swap_secret_key".to_string());
+            Exchange::new(platform, true, false, params)
+        }
+        ExchangeEnum::GateSpot => {
+            let mut params: BTreeMap<String, String> = BTreeMap::new();
+            params.insert("access_key".to_string(), "gate_spot_access_key".to_string());
+            params.insert("secret_key".to_string(), "gate_spot_secret_key".to_string());
+            Exchange::new(platform, true, false, params)
+        }
+    }
+}
+
+
+// 测试获取Binance交易所实体
+#[tokio::test]
+async fn test_binance_swap_account() {
+    let binance_exchange = test_new_exchange(ExchangeEnum::BinanceSwap);
+    binance_exchange.get_account();
+    binance_exchange.subscribe_account();
+    println!("{:?}", binance_exchange.get_params_info());
+}
+#[tokio::test]
+async fn test_binance_spot_account() {
+    let binance_exchange = test_new_exchange(ExchangeEnum::BinanceSpot);
+    binance_exchange.get_account();
+    binance_exchange.subscribe_account();
+    println!("{:?}", binance_exchange.get_params_info());
+}
+// 测试获取Gate交易所实体
+#[tokio::test]
+async fn test_gate_swap_account() {
+    let gate_exchange = test_new_exchange(ExchangeEnum::GateSwap);
+    gate_exchange.get_account();
+    gate_exchange.subscribe_account();
+    println!("{:?}", gate_exchange.get_params_info());
+}
+// 测试获取Gate交易所实体
+#[tokio::test]
+async fn test_gate_spot_account() {
+    let gate_exchange = test_new_exchange(ExchangeEnum::GateSpot);
+    gate_exchange.get_account();
+    gate_exchange.subscribe_account();
+    println!("{:?}", gate_exchange.get_params_info());
+}
+
+// 测试币对连接符号替换
+#[tokio::test]
+async fn test_replace_symbol() {
+    println!("test_replace_symbol: {}", utils::replace_symbol("BTC_USDT", "-"))
+}
+
+// 测试Binance期货K线推送
 #[tokio::test]
-async fn test_import() {
-    standard::test_standard();
-}
+async fn test_subscribe_kline() {
+    let binance_exchange:Box<dyn Platform> = test_new_exchange(ExchangeEnum::BinanceSwap);
+    binance_exchange.subscribe_kline();
+}