Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
soulmachine committed Jul 14, 2024
1 parent 4f451b8 commit ce60534
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
29 changes: 13 additions & 16 deletions crypto-msg-parser/src/exchanges/bitfinex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,24 @@ pub(crate) fn parse_l2(
Ok(vec![orderbook])
}



// See https://docs.bitfinex.com/reference/rest-public-book
// See https://api-pub.bitfinex.com/v2/book/{symbol}/{precision}
// request example https://api-pub.bitfinex.com/v2/book/tBTCUSD/P0?len=100
//[[68361,2,0.17328582],[68360,1,0.65244918],[68357,1,0.07]]
//price ,count ,amount
// See https://binance-docs.github.io/apidocs/spot/en/#order-book


pub(crate) fn parse_l2_snapshot(
market_type: MarketType,
msg: &str,
symbol:Option<&str>,
received_at: Option<i64>
symbol: Option<&str>,
received_at: Option<i64>,
) -> Result<Vec<OrderBookMsg>, SimpleError> {
let rs_msg = serde_json::from_str::<Vec<[Value;3]>>(msg)
.map_err(|_e| SimpleError::new(format!("Failed to deserialize {msg} to Vec<[Value;3]]>")))?;

let s=symbol.unwrap_or("").to_string();
let rs_msg = serde_json::from_str::<Vec<[Value; 3]>>(msg).map_err(|_e| {
SimpleError::new(format!("Failed to deserialize {msg} to Vec<[Value;3]]>"))
})?;

let s = symbol.unwrap_or("").to_string();
let pair = crypto_pair::normalize_pair(&s, EXCHANGE_NAME)
.ok_or_else(|| SimpleError::new(format!("Failed to normalize {s} from {msg}")))?;

Expand All @@ -243,8 +241,8 @@ pub(crate) fn parse_l2_snapshot(
Order { price, quantity_base, quantity_quote, quantity_contract }
};

let mut asks=Vec::new();
let mut bids=Vec::new();
let mut asks = Vec::new();
let mut bids = Vec::new();

for raw_order in rs_msg.iter() {
let order = parse_order(raw_order);
Expand All @@ -254,26 +252,25 @@ pub(crate) fn parse_l2_snapshot(
asks.push(order);
}
}
let orderbook = OrderBookMsg {

let orderbook = OrderBookMsg {
exchange: EXCHANGE_NAME.to_string(),
market_type,
symbol: s,
pair: pair.clone(),
msg_type: MessageType::L2Snapshot,
timestamp:received_at.unwrap(),
timestamp: received_at.unwrap(),
seq_id: None,
prev_seq_id: None,
asks: asks.clone(),
bids: bids.clone(),
snapshot,
json: msg.to_string(),
};
};

Ok(vec![orderbook])
}


