Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate ws;
- extern crate env_logger;
- extern crate serde;
- extern crate serde_json;
- #[macro_use] extern crate serde_derive;
- use ws::{connect, CloseCode};
- use std::collections::HashMap;
- use serde_json::{Value, Error};
- const WS_URL: &str = "wss://api2.poloniex.com";
- const HELLO_STRING: &str = r#"{ "command": "subscribe", "channel": "USDC_BTC" }"#;
- #[derive(Debug)]
- struct OrderBook {
- pair_name: String,
- pair_id: u64,
- bids: HashMap<String, String>,
- asks: HashMap<String, String>,
- }
- fn parse_poloniex_full_order_book(msg: &str) -> OrderBook{
- let f:Value = serde_json::from_str(msg).unwrap();
- let pair_id = f[0].as_u64().unwrap();
- let pair_name = f[2][0][1]["currencyPair"].as_str().unwrap();
- let asks = &f[2][0][1]["orderBook"][0].as_object().unwrap();
- let bids = &f[2][0][1]["orderBook"][1].as_object().unwrap();
- //println!("{:?}", asks);
- let asks: HashMap<String, String>= asks.iter().map(|(price, vol)|{
- (price.clone(), vol.as_str().unwrap().to_string())
- }).collect();
- let bids: HashMap<String, String>= bids.iter().map(|(price, vol)|{
- (price.clone(), vol.as_str().unwrap().to_string())
- }).collect();
- OrderBook {
- pair_name: pair_name.to_string(),
- pair_id,
- asks,
- bids,
- }
- }
- fn main() {
- env_logger::init();
- connect(WS_URL, |out| {
- out.send(HELLO_STRING).unwrap();
- move |msg: ws::Message| {
- let mut order_book = parse_poloniex_full_order_book(msg.as_text().unwrap());
- println!("{:?}", order_book);
- out.close(CloseCode::Normal)
- }
- }).unwrap()
- }
Add Comment
Please, Sign In to add comment