Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 6703266

Browse files
jayantkJayant Krishnamurthy
andauthored
Add a method to check status & get the current price (#5)
* add helper method * fix paren * update example and docs Co-authored-by: Jayant Krishnamurthy <[email protected]>
1 parent 0d2689f commit 6703266

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,37 @@ A rust API for desribing on-chain pyth account structures. A primer on pyth acc
44

55

66
Contains a library for use in on-chain program development and an off-chain example program for loading and printing product reference data and aggregate prices from all devnet pyth accounts.
7+
8+
### Running the Example
9+
10+
The example program prints the product reference data and current price information for Pyth on Solana devnet.
11+
Run the following commands to try this example program:
12+
13+
```
14+
cargo build --examples
15+
cargo run --example get_accounts
16+
```
17+
18+
The output of this command is a listing of Pyth's accounts, such as:
19+
20+
```
21+
product_account .. 6MEwdxe4g1NeAF9u6KDG14anJpFsVEa2cvr5H6iriFZ8
22+
symbol.......... SRM/USD
23+
asset_type...... Crypto
24+
quote_currency.. USD
25+
description..... SRM/USD
26+
generic_symbol.. SRMUSD
27+
base............ SRM
28+
price_account .. 992moaMQKs32GKZ9dxi8keyM2bUmbrwBZpK4p2K6X5Vs
29+
price ........ 7398000000
30+
conf ......... 3200000
31+
price_type ... price
32+
exponent ..... -9
33+
status ....... trading
34+
corp_act ..... nocorpact
35+
num_qt ....... 1
36+
valid_slot ... 91340924
37+
publish_slot . 91340925
38+
twap ......... 7426390900
39+
twac ......... 2259870
40+
```

examples/get_accounts.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,30 @@ fn main() {
107107
loop {
108108
let pd = clnt.get_account_data( &px_pkey ).unwrap();
109109
let pa = cast::<Price>( &pd );
110+
111+
let maybe_price = pa.get_current_price();
110112
assert_eq!( pa.magic, MAGIC, "not a valid pyth account" );
111113
assert_eq!( pa.atype, AccountType::Price as u32,
112114
"not a valid pyth price account" );
113115
assert_eq!( pa.ver, VERSION_2,
114116
"unexpected pyth price account version" );
115117
println!( " price_account .. {:?}", px_pkey );
118+
match maybe_price {
119+
Some((price, confidence, _)) => {
120+
println!(" price ........ {}", price);
121+
println!(" conf ......... {}", confidence);
122+
}
123+
None => {
124+
println!(" price ........ unavailable");
125+
println!(" conf ......... unavailable");
126+
}
127+
}
128+
116129
println!( " price_type ... {}", get_price_type(&pa.ptype));
117130
println!( " exponent ..... {}", pa.expo );
118131
println!( " status ....... {}", get_status(&pa.agg.status));
119132
println!( " corp_act ..... {}", get_corp_act(&pa.agg.corp_act));
120-
println!( " price ........ {}", pa.agg.price );
121-
println!( " conf ......... {}", pa.agg.conf );
133+
122134
println!( " num_qt ....... {}", pa.num_qt );
123135
println!( " valid_slot ... {}", pa.valid_slot );
124136
println!( " publish_slot . {}", pa.agg.pub_slot );

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ pub struct Price
131131
pub comp : [PriceComp;32] // price components one per quoter
132132
}
133133

134+
impl Price {
135+
/**
136+
* Get the current price and confidence interval as fixed-point numbers. Returns a triple of
137+
* the current price, confidence interval, and the exponent for both numbers (i.e., the number
138+
* of decimal places.)
139+
* For example:
140+
*
141+
* get_current_price() -> Some((12345, 267, -2)) // represents 123.45 +- 2.67
142+
* get_current_price() -> Some((123, 1, 2)) // represents 12300 +- 100
143+
*
144+
* Returns None if price information is currently unavailable.
145+
*/
146+
pub fn get_current_price(&self) -> Option<(i64, u64, i32)> {
147+
if !matches!(self.agg.status, PriceStatus::Trading) {
148+
None
149+
} else {
150+
Some((self.agg.price, self.agg.conf, self.expo))
151+
}
152+
}
153+
}
154+
134155
struct AccKeyU64
135156
{
136157
pub val: [u64;4]

0 commit comments

Comments
 (0)