Skip to content

Commit 8908f70

Browse files
committed
Update docs
1 parent d06d9cf commit 8908f70

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

examples/sol-anchor-contract/programs/example-sol-anchor-contract/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct QueryRequest<'info> {
3535
pub mod example_sol_anchor_contract {
3636
use super::*;
3737

38+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
39+
3840
pub fn init(ctx: Context<InitRequest>, config: AdminConfig) -> Result<()> {
3941
ctx.accounts.config.set_inner(config);
4042
Ok(())
@@ -53,7 +55,7 @@ pub mod example_sol_anchor_contract {
5355
// https://docs.pyth.network/consume-data/best-practices
5456
let current_timestamp1 = Clock::get()?.unix_timestamp;
5557
let loan_price = loan_feed
56-
.get_price_no_older_than(current_timestamp1, 60)
58+
.get_price_no_older_than(current_timestamp1, STALENESS_THRESHOLD)
5759
.ok_or(ErrorCode::PythOffline)?;
5860
let loan_max_price = loan_price
5961
.price

pyth-sdk-solana/README.md

Lines changed: 12 additions & 4 deletions
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 website can be used to identify the public keys of each price feed's price account (e.g., [Crypto.BTC/USD accounts](https://pyth.network/developers/price-feed-ids#solana-mainnet-beta)).
3030

3131
### On-chain
3232

@@ -37,9 +37,11 @@ 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+
let current_timestamp = Clock::get()?.unix_timestamp;
44+
let current_price: Price = price_feed.get_price_no_older_than(current_timestamp, STALENESS_THRESHOLD).unwrap();
4345
println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
4446
```
4547

@@ -58,10 +60,16 @@ The `load_price_feed_from_account` function will construct a `PriceFeed` struct
5860
```rust
5961
use pyth_sdk_solana::{load_price_feed_from_account, PriceFeed};
6062

63+
const STALENESS_THRESHOLD : u64 = 60; // staleness threshold in seconds
64+
let current_time = SystemTime::now()
65+
.duration_since(UNIX_EPOCH)
66+
.unwrap()
67+
.as_secs() as i64;
68+
6169
let price_key: Pubkey = ...;
62-
let mut price_account: Account = ...;
70+
let mut price_account: Account = clnt.get_account(&price_key).unwrap();
6371
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();
72+
let current_price: Price = price_feed.get_price_no_older_than(current_time, STALENESS_THRESHOLD).unwrap();
6573
println!("price: ({} +- {}) x 10^{}", current_price.price, current_price.conf, current_price.expo);
6674
```
6775

pyth-sdk-solana/src/state.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,15 @@ 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. checks to make sure price
229+
/// is valid.
229230
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.
231+
/// status of price (Trading is valid)
233232
pub status: PriceStatus,
234233
/// notification of any corporate action
235234
pub corp_act: CorpAction,

pyth-sdk/README.md

Lines changed: 7 additions & 3 deletions
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)