phemex_swap_handle.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use std::str::FromStr;
  2. use rust_decimal::Decimal;
  3. use rust_decimal::prelude::FromPrimitive;
  4. use serde_json::Value;
  5. use exchanges::response_base::ResponseData;
  6. use crate::{OrderBook, Trade, Record};
  7. pub fn handle_records(value: &Value) -> Vec<Record> {
  8. let mut records = vec![];
  9. let symbol = value["symbol"].as_str().unwrap().replace("USDT", "_USDT").replace("u1000", "1000");
  10. for record_value in value["kline_p"].as_array().unwrap() {
  11. records.push(Record {
  12. time: Decimal::from_i64(record_value[0].as_i64().unwrap() * 1000).unwrap(),
  13. open: Decimal::from_str(record_value[3].as_str().unwrap()).unwrap(),
  14. high: Decimal::from_str(record_value[4].as_str().unwrap()).unwrap(),
  15. low: Decimal::from_str(record_value[5].as_str().unwrap()).unwrap(),
  16. close: Decimal::from_str(record_value[6].as_str().unwrap()).unwrap(),
  17. volume: Decimal::from_str(record_value[7].as_str().unwrap()).unwrap(),
  18. symbol: symbol.clone(),
  19. });
  20. }
  21. return records;
  22. }
  23. pub fn format_depth_items(value: &Value) -> Vec<OrderBook> {
  24. let mut depth_items: Vec<OrderBook> = vec![];
  25. for value in value.as_array().unwrap() {
  26. depth_items.push(OrderBook {
  27. price: Decimal::from_str(value[0].as_str().unwrap()).unwrap(),
  28. amount: Decimal::from_str(value[1].as_str().unwrap()).unwrap(),
  29. })
  30. }
  31. return depth_items;
  32. }
  33. pub fn format_trade_items(res_data: &ResponseData) -> Vec<Trade> {
  34. let result = res_data.data["trades_p"].as_array().unwrap();
  35. let mut trades = vec![];
  36. for item in result {
  37. let side = item[1] == "Buy";
  38. let size = Decimal::from_str(item[3].as_str().unwrap()).unwrap();
  39. trades.push(Trade {
  40. id: item[0].to_string(),
  41. time: Decimal::from_i64(item[0].as_i64().unwrap() / 1000 / 1000).unwrap(),
  42. size: if side { size } else { -size },
  43. price: Decimal::from_str(item[2].as_str().unwrap().to_string().as_str()).unwrap(),
  44. symbol: res_data.data["symbol"].as_str().unwrap().replace("USDT", "_USDT").replace("u1000", "1000"),
  45. })
  46. }
  47. return trades;
  48. }