fn parse_one_candle(
market_type: MarketType,
symbol: &str,
Expand Down
2 changes: 1 addition & 1 deletion crypto-msg-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub fn parse_l2_snapshot(
) -> Result<Vec<OrderBookMsg>, SimpleError> {
let ret = match exchange {
"binance" => exchanges::binance::parse_l2_snapshot(market_type, msg, symbol, received_at),
"bitfinex" => exchanges::bitfinex::parse_l2_snapshot(market_type, msg, symbol,received_at),
"bitfinex" => exchanges::bitfinex::parse_l2_snapshot(market_type, msg, symbol, received_at),
"bitget" => exchanges::bitget::parse_l2_snapshot(market_type, msg, symbol),
_ => Err(SimpleError::new(format!("Unknown exchange {exchange}"))),
};
Expand Down
31 changes: 17 additions & 14 deletions crypto-msg-parser/tests/bitfinex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,18 +505,20 @@ mod l2_snapshot {

#[test]
fn spot() {

/* data sample excerpt
1677628803683 1677628803683 [[23144,3,0.87789],[23142,3,0.27991505],[23141,4,0.48547974],
[23325,5,-0.00297898],[23326,1,-0.00060965],[23327,2,-0.00119078]]
*/
/* data sample excerpt
1677628803683 1677628803683 [[23144,3,0.87789],[23142,3,0.27991505],[23141,4,0.48547974],
[23325,5,-0.00297898],[23326,1,-0.00060965],[23327,2,-0.00119078]]
*/

//let raw_msg = r#"[[30428,1,0.01],[30426,1,0.1],[30424,1,0.2954],[30423,1,0.3333],[30422,3,0.72231346],[30420,2,0.3349],[30416,2,0.29700845],[30415,3,0.482257],[30414,1,0.4],[30413,1,0.15439084]]"#;
//let raw_msg =
// r#"[[30428,1,0.01],[30426,1,0.1],[30424,1,0.2954],[30423,1,0.3333],[30422,3,
// 0.72231346],[30420,2,0.3349],[30416,2,0.29700845],[30415,3,0.482257],[30414,
// 1,0.4],[30413,1,0.15439084]]"#;
let raw_msg = r#"[[23144,3,0.87789],[23142,3,0.27991505],[23141,4,0.48547974],[23325,5,-0.00297898],[23326,1,-0.00060965],[23327,2,-0.00119078]]"#;
let received_at=Some(1677628803683) ;
let received_at = Some(1677628803683);
assert_eq!("NONE", extract_symbol(EXCHANGE_NAME, MarketType::Spot, raw_msg).unwrap());
assert_eq!(None, extract_timestamp(EXCHANGE_NAME, MarketType::Spot, raw_msg).unwrap());

let orderbook = &parse_l2_snapshot(
EXCHANGE_NAME,
MarketType::Spot,
Expand Down Expand Up @@ -544,13 +546,12 @@ mod l2_snapshot {
assert_eq!(orderbook.seq_id, None);
assert_eq!(orderbook.prev_seq_id, None);
//"bids":[23144,3,0.87789],[23142,3,0.27991505],[23141,4,0.48547974] descending


assert_eq!(orderbook.bids[0].price, 23144.0);
assert_eq!(orderbook.bids[0].quantity_base, 0.87789);
assert_eq!(orderbook.bids[0].quantity_quote, 23144.0 * 0.87789);
assert_eq!(orderbook.bids[0].quantity_contract, None);

assert_eq!(orderbook.bids[2].price, 23141.0);
assert_eq!(orderbook.bids[2].quantity_base, 0.48547974);
assert_eq!(orderbook.bids[2].quantity_quote, 23141.0 * 0.48547974);
Expand All @@ -569,13 +570,15 @@ mod l2_snapshot {

#[test]
fn linear_swap() {

/*
1677628819111 1677628819111 [[23143,5,0.4721613],[23142,3,0.22947044],[23141,2,0.6234],[23906,1,-0.005],[23920,1,-0.0026],[23923,1,-0.02]]
*/
//let raw_msg = r#"[[28293,1,0.0350506],[28291,1,0.0526735],[28289,2,0.1037385],[28287,1,0.1059222],[28285,1,0.1324028],[28284,1,0.1765371],[28282,1,0.2206713],[28280,1,0.2427385],[28277,1,0.2648056]]"#;
//let raw_msg =
// r#"[[28293,1,0.0350506],[28291,1,0.0526735],[28289,2,0.1037385],[28287,1,0.
// 1059222],[28285,1,0.1324028],[28284,1,0.1765371],[28282,1,0.2206713],[28280,
// 1,0.2427385],[28277,1,0.2648056]]"#;
let raw_msg = r#"[[23143,5,0.4721613],[23142,3,0.22947044],[23141,2,0.6234],[23906,1,-0.005],[23920,1,-0.0026],[23923,1,-0.02]]"#;
let received_at=Some(1677628819111) ;
let received_at = Some(1677628819111);
assert_eq!("NONE", extract_symbol(EXCHANGE_NAME, MarketType::LinearSwap, raw_msg).unwrap());
assert_eq!(
None,
Expand All @@ -587,7 +590,7 @@ mod l2_snapshot {
MarketType::Spot,
raw_msg,
Some("tBTCF0:USTF0"),
received_at
received_at,
)
.unwrap()[0];

Expand Down

0 comments on commit ce60534

Please sign in to comment.