@@ -18,13 +18,64 @@ pub struct PriceFeedData {
18
18
pub source_timestamp_us : TimestampUs ,
19
19
/// Timestamp of the last update provided by the publisher.
20
20
pub publisher_timestamp_us : TimestampUs ,
21
- /// Last known value of the "main" (?) price of this price feed.
21
+ /// Last known value of the best executable price of this price feed.
22
22
/// `None` if no value is currently available.
23
+ #[ serde( with = "crate::serde_price_as_i64" ) ]
23
24
pub price : Option < Price > ,
24
25
/// Last known value of the best bid price of this price feed.
25
26
/// `None` if no value is currently available.
27
+ #[ serde( with = "crate::serde_price_as_i64" ) ]
26
28
pub best_bid_price : Option < Price > ,
27
29
/// Last known value of the best ask price of this price feed.
28
30
/// `None` if no value is currently available.
31
+ #[ serde( with = "crate::serde_price_as_i64" ) ]
29
32
pub best_ask_price : Option < Price > ,
30
33
}
34
+
35
+ #[ test]
36
+ fn price_feed_data_serde ( ) {
37
+ let data = [
38
+ 1 , 0 , 0 , 0 , // price_feed_id
39
+ 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // source_timestamp_us
40
+ 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // publisher_timestamp_us
41
+ 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // price
42
+ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_bid_price
43
+ 6 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , // best_ask_price
44
+ ] ;
45
+
46
+ let expected = PriceFeedData {
47
+ price_feed_id : PriceFeedId ( 1 ) ,
48
+ source_timestamp_us : TimestampUs ( 2 ) ,
49
+ publisher_timestamp_us : TimestampUs ( 3 ) ,
50
+ price : Some ( Price ( 4 . try_into ( ) . unwrap ( ) ) ) ,
51
+ best_bid_price : Some ( Price ( 5 . try_into ( ) . unwrap ( ) ) ) ,
52
+ best_ask_price : Some ( Price ( ( 2 * 256 + 6 ) . try_into ( ) . unwrap ( ) ) ) ,
53
+ } ;
54
+ assert_eq ! (
55
+ bincode:: deserialize:: <PriceFeedData >( & data) . unwrap( ) ,
56
+ expected
57
+ ) ;
58
+ assert_eq ! ( bincode:: serialize( & expected) . unwrap( ) , data) ;
59
+
60
+ let data2 = [
61
+ 1 , 0 , 0 , 0 , // price_feed_id
62
+ 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // source_timestamp_us
63
+ 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // publisher_timestamp_us
64
+ 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // price
65
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_bid_price
66
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_ask_price
67
+ ] ;
68
+ let expected2 = PriceFeedData {
69
+ price_feed_id : PriceFeedId ( 1 ) ,
70
+ source_timestamp_us : TimestampUs ( 2 ) ,
71
+ publisher_timestamp_us : TimestampUs ( 3 ) ,
72
+ price : Some ( Price ( 4 . try_into ( ) . unwrap ( ) ) ) ,
73
+ best_bid_price : None ,
74
+ best_ask_price : None ,
75
+ } ;
76
+ assert_eq ! (
77
+ bincode:: deserialize:: <PriceFeedData >( & data2) . unwrap( ) ,
78
+ expected2
79
+ ) ;
80
+ assert_eq ! ( bincode:: serialize( & expected2) . unwrap( ) , data2) ;
81
+ }
0 commit comments