Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion program/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ thiserror = "1.0"
num-derive = "0.3"
num-traits = "0.2"
byteorder = "1.4.3"
serde = { version = "1.0", features = ["derive"], optional = true }
strum = { version = "0.24.1", features = ["derive"], optional = true }

[dev-dependencies]
solana-program-test = "=1.13.3"
Expand All @@ -30,7 +32,6 @@ serde_json = "1.0"
test-generator = "0.3.1"
csv = "1.1"


[features]
debug = []
library = []
Expand Down
3 changes: 3 additions & 0 deletions program/rust/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ mod permission;
mod price;
mod product;

// Some types only exist during use as a library.
#[cfg(feature = "strum")]
pub use price::MessageType;
#[cfg(test)]
pub use product::{
account_has_key_values,
Expand Down
35 changes: 35 additions & 0 deletions program/rust/src/accounts/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ impl PythAccount for PriceAccountV2 {
/// Once we start using the unused structs and methods, the contract size will increase.

#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "strum",
derive(strum::EnumDiscriminants),
strum_discriminants(name(MessageType)),
strum_discriminants(vis(pub)),
strum_discriminants(derive(
Hash,
strum::EnumIter,
strum::EnumString,
strum::IntoStaticStr,
strum::ToString,
)),
cfg_attr(
feature = "serde",
strum_discriminants(derive(serde::Serialize, serde::Deserialize))
)
)]
pub enum Message {
PriceFeedMessage(PriceFeedMessage),
TwapMessage(TwapMessage),
Expand All @@ -248,16 +266,32 @@ impl Message {
_ => Err(OracleError::DeserializationError),
}
}

pub fn to_bytes(self) -> Vec<u8> {
match self {
Self::PriceFeedMessage(msg) => msg.to_bytes().to_vec(),
Self::TwapMessage(msg) => msg.to_bytes().to_vec(),
}
}

pub fn publish_time(&self) -> i64 {
match self {
Self::PriceFeedMessage(msg) => msg.publish_time,
Self::TwapMessage(msg) => msg.publish_time,
}
}

pub fn id(&self) -> [u8; 32] {
match self {
Self::PriceFeedMessage(msg) => msg.id,
Self::TwapMessage(msg) => msg.id,
}
}
}

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PriceFeedMessage {
pub id: [u8; 32],
pub price: i64,
Expand Down Expand Up @@ -409,6 +443,7 @@ impl PriceFeedMessage {
/// Message format for sending Twap data via the accumulator program
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TwapMessage {
pub id: [u8; 32],
pub cumulative_price: i128,
Expand Down
2 changes: 2 additions & 0 deletions program/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mod log;
// While we have `pyth-sdk-rs` which exposes a more friendly interface, this is still useful when a
// downstream user wants to confirm for example that they can compile against the binary interface
// of this program for their specific solana version.
#[cfg(feature = "strum")]
pub use accounts::MessageType;
#[cfg(feature = "library")]
pub use accounts::{
AccountHeader,
Expand Down