coinsph_swap.rs 13 KB

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