Skip to content

Commit 1bdac0d

Browse files
authored
Improvements to Message in the Oracle Program (#375)
* feat: library features and Message improvements - Adds strum/serde during library use - Adds methods to extract id/publish_time from any message. * fix: incorrect strum version, upgrade
1 parent d9b915c commit 1bdac0d

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program/rust/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ thiserror = "1.0"
1515
num-derive = "0.3"
1616
num-traits = "0.2"
1717
byteorder = "1.4.3"
18+
serde = { version = "1.0", features = ["derive"], optional = true }
19+
strum = { version = "0.24.1", features = ["derive"], optional = true }
1820

1921
[dev-dependencies]
2022
solana-program-test = "=1.13.3"
@@ -30,7 +32,6 @@ serde_json = "1.0"
3032
test-generator = "0.3.1"
3133
csv = "1.1"
3234

33-
3435
[features]
3536
debug = []
3637
library = []

program/rust/src/accounts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ mod permission;
4040
mod price;
4141
mod product;
4242

43+
// Some types only exist during use as a library.
44+
#[cfg(feature = "strum")]
45+
pub use price::MessageType;
4346
#[cfg(test)]
4447
pub use product::{
4548
account_has_key_values,

program/rust/src/accounts/price.rs

+35
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ impl PythAccount for PriceAccountV2 {
230230
/// Once we start using the unused structs and methods, the contract size will increase.
231231
232232
#[derive(Debug, Copy, Clone, PartialEq)]
233+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
234+
#[cfg_attr(
235+
feature = "strum",
236+
derive(strum::EnumDiscriminants),
237+
strum_discriminants(name(MessageType)),
238+
strum_discriminants(vis(pub)),
239+
strum_discriminants(derive(
240+
Hash,
241+
strum::EnumIter,
242+
strum::EnumString,
243+
strum::IntoStaticStr,
244+
strum::ToString,
245+
)),
246+
cfg_attr(
247+
feature = "serde",
248+
strum_discriminants(derive(serde::Serialize, serde::Deserialize))
249+
)
250+
)]
233251
pub enum Message {
234252
PriceFeedMessage(PriceFeedMessage),
235253
TwapMessage(TwapMessage),
@@ -248,16 +266,32 @@ impl Message {
248266
_ => Err(OracleError::DeserializationError),
249267
}
250268
}
269+
251270
pub fn to_bytes(self) -> Vec<u8> {
252271
match self {
253272
Self::PriceFeedMessage(msg) => msg.to_bytes().to_vec(),
254273
Self::TwapMessage(msg) => msg.to_bytes().to_vec(),
255274
}
256275
}
276+
277+
pub fn publish_time(&self) -> i64 {
278+
match self {
279+
Self::PriceFeedMessage(msg) => msg.publish_time,
280+
Self::TwapMessage(msg) => msg.publish_time,
281+
}
282+
}
283+
284+
pub fn id(&self) -> [u8; 32] {
285+
match self {
286+
Self::PriceFeedMessage(msg) => msg.id,
287+
Self::TwapMessage(msg) => msg.id,
288+
}
289+
}
257290
}
258291

259292
#[repr(C)]
260293
#[derive(Debug, Copy, Clone, PartialEq)]
294+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
261295
pub struct PriceFeedMessage {
262296
pub id: [u8; 32],
263297
pub price: i64,
@@ -409,6 +443,7 @@ impl PriceFeedMessage {
409443
/// Message format for sending Twap data via the accumulator program
410444
#[repr(C)]
411445
#[derive(Debug, Copy, Clone, PartialEq)]
446+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
412447
pub struct TwapMessage {
413448
pub id: [u8; 32],
414449
pub cumulative_price: i128,

program/rust/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ mod log;
2323
// While we have `pyth-sdk-rs` which exposes a more friendly interface, this is still useful when a
2424
// downstream user wants to confirm for example that they can compile against the binary interface
2525
// of this program for their specific solana version.
26+
#[cfg(feature = "strum")]
27+
pub use accounts::MessageType;
2628
#[cfg(feature = "library")]
2729
pub use accounts::{
2830
AccountHeader,

0 commit comments

Comments
 (0)