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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use resources::{Amount, AssetIdentifier};
use std::fmt;
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
pub struct PriceRatio {
#[serde(rename = "n")]
numerator: u64,
#[serde(rename = "d")]
denominator: u64,
}
impl fmt::Display for PriceRatio {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "n: {}, d: {}", self.numerator(), self.denominator())
}
}
impl PriceRatio {
pub fn new(numerator: u64, denominator: u64) -> Self {
Self {
numerator,
denominator,
}
}
pub fn numerator(&self) -> u64 {
self.numerator
}
pub fn denominator(&self) -> u64 {
self.denominator
}
}
#[derive(Deserialize, Debug)]
pub struct OfferSummary {
amount: Amount,
#[serde(rename = "price_r")]
price_ratio: PriceRatio,
price: Amount,
}
impl OfferSummary {
pub fn amount(&self) -> Amount {
self.amount
}
pub fn price_ratio(&self) -> PriceRatio {
self.price_ratio
}
pub fn price(&self) -> Amount {
self.price
}
}
#[cfg(test)]
mod offer_summary_tests {
use super::*;
use serde_json;
fn offer_summary_json() -> &'static str {
include_str!("../../fixtures/offer_summary.json")
}
#[test]
fn it_parses_an_offer_summary_from_json() {
let offer_summary: OfferSummary = serde_json::from_str(&offer_summary_json()).unwrap();
assert_eq!(offer_summary.price_ratio().numerator, 100000000);
assert_eq!(offer_summary.price(), Amount::new(77200005));
assert_eq!(offer_summary.amount(), Amount::new(120000000));
}
}
#[derive(Deserialize, Debug, Clone)]
pub struct Offer {
id: i64,
paging_token: String,
seller: String,
selling: AssetIdentifier,
buying: AssetIdentifier,
amount: Amount,
#[serde(rename = "price_r")]
price_ratio: PriceRatio,
price: Amount,
}
impl Offer {
pub fn id(&self) -> i64 {
self.id
}
pub fn paging_token(&self) -> &str {
&self.paging_token
}
pub fn seller(&self) -> &str {
&self.seller
}
pub fn selling(&self) -> &AssetIdentifier {
&self.selling
}
pub fn buying(&self) -> &AssetIdentifier {
&self.buying
}
pub fn price_ratio(&self) -> (u64, u64) {
(self.price_ratio.numerator, self.price_ratio.denominator)
}
pub fn amount(&self) -> Amount {
self.amount
}
pub fn price(&self) -> Amount {
self.price
}
}
#[cfg(test)]
mod offer_tests {
use super::*;
use serde_json;
fn offer_json() -> &'static str {
include_str!("../../fixtures/offer.json")
}
#[test]
fn it_parses_an_offer_from_json() {
let offer: Offer = serde_json::from_str(&offer_json()).unwrap();
assert_eq!(offer.id(), 121);
assert_eq!(offer.paging_token(), "121");
assert_eq!(offer.selling().code(), "BAR");
assert_eq!(offer.buying().code(), "FOO");
assert_eq!(offer.price_ratio(), (387, 50));
assert_eq!(offer.amount(), Amount::new(236_692_509));
assert_eq!(offer.price(), Amount::new(77_400_000));
}
}