use std::str::FromStr; use rust_decimal::Decimal; use serde_json::Value; use exchanges::response_base::ResponseData; use crate::{Trade, Record, OrderBook}; pub fn handle_records(value: &Value) -> Vec { let mut records = vec![]; let n = value["n"].as_str().unwrap().to_string(); let n_split: Vec = n.split("_").map(|s| s.to_string()).collect(); let symbol = format!("{}_{}", n_split[n_split.len() - 2], n_split[n_split.len() - 1]); records.push(Record { time: Decimal::from_str(value["t"].as_str().unwrap()).unwrap() * Decimal::ONE_THOUSAND, open: Decimal::from_str(value["o"].as_str().unwrap()).unwrap(), high: Decimal::from_str(value["h"].as_str().unwrap()).unwrap(), low: Decimal::from_str(value["l"].as_str().unwrap()).unwrap(), close: Decimal::from_str(value["c"].as_str().unwrap()).unwrap(), volume: Decimal::from_str(value["v"].as_str().unwrap()).unwrap(), symbol, }); return records; } pub fn format_depth_items(value: &Value) -> Vec { let mut depth_items: Vec = vec![]; for value in value.as_array().unwrap() { let info = value.as_array().unwrap(); depth_items.push(OrderBook { price: Decimal::from_str(info[0].as_str().unwrap()).unwrap(), amount: Decimal::from_str(info[1].as_str().unwrap()).unwrap(), }) } return depth_items; } pub fn format_trade_items(res_data: &ResponseData) -> Vec { let side = res_data.data["side"].as_str().unwrap(); let amount = Decimal::from_str(res_data.data["amount"].as_str().unwrap()).unwrap(); return vec![Trade { id: res_data.data["id"].to_string(), time: Decimal::from_str(res_data.data["create_time_ms"].as_str().unwrap()).unwrap().floor(), size: if side == "buy" { amount } else { -amount }, price: Decimal::from_str(res_data.data["price"].as_str().unwrap().to_string().as_str()).unwrap(), symbol: res_data.data["currency_pair"].as_str().unwrap().to_string(), }]; }