cointr_swap.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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::cointr_swap_rest::CointrSwapRest;
  11. // use rust_decimal::prelude::FromPrimitive;
  12. //
  13. // #[allow(dead_code)]
  14. // #[derive(Clone)]
  15. // pub struct CointrSwap {
  16. // exchange: ExchangeEnum,
  17. // symbol: String,
  18. // is_colo: bool,
  19. // params: BTreeMap<String, String>,
  20. // request: CointrSwapRest,
  21. // market: Market,
  22. // order_sender: Sender<Order>,
  23. // error_sender: Sender<Error>,
  24. // }
  25. //
  26. // impl CointrSwap {
  27. // pub async fn new(symbol: String, is_colo: bool, params: BTreeMap<String, String>, order_sender: Sender<Order>, error_sender: Sender<Error>) -> CointrSwap {
  28. // let market = Market::new();
  29. // let mut cointr_swap = CointrSwap {
  30. // exchange: ExchangeEnum::CointrSwap,
  31. // symbol: symbol.to_uppercase(),
  32. // is_colo,
  33. // params: params.clone(),
  34. // request: CointrSwapRest::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 = cointr_swap.set_dual_mode(symbol_array[1], true).await;
  43. // match mode_result {
  44. // Ok(ok) => {
  45. // info!("Cointr:设置持仓模式成功!{:?}", ok);
  46. // }
  47. // Err(error) => {
  48. // error!("Cointr:设置持仓模式失败!mode_result={}", error)
  49. // }
  50. // }
  51. // // 获取市场信息
  52. // cointr_swap.market = CointrSwap::get_market(&mut cointr_swap).await.unwrap_or(cointr_swap.market);
  53. // return cointr_swap;
  54. // }
  55. // }
  56. //
  57. // #[async_trait]
  58. // impl Platform for CointrSwap {
  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::CointrSwap
  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, "cointr_swap:该交易所方法未实现".to_string()))
  87. // }
  88. // // 获取账号信息
  89. // async fn get_account(&mut self) -> Result<Account, Error> {
  90. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  91. // }
  92. //
  93. // async fn get_spot_account(&mut self) -> Result<Vec<Account>, Error> {
  94. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  95. // }
  96. //
  97. // // 获取持仓信息
  98. // async fn get_position(&mut self) -> Result<Vec<Position>, Error> {
  99. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  100. // }
  101. // // 获取所有持仓
  102. // async fn get_positions(&mut self) -> Result<Vec<Position>, Error> {
  103. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  104. // }
  105. // // 获取市场行情
  106. // async fn get_ticker(&mut self) -> Result<Ticker, Error> {
  107. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  108. // }
  109. //
  110. // async fn get_ticker_symbol(&mut self, _symbol: String) -> Result<Ticker, Error> {
  111. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  112. // }
  113. //
  114. // async fn get_market(&mut self) -> Result<Market, Error> {
  115. // let symbol_format = utils::format_symbol(self.symbol.clone(), "");
  116. // let params = json!({});
  117. // let res_data = self.request.get_market(params).await;
  118. // if res_data.code == 200 {
  119. // let res_data_json = res_data.data["data"].as_array().unwrap();
  120. // let market_info = res_data_json.iter().find(|item| item["instId"].as_str().unwrap() == symbol_format);
  121. // match market_info {
  122. // None => {
  123. // error!("cointr_swap:获取Market信息错误!\nget_market:res_data={:?}", res_data);
  124. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  125. // }
  126. // Some(value) => {
  127. // let symbol = value["instId"].as_str().unwrap().to_string();
  128. // let base_asset = value["baseCcy"].as_str().unwrap().to_string();
  129. // let quote_asset = value["quoteCcy"].as_str().unwrap().to_string();
  130. //
  131. // let tick_size = Decimal::from_str(value["tickSz"].as_str().unwrap()).unwrap();
  132. // let step: Vec<&str> = value["steps"].as_str().unwrap().split(",").collect();
  133. // let amount_size = tick_size * Decimal::from_str(step[0]).unwrap();
  134. // let price_precision = Decimal::from_str(value["pxPrecision"].as_str().unwrap()).unwrap();
  135. // let amount_precision = Decimal::from_u32(amount_size.scale()).unwrap();
  136. // let min_qty = Decimal::from_str(value["minSz"].as_str().unwrap()).unwrap();
  137. // let max_qty = Decimal::from_str(value["maxSz"].as_str().unwrap()).unwrap();
  138. // let min_notional = min_qty;
  139. // let max_notional = max_qty;
  140. // let ct_val = Decimal::from_str(value["ctVal"].as_str().unwrap()).unwrap();
  141. //
  142. // let result = Market {
  143. // symbol,
  144. // base_asset,
  145. // quote_asset,
  146. // tick_size,
  147. // amount_size,
  148. // price_precision,
  149. // amount_precision,
  150. // min_qty,
  151. // max_qty,
  152. // min_notional,
  153. // max_notional,
  154. // ct_val,
  155. // };
  156. // Ok(result)
  157. // }
  158. // }
  159. // } else {
  160. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  161. // }
  162. // }
  163. //
  164. // async fn get_market_symbol(&mut self, symbol: String) -> Result<Market, Error> {
  165. // let symbol_format = format!("PERP_{}", symbol.clone().to_uppercase());
  166. // let params = json!({});
  167. // let res_data = self.request.get_market(params).await;
  168. // if res_data.code == 200 {
  169. // let res_data_json = res_data.data["rows"].as_array().unwrap();
  170. // let market_info = res_data_json.iter().find(|item| item["symbol"].as_str().unwrap() == symbol_format);
  171. // match market_info {
  172. // None => {
  173. // error!("cointr_swap:获取Market信息错误!\nget_market:res_data={:?}", res_data);
  174. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  175. // }
  176. // Some(value) => {
  177. // let symbol = value["symbol"].as_str().unwrap().to_string().replace("PERP_", "");
  178. // let symbol_array: Vec<&str> = symbol.split("_").collect();
  179. // let base_asset = symbol_array[0].to_string();
  180. // let quote_asset = symbol_array[1].to_string();
  181. //
  182. // let tick_size = Decimal::from_str(value["quote_min"].as_str().unwrap()).unwrap();
  183. // let amount_size = Decimal::from_str(&value["base_min"].as_str().unwrap()).unwrap();
  184. // let price_precision = Decimal::from_u32(tick_size.scale()).unwrap();
  185. // let amount_precision = Decimal::from_u32(amount_size.scale()).unwrap();
  186. // let min_qty = Decimal::from_str(value["quote_min"].as_str().unwrap()).unwrap();
  187. // let max_qty = Decimal::from_str(value["quote_max"].as_str().unwrap()).unwrap();
  188. // let min_notional = Decimal::from_str(value["min_notional"].as_str().unwrap()).unwrap();
  189. // let max_notional = max_qty;
  190. // let ct_val = Decimal::ONE;
  191. //
  192. // let result = Market {
  193. // symbol,
  194. // base_asset,
  195. // quote_asset,
  196. // tick_size,
  197. // amount_size,
  198. // price_precision,
  199. // amount_precision,
  200. // min_qty,
  201. // max_qty,
  202. // min_notional,
  203. // max_notional,
  204. // ct_val,
  205. // };
  206. // Ok(result)
  207. // }
  208. // }
  209. // } else {
  210. // Err(Error::new(ErrorKind::Other, res_data.to_string()))
  211. // }
  212. // }
  213. //
  214. // // 获取订单详情
  215. // async fn get_order_detail(&mut self, _order_id: &str, _custom_id: &str) -> Result<Order, Error> {
  216. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  217. // }
  218. // // 获取订单列表
  219. // async fn get_orders_list(&mut self, _status: &str) -> Result<Vec<Order>, Error> {
  220. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  221. // }
  222. // // 下单接口
  223. // async fn take_order(&mut self, _custom_id: &str, _origin_side: &str, _price: Decimal, _amount: Decimal) -> Result<Order, Error> {
  224. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  225. // }
  226. //
  227. // 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> {
  228. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  229. // }
  230. //
  231. // // 撤销订单
  232. // async fn cancel_order(&mut self, _order_id: &str, _custom_id: &str) -> Result<Order, Error> {
  233. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  234. // }
  235. // // 批量撤销订单
  236. // async fn cancel_orders(&mut self) -> Result<Vec<Order>, Error> {
  237. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  238. // }
  239. //
  240. // async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> {
  241. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  242. // }
  243. //
  244. // async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<Value, Error> {
  245. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  246. // }
  247. //
  248. // async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<Value, Error> {
  249. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  250. // }
  251. //
  252. // // 设置持仓模式
  253. // async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
  254. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  255. // }
  256. //
  257. // // 更新双持仓模式下杠杆
  258. // async fn set_dual_leverage(&mut self, _leverage: &str) -> Result<String, Error> {
  259. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  260. // }
  261. //
  262. // async fn set_auto_deposit_status(&mut self, _status: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "cointr:该交易所方法未实现".to_string())) }
  263. //
  264. // // 交易账户互转
  265. // async fn wallet_transfers(&mut self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> {
  266. // Err(Error::new(ErrorKind::NotFound, "cointr_swap:该交易所方法未实现".to_string()))
  267. // }
  268. // }