|
|
@@ -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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|