33//! eliminating WebSocket overhead.
44
55use {
6- super :: router:: { Price , PriceFeedId , TimestampUs } ,
6+ super :: router:: { Price , PriceFeedId , Rate , TimestampUs } ,
77 derive_more:: derive:: From ,
88 serde:: { Deserialize , Serialize } ,
99} ;
1212/// from the publisher to the router.
1313#[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
1414#[ serde( rename_all = "camelCase" ) ]
15- pub struct PriceFeedData {
15+ pub struct PriceFeedDataV2 {
16+ pub price_feed_id : PriceFeedId ,
17+ /// Timestamp of the last update provided by the source of the prices
18+ /// (like an exchange). If unavailable, this value is set to `publisher_timestamp_us`.
19+ pub source_timestamp_us : TimestampUs ,
20+ /// Timestamp of the last update provided by the publisher.
21+ pub publisher_timestamp_us : TimestampUs ,
22+ /// Last known value of the best executable price of this price feed.
23+ /// `None` if no value is currently available.
24+ pub price : Option < Price > ,
25+ /// Last known value of the best bid price of this price feed.
26+ /// `None` if no value is currently available.
27+ pub best_bid_price : Option < Price > ,
28+ /// Last known value of the best ask price of this price feed.
29+ /// `None` if no value is currently available.
30+ pub best_ask_price : Option < Price > ,
31+ /// Last known value of the funding rate of this feed.
32+ /// `None` if no value is currently available.
33+ pub funding_rate : Option < Rate > ,
34+ }
35+
36+ /// Old Represents a binary (bincode-serialized) stream update sent
37+ /// from the publisher to the router.
38+ /// Superseded by `PriceFeedData`.
39+ #[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
40+ #[ serde( rename_all = "camelCase" ) ]
41+ pub struct PriceFeedDataV1 {
1642 pub price_feed_id : PriceFeedId ,
1743 /// Timestamp of the last update provided by the source of the prices
1844 /// (like an exchange). If unavailable, this value is set to `publisher_timestamp_us`.
@@ -33,6 +59,20 @@ pub struct PriceFeedData {
3359 pub best_ask_price : Option < Price > ,
3460}
3561
62+ impl From < PriceFeedDataV1 > for PriceFeedDataV2 {
63+ fn from ( v0 : PriceFeedDataV1 ) -> Self {
64+ Self {
65+ price_feed_id : v0. price_feed_id ,
66+ source_timestamp_us : v0. source_timestamp_us ,
67+ publisher_timestamp_us : v0. publisher_timestamp_us ,
68+ price : v0. price ,
69+ best_bid_price : v0. best_bid_price ,
70+ best_ask_price : v0. best_ask_price ,
71+ funding_rate : None ,
72+ }
73+ }
74+ }
75+
3676/// A response sent from the server to the publisher client.
3777/// Currently only serde errors are reported back to the client.
3878#[ derive( Debug , Clone , Serialize , Deserialize , From ) ]
@@ -49,7 +89,7 @@ pub struct UpdateDeserializationErrorResponse {
4989}
5090
5191#[ test]
52- fn price_feed_data_serde ( ) {
92+ fn price_feed_data_v1_serde ( ) {
5393 let data = [
5494 1 , 0 , 0 , 0 , // price_feed_id
5595 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // source_timestamp_us
@@ -59,7 +99,7 @@ fn price_feed_data_serde() {
5999 6 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , // best_ask_price
60100 ] ;
61101
62- let expected = PriceFeedData {
102+ let expected = PriceFeedDataV1 {
63103 price_feed_id : PriceFeedId ( 1 ) ,
64104 source_timestamp_us : TimestampUs ( 2 ) ,
65105 publisher_timestamp_us : TimestampUs ( 3 ) ,
@@ -68,7 +108,7 @@ fn price_feed_data_serde() {
68108 best_ask_price : Some ( Price ( ( 2 * 256 + 6 ) . try_into ( ) . unwrap ( ) ) ) ,
69109 } ;
70110 assert_eq ! (
71- bincode:: deserialize:: <PriceFeedData >( & data) . unwrap( ) ,
111+ bincode:: deserialize:: <PriceFeedDataV1 >( & data) . unwrap( ) ,
72112 expected
73113 ) ;
74114 assert_eq ! ( bincode:: serialize( & expected) . unwrap( ) , data) ;
@@ -81,16 +121,68 @@ fn price_feed_data_serde() {
81121 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_bid_price
82122 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_ask_price
83123 ] ;
84- let expected2 = PriceFeedData {
124+ let expected2 = PriceFeedDataV1 {
125+ price_feed_id : PriceFeedId ( 1 ) ,
126+ source_timestamp_us : TimestampUs ( 2 ) ,
127+ publisher_timestamp_us : TimestampUs ( 3 ) ,
128+ price : Some ( Price ( 4 . try_into ( ) . unwrap ( ) ) ) ,
129+ best_bid_price : None ,
130+ best_ask_price : None ,
131+ } ;
132+ assert_eq ! (
133+ bincode:: deserialize:: <PriceFeedDataV1 >( & data2) . unwrap( ) ,
134+ expected2
135+ ) ;
136+ assert_eq ! ( bincode:: serialize( & expected2) . unwrap( ) , data2) ;
137+ }
138+
139+ #[ test]
140+ fn price_feed_data_v2_serde ( ) {
141+ let data = [
142+ 1 , 0 , 0 , 0 , // price_feed_id
143+ 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // source_timestamp_us
144+ 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // publisher_timestamp_us
145+ 1 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // price
146+ 1 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // best_bid_price
147+ 1 , 6 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , // best_ask_price
148+ 0 , // funding_rate
149+ ] ;
150+
151+ let expected = PriceFeedDataV2 {
152+ price_feed_id : PriceFeedId ( 1 ) ,
153+ source_timestamp_us : TimestampUs ( 2 ) ,
154+ publisher_timestamp_us : TimestampUs ( 3 ) ,
155+ price : Some ( Price ( 4 . try_into ( ) . unwrap ( ) ) ) ,
156+ best_bid_price : Some ( Price ( 5 . try_into ( ) . unwrap ( ) ) ) ,
157+ best_ask_price : Some ( Price ( ( 2 * 256 + 6 ) . try_into ( ) . unwrap ( ) ) ) ,
158+ funding_rate : None ,
159+ } ;
160+ assert_eq ! (
161+ bincode:: deserialize:: <PriceFeedDataV2 >( & data) . unwrap( ) ,
162+ expected
163+ ) ;
164+ assert_eq ! ( bincode:: serialize( & expected) . unwrap( ) , data) ;
165+
166+ let data2 = [
167+ 1 , 0 , 0 , 0 , // price_feed_id
168+ 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // source_timestamp_us
169+ 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // publisher_timestamp_us
170+ 1 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , // price
171+ 0 , // best_bid_price
172+ 0 , // best_ask_price
173+ 1 , 7 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , // funding_rate
174+ ] ;
175+ let expected2 = PriceFeedDataV2 {
85176 price_feed_id : PriceFeedId ( 1 ) ,
86177 source_timestamp_us : TimestampUs ( 2 ) ,
87178 publisher_timestamp_us : TimestampUs ( 3 ) ,
88179 price : Some ( Price ( 4 . try_into ( ) . unwrap ( ) ) ) ,
89180 best_bid_price : None ,
90181 best_ask_price : None ,
182+ funding_rate : Some ( Rate ( 3 * 256 + 7 ) ) ,
91183 } ;
92184 assert_eq ! (
93- bincode:: deserialize:: <PriceFeedData >( & data2) . unwrap( ) ,
185+ bincode:: deserialize:: <PriceFeedDataV2 >( & data2) . unwrap( ) ,
94186 expected2
95187 ) ;
96188 assert_eq ! ( bincode:: serialize( & expected2) . unwrap( ) , data2) ;
0 commit comments