bitmart_swap.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // use std::collections::{BTreeMap};
  2. // use std::io::{Error, ErrorKind};
  3. // use std::str::FromStr;
  4. // use tokio::sync::mpsc::Sender;
  5. // use async_trait::async_trait;
  6. // use rust_decimal::{Decimal};
  7. // use serde_json::{json, Value};
  8. // use tracing::{error, info};
  9. // use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, utils};
  10. // use exchanges::bitmart_swap_rest::BitMartSwapRest;
  11. // use rust_decimal::prelude::FromPrimitive;
  12. //
  13. // #[allow(dead_code)]
  14. // #[derive(Clone)]
  15. // pub struct BitMartSwap {
  16. // exchange: ExchangeEnum,
  17. // symbol: String,
  18. // is_colo: bool,
  19. // params: BTreeMap<String, String>,
  20. // request: BitMartSwapRest,
  21. // market: Market,
  22. // order_sender: Sender<Order>,
  23. // error_sender: Sender<Error>,
  24. // }
  25. //
  26. // impl BitMartSwap {
  27. // pub async fn new(symbol: String, is_colo: bool, params: BTreeMap<String, String>, order_sender: Sender<Order>, error_sender: Sender<Error>) -> BitMartSwap {
  28. // let market = Market::new();
  29. // let mut bitmart_swap = BitMartSwap {
  30. // exchange: ExchangeEnum::BitMartSwap,
  31. // symbol: symbol.to_uppercase(),
  32. // is_colo,
  33. // params: params.clone(),
  34. // request: BitMartSwapRest::new(is_colo, params.clone()),
  35. // market,
  36. // order_sender,
  37. // error_sender,
  38. // };
  39. //
  40. // // 修改持仓模式
  41. // let symbol_array: Vec<&str> = symbol.split("_").collect();
  42. // let mode_result = bitmart_swap.set_dual_mode(symbol_array[1], true).await;
  43. // match mode_result {
  44. // Ok(ok) => {
  45. // info!("BitMart:设置持仓模式成功!{:?}", ok);
  46. // }
  47. // Err(error) => {
  48. // error!("BitMart:设置持仓模式失败!mode_result={}", error)
  49. // }
  50. // }
  51. // // 获取市场信息
  52. // bitmart_swap.market = BitMartSwap::get_market(&mut bitmart_swap).await.unwrap_or(bitmart_swap.market);
  53. // return bitmart_swap;
  54. // }
  55. // }
  56. //
  57. // #[async_trait]
  58. // impl Platform for BitMartSwap {
  59. // // 克隆方法
  60. // fn clone_box(&self) -> Box<dyn Platform + Send + Sync> { Box::new(self.clone()) }
  61. // // 获取交易所模式
  62. // fn get_self_exchange(&self) -> ExchangeEnum {
  63. // ExchangeEnum::BitMartSwap
  64. // }
  65. // // 获取交易对
  66. // fn get_self_symbol(&self) -> String { self.symbol.clone() }
  67. // // 获取是否使用高速通道
  68. // fn get_self_is_colo(&self) -> bool {
  69. // self.is_colo
  70. // }
  71. // // 获取params信息
  72. // fn get_self_params(&self) -> BTreeMap<String, String> {
  73. // self.params.clone()
  74. // }
  75. // // 获取market信息
  76. // fn get_self_market(&self) -> Market { self.market.clone() }
  77. // // 获取请求时间
  78. // fn get_request_delays(&self) -> Vec<i64> { self.request.get_delays() }
  79. // // 获取请求平均时间
  80. // fn get_request_avg_delay(&self) -> Decimal { self.request.get_avg_delay() }
  81. // // 获取请求最大时间
  82. // fn get_request_max_delay(&self) -> i64 { self.request.get_max_delay() }
  83. //
  84. // // 获取服务器时间
  85. // async fn get_server_time(&mut self) -> Result<String, Error> {
  86. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  87. // }
  88. // // 获取账号信息
  89. // async fn get_account(&mut self) -> Result<Account, Error> {
  90. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  91. // }
  92. //
  93. // async fn get_spot_account(&mut self) -> Result<Vec<Account>, Error> {
  94. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  95. // }
  96. //
  97. // // 获取持仓信息
  98. // async fn get_position(&mut self) -> Result<Vec<Position>, Error> {
  99. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  100. // }
  101. // // 获取所有持仓
  102. // async fn get_positions(&mut self) -> Result<Vec<Position>, Error> {
  103. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  104. // }
  105. // // 获取市场行情
  106. // async fn get_ticker(&mut self) -> Result<Ticker, Error> {
  107. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  108. // }
  109. //
  110. // async fn get_ticker_symbol(&mut self, _symbol: String) -> Result<Ticker, Error> {
  111. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  112. // }
  113. //
  114. // async fn get_market(&mut self) -> Result<Market, Error> {
  115. // self.get_market_symbol(self.symbol.clone()).await
  116. // }
  117. //
  118. // async fn get_market_symbol(&mut self, symbol: String) -> Result<Market, Error> {
  119. // let symbol_format =utils::format_symbol(symbol.clone(), "");
  120. // let params = json!({
  121. // "symbol": symbol_format.clone()
  122. // });
  123. // let res_data = self.request.get_market(params).await;
  124. // if res_data.code == 200 {
  125. // let res_data_json = res_data.data["symbols"].as_array().unwrap();
  126. // let market_info = res_data_json.iter().find(|item| item["symbol"].as_str().unwrap() == symbol_format);
  127. // match market_info {
  128. // None => {
  129. // error!("bitmart_swap:获取Market信息错误!\nget_market:res_data={:?}", res_data);
  130. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  131. // }
  132. // Some(value) => {
  133. // let base_asset = value["base_currency"].as_str().unwrap().to_string();
  134. // let quote_asset = value["quote_currency"].as_str().unwrap().to_string();
  135. //
  136. // let tick_size = Decimal::from_str(value["price_precision"].as_str().unwrap()).unwrap();
  137. // let price_precision = Decimal::from_u32(tick_size.scale()).unwrap();
  138. // let amount_size = Decimal::from_str(value["vol_precision"].as_str().unwrap()).unwrap();
  139. // let amount_precision = Decimal::from_u32(amount_size.scale()).unwrap();
  140. // let min_qty = Decimal::from_str(value["min_volume"].as_str().unwrap()).unwrap();
  141. // let max_qty = Decimal::from_str(value["max_volume"].as_str().unwrap()).unwrap();
  142. // let ct_val = Decimal::from_str(value["contract_size"].as_str().unwrap()).unwrap();
  143. //
  144. // let result = Market {
  145. // symbol: format!("{}_{}", base_asset, quote_asset),
  146. // base_asset,
  147. // quote_asset,
  148. // tick_size,
  149. // amount_size,
  150. // price_precision,
  151. // amount_precision,
  152. // min_qty,
  153. // max_qty,
  154. // min_notional: min_qty * ct_val,
  155. // max_notional: max_qty * ct_val,
  156. // ct_val,
  157. // };
  158. // Ok(result)
  159. // }
  160. // }
  161. // } else {
  162. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  163. // }
  164. // }
  165. //
  166. // // 获取订单详情
  167. // async fn get_order_detail(&mut self, _order_id: &str, _custom_id: &str) -> Result<Order, Error> {
  168. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  169. // }
  170. // // 获取订单列表
  171. // async fn get_orders_list(&mut self, _status: &str) -> Result<Vec<Order>, Error> {
  172. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  173. // }
  174. // // 下单接口
  175. // async fn take_order(&mut self, _custom_id: &str, _origin_side: &str, _price: Decimal, _amount: Decimal) -> Result<Order, Error> {
  176. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  177. // }
  178. //
  179. // async fn take_order_symbol(&mut self, _symbol: String, _ct_val: Decimal, _custom_id: &str, _origin_side: &str, _price: Decimal, _amount: Decimal) -> Result<Order, Error> {
  180. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  181. // }
  182. //
  183. // // 撤销订单
  184. // async fn cancel_order(&mut self, _order_id: &str, _custom_id: &str) -> Result<Order, Error> {
  185. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  186. // }
  187. // // 批量撤销订单
  188. // async fn cancel_orders(&mut self) -> Result<Vec<Order>, Error> {
  189. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  190. // }
  191. //
  192. // async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> {
  193. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  194. // }
  195. //
  196. // async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<Value, Error> {
  197. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  198. // }
  199. //
  200. // async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<Value, Error> {
  201. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  202. // }
  203. //
  204. // // 设置持仓模式
  205. // async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
  206. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  207. // }
  208. //
  209. // // 更新双持仓模式下杠杆
  210. // async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> {
  211. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  212. // }
  213. //
  214. // async fn set_auto_deposit_status(&mut self, _status: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "bitmart:该交易所方法未实现".to_string())) }
  215. //
  216. // // 交易账户互转
  217. // async fn wallet_transfers(&mut self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> {
  218. // Err(Error::new(ErrorKind::NotFound, "bitmart_swap:该交易所方法未实现".to_string()))
  219. // }
  220. // }