Skip to content

Commit 42efedf

Browse files
authored
Merge pull request #100 from pyth-network/guibescos/remove-mentions-of-get-current-price
Remove mentions of get_current_price
2 parents d06d9cf + f00da0e commit 42efedf

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Key features of this SDK include:
77
* Get the current price of over [50 products](https://pyth.network/markets/), including cryptocurrencies,
88
US equities, forex and more.
99
* Combine listed products to create new price feeds, e.g., for baskets of tokens or non-USD quote currencies.
10-
* Consume prices in Solana programs, CosmWasm smart contracts, or off-chain applications.
10+
* Consume prices in Solana programs or off-chain applications.
1111

1212
Please see the [pyth.network documentation](https://docs.pyth.network/) for more information about pyth.network.
1313

@@ -18,7 +18,6 @@ This repository is divided into several crates focused on specific use cases:
1818
1. [Pyth SDK](pyth-sdk) provides common data types and interfaces for that are shared across different blockchains.
1919
2. [Pyth SDK Solana](pyth-sdk-solana) provides an interface for reading Pyth price feeds in Solana programs.
2020
This crate may also be used in off-chain applications that read prices from the Solana blockchain.
21-
3. [Pyth SDK CosmWasm](pyth-sdk-cw) provides an interface for reading Pyth price feeds in on-chain CosmWasm contracts.
2221

2322
Please see the documentation for the relevant crate to get started using Pyth Network.
2423

pyth-sdk-solana/README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Applications can obtain the content of these accounts in two different ways:
2626
* On-chain programs should pass these accounts to the instructions that require price feeds.
2727
* Off-chain programs can access these accounts using the Solana RPC client (as in the [eth price example program](examples/eth_price.rs)).
2828

29-
The pyth.network website can be used to identify the public keys of the various Pyth Network accounts (e.g., [Crypto.BTC/USD accounts](https://pyth.network/markets/#Crypto.BTC/USD)).
29+
The [pyth.network](https://pyth.network/developers/price-feed-ids#solana-mainnet-beta) website can be used to identify the public keys of each price feed's price account (e.g. Crypto.BTC/USD).
3030

3131
### On-chain
3232

@@ -37,15 +37,19 @@ The `load_price_feed_from_account_info` function will construct a `PriceFeed` st
3737
```rust
3838
use pyth_sdk_solana::{load_price_feed_from_account_info, PriceFeed};
3939

40+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
4041
let price_account_info: AccountInfo = ...;
4142
let price_feed: PriceFeed = load_price_feed_from_account_info( &price_account_info ).unwrap();
42-
let current_price: Price = price_feed.get_current_price().unwrap();
43-
println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
43+
let current_timestamp = Clock::get()?.unix_timestamp;
44+
let current_price: Price = price_feed.get_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).unwrap();
45+
msg!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
4446
```
4547

4648
The `PriceFeed` object returned by `load_price_feed_from_account_info` contains all currently-available pricing information about the product.
4749
This struct also has some useful functions for manipulating and combining prices; see the [common SDK documentation](../pyth-sdk) for more details.
4850

51+
The function `get_price_no_older_than` takes in an `age` in seconds. If the current on-chain aggregate is older than `current_timestamp - age`, `get_price_no_older_than` will return `None`.
52+
4953
Note that your application should also validate the address of the passed-in price account before using it.
5054
Otherwise, an attacker could pass in a different account and set the price to an arbitrary value.
5155

@@ -58,10 +62,16 @@ The `load_price_feed_from_account` function will construct a `PriceFeed` struct
5862
```rust
5963
use pyth_sdk_solana::{load_price_feed_from_account, PriceFeed};
6064

65+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
66+
let current_time = SystemTime::now()
67+
.duration_since(UNIX_EPOCH)
68+
.unwrap()
69+
.as_secs() as i64;
70+
6171
let price_key: Pubkey = ...;
62-
let mut price_account: Account = ...;
72+
let mut price_account: Account = clnt.get_account(&price_key).unwrap();
6373
let price_feed: PriceFeed = load_price_feed_from_account( &price_key, &mut price_account ).unwrap();
64-
let current_price: Price = price_feed.get_current_price().unwrap();
74+
let current_price: Price = price_feed.get_price_no_older_than(current_time, STALENESS_THRESHOLD).unwrap();
6575
println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
6676
```
6777

pyth-sdk-solana/src/state.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,14 @@ unsafe impl Pod for ProductAccount {
220220
#[repr(C)]
221221
pub struct PriceInfo {
222222
/// the current price.
223-
/// For the aggregate price use price.get_current_price() whenever possible. It has more checks
224-
/// to make sure price is valid.
223+
/// For the aggregate price use `get_price_no_older_than()` whenever possible. Accessing fields
224+
/// directly might expose you to stale or invalid prices.
225225
pub price: i64,
226226
/// confidence interval around the price.
227-
/// For the aggregate confidence use price.get_current_price() whenever possible. It has more
228-
/// checks to make sure price is valid.
227+
/// For the aggregate confidence use `get_price_no_older_than()` whenever possible. Accessing
228+
/// fields directly might expose you to stale or invalid prices.
229229
pub conf: u64,
230-
/// status of price (Trading is valid).
231-
/// For the aggregate status use price.get_current_status() whenever possible.
232-
/// Price data can sometimes go stale and the function handles the status in such cases.
230+
/// status of price (Trading is valid)
233231
pub status: PriceStatus,
234232
/// notification of any corporate action
235233
pub corp_act: CorpAction,

pyth-sdk/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Pyth Network Common Rust SDK
22

33
This crate contains Pyth Network data structures that are shared across all Rust-based consumers of Pyth Network data.
4-
This crate is typically used in combination with a platform-specific crate such as [pyth-sdk-solana](../pyth-sdk-solana) or [pyth-sdk-cw](../pyth-sdk-cw).
4+
This crate is typically used in combination with a platform-specific crate such as [pyth-sdk-solana](../pyth-sdk-solana).
55

66
## Usage
77

@@ -19,7 +19,9 @@ Once you have a `PriceFeed`, you can call one of the methods below to get the pr
1919
Get the current price of the product from its `PriceFeed`:
2020

2121
```rust
22-
let current_price: Price = price_feed.get_current_price().ok_or(StdError::not_found("Current price is not available"))?;
22+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
23+
let current_timestamp = ...;
24+
let current_price: Price = price_feed.get_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).ok_or(StdError::not_found("Current price is not available"))?;
2325
println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
2426
```
2527

@@ -35,7 +37,9 @@ Please see the [consumer best practices guide](https://docs.pyth.network/consume
3537
The EMA price can be retrieved as follows:
3638

3739
```rust
38-
let ema_price: Price = price_feed.get_ema_price().ok_or(StdError::not_found("EMA price is not available"))?;
40+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
41+
let current_timestamp = ...;
42+
let ema_price: Price = price_feed.get_ema_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).ok_or(StdError::not_found("EMA price is not available"))?;
3943
println!("price: ({} +- {}) x 10^{}", ema_price.price, ema_price.conf, ema_price.expo);
4044
```
4145

0 commit comments

Comments
 (0)