1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use resources::{offer::OfferSummary, AssetIdentifier};
#[derive(Deserialize, Debug)]
pub struct Orderbook {
bids: Vec<OfferSummary>,
asks: Vec<OfferSummary>,
base: AssetIdentifier,
counter: AssetIdentifier,
}
impl Orderbook {
pub fn bids(&self) -> &Vec<OfferSummary> {
&self.bids
}
pub fn asks(&self) -> &Vec<OfferSummary> {
&self.asks
}
pub fn base(&self) -> &AssetIdentifier {
&self.base
}
pub fn counter(&self) -> &AssetIdentifier {
&self.counter
}
}
#[cfg(test)]
mod orderbook_tests {
use super::*;
use resources::Amount;
use serde_json;
fn orderbook_json() -> &'static str {
include_str!("../../fixtures/orderbook.json")
}
#[test]
fn it_parses_an_orderbook_from_json() {
let orderbook: Orderbook = serde_json::from_str(&orderbook_json()).unwrap();
assert_eq!(
orderbook.bids().first().unwrap().amount(),
Amount::new(120_000_000)
);
assert_eq!(
orderbook.asks().first().unwrap().amount(),
Amount::new(2_384_804_125)
);
assert_eq!(orderbook.base().code(), "XLM".to_string());
assert_eq!(orderbook.counter().code(), "FOO".to_string());
}
}