Skip to content

Commit 46f6365

Browse files
authored
Add eth price example (#9)
* Add eth price example
1 parent c69e3d2 commit 46f6365

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

pyth-sdk-solana/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The pyth.network website also lists the public keys of the accounts (e.g., [Cryp
4343
This crate provides utilities for interpreting and manipulating the content of these accounts.
4444
Applications can obtain the content of these accounts in two different ways:
4545
* On-chain programs should pass these accounts to the instructions that require price feeds.
46-
* Off-chain programs can access these accounts using the Solana RPC client (as in the [example program](examples/get_accounts.rs)).
46+
* Off-chain programs can access these accounts using the Solana RPC client (as in the [eth price example program](examples/eth_price.rs)).
4747

4848
In both cases, the content of the account will be provided to the application as a binary blob (`Vec<u8>`).
4949
The examples below assume that the user has already obtained this account data.
@@ -139,6 +139,23 @@ Run the following commands to try this example program:
139139

140140
```
141141
cargo build --examples
142+
cargo run --example eth_price
143+
```
144+
145+
The output of this command is price of ETH/USD over time, such as:
146+
147+
```
148+
.....ETH/USD.....
149+
status .......... Trading
150+
num_publishers .. 19
151+
price ........... 291958500000 x 10^-8
152+
conf ............ 163920000 x 10^-8
153+
twap ............ 291343470000 x 10^-8
154+
twac ............ 98874533 x 10^-8
155+
```
156+
157+
For an example of using Solana Account structure please run:
158+
```
142159
cargo run --example get_accounts
143160
```
144161

pyth-sdk-solana/examples/eth_price.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// example usage of reading pyth price from solana price account
2+
3+
use pyth_sdk_solana::load_price;
4+
use solana_client::rpc_client::RpcClient;
5+
use solana_program::pubkey::Pubkey;
6+
use std::str::FromStr;
7+
use std::{
8+
thread,
9+
time,
10+
};
11+
12+
13+
fn main() {
14+
let url = "http://api.mainnet-beta.solana.com";
15+
// Pyth eth/usd price account on mainnet. can be found from https://pyth.network
16+
let key = "JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB";
17+
let clnt = RpcClient::new(url.to_string());
18+
let eth_price_key = Pubkey::from_str(key).unwrap();
19+
20+
loop {
21+
// get price data from key
22+
let eth_price_data = clnt.get_account_data(&eth_price_key).unwrap();
23+
let eth_price = load_price(&eth_price_data).unwrap();
24+
25+
println!(".....ETH/USD.....");
26+
println!("status .......... {:?}", eth_price.status);
27+
println!("num_publishers .. {}", eth_price.num_publishers);
28+
29+
let maybe_price = eth_price.get_current_price();
30+
match maybe_price {
31+
Some(p) => {
32+
println!("price ........... {} x 10^{}", p.price, p.expo);
33+
println!("conf ............ {} x 10^{}", p.conf, p.expo);
34+
}
35+
None => {
36+
println!("price ........... unavailable");
37+
println!("conf ............ unavailable");
38+
}
39+
}
40+
41+
42+
let maybe_twap = eth_price.get_twap();
43+
match maybe_twap {
44+
Some(twap) => {
45+
println!("twap ............ {} x 10^{}", twap.price, twap.expo);
46+
println!("twac ............ {} x 10^{}", twap.conf, twap.expo);
47+
}
48+
None => {
49+
println!("twap ............ unavailable");
50+
println!("twac ............ unavailable");
51+
}
52+
}
53+
54+
println!("");
55+
56+
thread::sleep(time::Duration::from_secs(1));
57+
}
58+
}

0 commit comments

Comments
 (0)