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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use borsh::{
BorshDeserialize,
BorshSerialize,
};
use hex::FromHexError;
use schemars::JsonSchema;
use std::fmt;
pub mod utils;
mod price;
pub use price::Price;
#[derive(
Copy,
Clone,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshSerialize,
BorshDeserialize,
serde::Serialize,
serde::Deserialize,
JsonSchema,
)]
#[repr(C)]
pub struct Identifier(
#[serde(with = "hex")]
#[schemars(with = "String")]
[u8; 32],
);
impl Identifier {
pub fn new(bytes: [u8; 32]) -> Identifier {
Identifier(bytes)
}
pub fn to_bytes(&self) -> [u8; 32] {
self.0
}
pub fn to_hex(&self) -> String {
hex::encode(self.0)
}
pub fn from_hex<T: AsRef<[u8]>>(s: T) -> Result<Identifier, FromHexError> {
let mut bytes = [0u8; 32];
hex::decode_to_slice(s, &mut bytes)?;
Ok(Identifier::new(bytes))
}
}
impl fmt::Debug for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{}", self.to_hex())
}
}
impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{}", self.to_hex())
}
}
impl AsRef<[u8]> for Identifier {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
pub type PriceIdentifier = Identifier;
pub type ProductIdentifier = Identifier;
pub type UnixTimestamp = i64;
pub type DurationInSeconds = u64;
#[derive(
Copy,
Clone,
Debug,
Default,
PartialEq,
Eq,
BorshSerialize,
BorshDeserialize,
serde::Serialize,
serde::Deserialize,
JsonSchema,
)]
#[repr(C)]
pub struct PriceFeed {
pub id: PriceIdentifier,
price: Price,
ema_price: Price,
}
impl PriceFeed {
#[allow(clippy::too_many_arguments)]
pub fn new(id: PriceIdentifier, price: Price, ema_price: Price) -> PriceFeed {
PriceFeed {
id,
price,
ema_price,
}
}
pub fn get_price_unchecked(&self) -> Price {
self.price
}
pub fn get_ema_price_unchecked(&self) -> Price {
self.ema_price
}
pub fn get_price_no_older_than(
&self,
current_time: UnixTimestamp,
age: DurationInSeconds,
) -> Option<Price> {
let price = self.get_price_unchecked();
let time_diff_abs = (price.publish_time - current_time).abs() as u64;
if time_diff_abs > age {
return None;
}
Some(price)
}
pub fn get_ema_price_no_older_than(
&self,
current_time: UnixTimestamp,
age: DurationInSeconds,
) -> Option<Price> {
let price = self.get_ema_price_unchecked();
let time_diff_abs = (price.publish_time - current_time).abs() as u64;
if time_diff_abs > age {
return None;
}
Some(price)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn test_ser_then_deser_default() {
let price_feed = PriceFeed::default();
let ser = serde_json::to_string(&price_feed).unwrap();
let deser: PriceFeed = serde_json::from_str(&ser).unwrap();
assert_eq!(price_feed, deser);
}
#[test]
pub fn test_ser_large_number() {
let price_feed = PriceFeed {
ema_price: Price {
conf: 1_234_567_000_000_000_789,
..Price::default()
},
..PriceFeed::default()
};
let price_feed_json = serde_json::to_value(price_feed).unwrap();
assert_eq!(
price_feed_json["ema_price"]["conf"].as_str(),
Some("1234567000000000789")
);
}
#[test]
pub fn test_deser_large_number() {
let mut price_feed_json = serde_json::to_value(PriceFeed::default()).unwrap();
price_feed_json["price"]["price"] =
serde_json::Value::String(String::from("1000000000000000123"));
let p: PriceFeed = serde_json::from_value(price_feed_json).unwrap();
assert_eq!(p.get_price_unchecked().price, 1_000_000_000_000_000_123);
}
#[test]
pub fn test_ser_id_length_32_bytes() {
let mut price_feed = PriceFeed::default();
price_feed.id.0[0] = 106; let price_feed_json = serde_json::to_value(price_feed).unwrap();
let id_str = price_feed_json["id"].as_str().unwrap();
assert_eq!(id_str.len(), 64);
assert_eq!(
id_str,
"6a00000000000000000000000000000000000000000000000000000000000000"
);
}
#[test]
pub fn test_deser_invalid_id_length_fails() {
let mut price_feed_json = serde_json::to_value(PriceFeed::default()).unwrap();
price_feed_json["id"] = serde_json::Value::String(String::from("1234567890"));
assert!(serde_json::from_value::<PriceFeed>(price_feed_json).is_err());
}
#[test]
pub fn test_identifier_from_hex_ok() {
let id = Identifier::from_hex(
"0a3f000000000000000000000000000000000000000000000000000000000000",
)
.unwrap();
assert_eq!(id.to_bytes()[0], 10);
}
#[test]
pub fn test_identifier_from_hex_invalid_err() {
let try_parse_odd = Identifier::from_hex("010"); assert_eq!(try_parse_odd, Err(FromHexError::OddLength));
let try_parse_invalid_len = Identifier::from_hex("0a"); assert_eq!(
try_parse_invalid_len,
Err(FromHexError::InvalidStringLength)
);
}
#[test]
pub fn test_identifier_debug_fmt() {
let mut id = Identifier::default();
id.0[0] = 10;
let id_str = format!("{:?}", id);
assert_eq!(
id_str,
"0x0a00000000000000000000000000000000000000000000000000000000000000"
);
}
#[test]
pub fn test_identifier_display_fmt() {
let mut id = Identifier::default();
id.0[0] = 10;
let id_str = format!("{}", id);
assert_eq!(
id_str,
"0x0a00000000000000000000000000000000000000000000000000000000000000"
);
}
}