exchange.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use std::collections::{BTreeMap};
  2. use std::io::Error;
  3. use tokio::sync::mpsc::Sender;
  4. use crate::{Order, Platform};
  5. use crate::binance_swap::BinanceSwap;
  6. use crate::bitget_swap::BitgetSwap;
  7. use crate::bybit_swap::BybitSwap;
  8. use crate::coinex_swap::CoinexSwap;
  9. use crate::gate_swap::GateSwap;
  10. /// 交易所交易模式枚举
  11. /// - `BinanceSwap`: Binance交易所期货;
  12. /// - `BinanceSpot`: Binance交易所现货;
  13. /// - `GateSwap`: Gate交易所期货;
  14. /// - `GateSpot`: Gate交易所现货;
  15. /// - `KucoinSwap`: kucoin交易所期货;
  16. #[derive(Debug, Clone, PartialEq, Eq)]
  17. pub enum ExchangeEnum {
  18. BinanceSwap,
  19. CoinexSwap,
  20. // BinanceSpot,
  21. GateSwap,
  22. // GateSpot,
  23. // KucoinSwap,
  24. // KucoinSpot,
  25. // OkxSwap,
  26. // BitgetSpot,
  27. BitgetSwap,
  28. BybitSwap,
  29. // HtxSwap,
  30. // BingxSwap,
  31. // MexcSwap,
  32. // BitMartSwap,
  33. // CoinsphSwap,
  34. // PhemexSwap,
  35. // WooSwap,
  36. // CointrSwap
  37. }
  38. /// Exchange结构体
  39. ///
  40. /// 方法:
  41. /// - 创建Exchange:
  42. ///
  43. /// new(platform: [ExchangeEnum], symbol: `String`, is_colo: `bool`, params: `BTreeMap<String, String>`, order_sender: `Sender<Order>`, error_sender: `Sender<Error>`) -> `Box<dyn Platform + Send + Sync>`
  44. /// - `platform(`[ExchangeEnum]`)`: 交易所平台枚举
  45. /// - `symbol(String)`: 币对
  46. /// - `is_colo(bool)`: 是否开始告诉模式
  47. /// - `params(BTreeMap<String, String>)`: 登录所需参数
  48. /// - `order_sender(Sender<Order>)`: 订单消息发送者
  49. /// - `error_sender(Sender<Error>)`: 错误消息发送者
  50. ///
  51. /// 示例参数值:
  52. ///
  53. /// | 交易所枚举 | params参数示例 BTreeMap<String, String> |
  54. /// | --- | --- |
  55. /// | BinanceSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
  56. /// | BinanceSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
  57. /// | GateSwap | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
  58. /// | GateSpot | {"access_key":"your_access_key","secret_key":"your_secret_key"} |
  59. /// | KucoinSwap | {"access_key":"your_access_key","secret_key":"your_secret_key","pass_key":"your_pass_key"} |
  60. /// ```rust
  61. /// use std::collections::BTreeMap;
  62. /// use std::io::Error;
  63. /// use tokio::sync::mpsc;
  64. /// use standard::exchange::{Exchange, ExchangeEnum};
  65. /// use standard::Order;
  66. ///
  67. /// let mut params:BTreeMap<String,String> = BTreeMap::new();
  68. /// params.insert("access_key".to_string(), "your_access_key".to_string());
  69. /// params.insert("secret_key".to_string(), "your_secret_key".to_string());
  70. /// let (order_sender, _order_receiver): (mpsc::Sender<Order>, mpsc::Receiver<Order>) = mpsc::channel(1024);
  71. /// let (error_sender, _error_receiver): (mpsc::Sender<Error>, mpsc::Receiver<Error>) = mpsc::channel(1024);
  72. ///
  73. /// let exchange = Exchange::new(ExchangeEnum::GateSwap, "BTC_USDT".to_string(), false, params, order_sender, error_sender);
  74. #[derive(Debug, Clone)]
  75. pub struct Exchange;
  76. impl Exchange {
  77. pub async fn new(exchange: ExchangeEnum, symbol: String, is_colo: bool, params: BTreeMap<String, String>, order_sender: Sender<Order>, error_sender: Sender<Error>) -> Box<dyn Platform + Send + Sync> {
  78. match exchange {
  79. ExchangeEnum::BinanceSwap => {
  80. Box::new(BinanceSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  81. }
  82. ExchangeEnum::GateSwap => {
  83. Box::new(GateSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  84. }
  85. // ExchangeEnum::GateSpot => {
  86. // Box::new(GateSpot::new(symbol, is_colo, params, order_sender, error_sender).await)
  87. // }
  88. // ExchangeEnum::KucoinSwap => {
  89. // Box::new(KucoinSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  90. // }
  91. // ExchangeEnum::KucoinSpot =>{
  92. // Box::new(KucoinSpot::new(symbol, is_colo, params, order_sender, error_sender).await)
  93. // }
  94. // ExchangeEnum::OkxSwap => {
  95. // Box::new(OkxSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  96. // }
  97. // ExchangeEnum::BitgetSpot => {
  98. // Box::new(BitgetSpot::new(symbol, is_colo, params, order_sender, error_sender).await)
  99. // }
  100. ExchangeEnum::BitgetSwap => {
  101. Box::new(BitgetSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  102. }
  103. ExchangeEnum::BybitSwap => {
  104. Box::new(BybitSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  105. }
  106. ExchangeEnum::CoinexSwap => {
  107. Box::new(CoinexSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  108. }
  109. // ExchangeEnum::HtxSwap => {
  110. // Box::new(HtxSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  111. // }
  112. // ExchangeEnum::BingxSwap => {
  113. // Box::new(BingxSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  114. // }
  115. // ExchangeEnum::MexcSwap => {
  116. // Box::new(MexcSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  117. // }
  118. // ExchangeEnum::BitMartSwap => {
  119. // Box::new(BitMartSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  120. // }
  121. // ExchangeEnum::CoinsphSwap => {
  122. // Box::new(CoinsphSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  123. // }
  124. // ExchangeEnum::PhemexSwap => {
  125. // Box::new(PhemexSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  126. // }
  127. // ExchangeEnum::WooSwap => {
  128. // Box::new(WooSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  129. // }
  130. // ExchangeEnum::CointrSwap => {
  131. // Box::new(CointrSwap::new(symbol, is_colo, params, order_sender, error_sender).await)
  132. // }
  133. }
  134. }
  135. }