pyth_lazer_protocol/publisher.rs
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
//! WebSocket JSON protocol types for API the publisher provides to the router.
//! Publisher data sourcing may also be implemented in the router process,
//! eliminating WebSocket overhead.
use {
super::router::{Price, PriceFeedId, TimestampUs},
serde::{Deserialize, Serialize},
};
/// Represents a binary (bincode-serialized) stream update sent
/// from the publisher to the router.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PriceFeedData {
pub price_feed_id: PriceFeedId,
/// Timestamp of the last update provided by the source of the prices
/// (like an exchange). If unavailable, this value is set to `publisher_timestamp_us`.
pub source_timestamp_us: TimestampUs,
/// Timestamp of the last update provided by the publisher.
pub publisher_timestamp_us: TimestampUs,
/// Last known value of the best executable price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub price: Option<Price>,
/// Last known value of the best bid price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub best_bid_price: Option<Price>,
/// Last known value of the best ask price of this price feed.
/// `None` if no value is currently available.
#[serde(with = "crate::serde_price_as_i64")]
pub best_ask_price: Option<Price>,
}
#[test]
fn price_feed_data_serde() {
let data = [
1, 0, 0, 0, // price_feed_id
2, 0, 0, 0, 0, 0, 0, 0, // source_timestamp_us
3, 0, 0, 0, 0, 0, 0, 0, // publisher_timestamp_us
4, 0, 0, 0, 0, 0, 0, 0, // price
5, 0, 0, 0, 0, 0, 0, 0, // best_bid_price
6, 2, 0, 0, 0, 0, 0, 0, // best_ask_price
];
let expected = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: Some(Price(5.try_into().unwrap())),
best_ask_price: Some(Price((2 * 256 + 6).try_into().unwrap())),
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data).unwrap(),
expected
);
assert_eq!(bincode::serialize(&expected).unwrap(), data);
let data2 = [
1, 0, 0, 0, // price_feed_id
2, 0, 0, 0, 0, 0, 0, 0, // source_timestamp_us
3, 0, 0, 0, 0, 0, 0, 0, // publisher_timestamp_us
4, 0, 0, 0, 0, 0, 0, 0, // price
0, 0, 0, 0, 0, 0, 0, 0, // best_bid_price
0, 0, 0, 0, 0, 0, 0, 0, // best_ask_price
];
let expected2 = PriceFeedData {
price_feed_id: PriceFeedId(1),
source_timestamp_us: TimestampUs(2),
publisher_timestamp_us: TimestampUs(3),
price: Some(Price(4.try_into().unwrap())),
best_bid_price: None,
best_ask_price: None,
};
assert_eq!(
bincode::deserialize::<PriceFeedData>(&data2).unwrap(),
expected2
);
assert_eq!(bincode::serialize(&expected2).unwrap(), data2);
}