Skip to content

Commit cba4c51

Browse files
authored
Make price/conf fields private with public unchecked accessor (#26)
* Make price/conf fields private with public unchecked accessor
1 parent 4dffeb3 commit cba4c51

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

pyth-sdk-solana/src/state.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use solana_program::pubkey::Pubkey;
1616
use std::mem::size_of;
1717

1818
pub use pyth_sdk::{
19-
PriceFeed,
2019
Price,
20+
PriceFeed,
2121
PriceStatus,
2222
};
2323

@@ -345,18 +345,18 @@ impl PriceAccount {
345345
status = PriceStatus::Unknown;
346346
}
347347

348-
PriceFeed {
349-
id: price_key.to_bytes(),
350-
price: self.agg.price,
351-
conf: self.agg.conf,
348+
PriceFeed::new(
349+
price_key.to_bytes(),
352350
status,
353-
max_num_publishers: self.num,
354-
num_publishers: self.num_qt,
355-
ema_price: self.ema_price.val,
356-
ema_conf: self.ema_conf.val as u64,
357-
expo: self.expo,
358-
product_id: self.prod.val,
359-
}
351+
self.expo,
352+
self.num,
353+
self.num_qt,
354+
self.prod.val,
355+
self.agg.price,
356+
self.agg.conf,
357+
self.ema_price.val,
358+
self.ema_conf.val as u64,
359+
)
360360
}
361361
}
362362

pyth-sdk/src/lib.rs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ impl Default for PriceStatus {
6464
pub struct PriceFeed {
6565
/// Unique identifier for this price.
6666
pub id: PriceIdentifier,
67-
/// The current price.
68-
pub price: i64,
69-
/// Confidence interval around the price.
70-
pub conf: u64,
7167
/// Status of price (Trading is valid).
7268
pub status: PriceStatus,
7369
/// Price exponent.
@@ -76,18 +72,49 @@ pub struct PriceFeed {
7672
pub max_num_publishers: u32,
7773
/// Number of publishers that made up current aggregate.
7874
pub num_publishers: u32,
79-
/// Exponentially moving average price.
80-
pub ema_price: i64,
81-
/// Exponentially moving average confidence interval.
82-
pub ema_conf: u64,
8375
/// Product account key.
8476
pub product_id: ProductIdentifier,
77+
/// The current price.
78+
price: i64,
79+
/// Confidence interval around the price.
80+
conf: u64,
81+
/// Exponentially moving average price.
82+
ema_price: i64,
83+
/// Exponentially moving average confidence interval.
84+
ema_conf: u64,
8585
}
8686

8787
impl PriceFeed {
88+
/// Constructs a new Price Feed
89+
pub fn new(
90+
id: PriceIdentifier,
91+
status: PriceStatus,
92+
expo: i32,
93+
max_num_publishers: u32,
94+
num_publishers: u32,
95+
product_id: ProductIdentifier,
96+
price: i64,
97+
conf: u64,
98+
ema_price: i64,
99+
ema_conf: u64,
100+
) -> PriceFeed {
101+
PriceFeed {
102+
id,
103+
price,
104+
conf,
105+
status,
106+
expo,
107+
max_num_publishers,
108+
num_publishers,
109+
ema_price,
110+
ema_conf,
111+
product_id,
112+
}
113+
}
114+
88115
/// Get the current price and confidence interval as fixed-point numbers of the form a *
89116
/// 10^e.
90-
///
117+
///
91118
/// Returns a struct containing the current price, confidence interval, and the exponent for
92119
/// both numbers. Returns `None` if price information is currently unavailable for any
93120
/// reason.
@@ -103,9 +130,24 @@ impl PriceFeed {
103130
}
104131
}
105132

133+
/// Get the "unchecked" current price and confidence interval as fixed-point numbers of the form
134+
/// a * 10^e.
135+
///
136+
/// Returns a struct containing the current price, confidence interval, and the exponent for
137+
/// both numbers. This method returns the price value without checking availability of the
138+
/// price. This value might not be valid or updated when the price is not available.
139+
/// Please use `get_current_price` where possible.
140+
pub fn get_current_price_unchecked(&self) -> Price {
141+
Price {
142+
price: self.price,
143+
conf: self.conf,
144+
expo: self.expo,
145+
}
146+
}
147+
106148
/// Get the exponential moving average price (ema_price) and a confidence interval on the
107149
/// result.
108-
///
150+
///
109151
/// Returns `None` if the ema price is currently unavailable.
110152
/// At the moment, the confidence interval returned by this method is computed in
111153
/// a somewhat questionable way, so we do not recommend using it for high-value applications.
@@ -117,4 +159,22 @@ impl PriceFeed {
117159
expo: self.expo,
118160
})
119161
}
162+
163+
/// Get the "unchecked" exponential moving average price (ema_price) and a confidence interval
164+
/// on the result.
165+
///
166+
/// Returns the price value without checking availability of the price.
167+
/// This value might not be valid or updated when the price is not available.
168+
/// Please use `get_ema_price` where possible.
169+
///
170+
/// At the moment, the confidence interval returned by this method is computed in
171+
/// a somewhat questionable way, so we do not recommend using it for high-value applications.
172+
pub fn get_ema_price_unchecked(&self) -> Price {
173+
// This method currently cannot return None, but may do so in the future.
174+
Price {
175+
price: self.ema_price,
176+
conf: self.ema_conf,
177+
expo: self.expo,
178+
}
179+
}
120180
}

0 commit comments

Comments
 (0)