-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy patheth_price.rs
72 lines (61 loc) · 2.27 KB
/
eth_price.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// example usage of reading pyth price from solana/pythnet price account
use pyth_sdk_solana::state::SolanaPriceAccount;
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use std::str::FromStr;
use std::time::{
SystemTime,
UNIX_EPOCH,
};
use std::{
thread,
time,
};
fn main() {
let url = "http:/pythnet.rpcpool.com";
// Pyth ETH/USD price account on pythnet (can be found on https://pyth.network/developers/price-feed-ids#solana-mainnet-beta which has the same price feed IDs as pythnet)
let key = "JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB";
let clnt = RpcClient::new(url.to_string());
let eth_price_key = Pubkey::from_str(key).unwrap();
loop {
// get price data from key
let mut eth_price_account = clnt.get_account(ð_price_key).unwrap();
let eth_price_feed =
SolanaPriceAccount::account_to_feed(ð_price_key, &mut eth_price_account).unwrap();
println!(".....ETH/USD.....");
let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let maybe_price = eth_price_feed.get_price_no_older_than(current_time, 60);
match maybe_price {
Some(p) => {
println!("price ........... {} x 10^{}", p.price, p.expo);
println!("conf ............ {} x 10^{}", p.conf, p.expo);
}
None => {
println!("price ........... unavailable");
println!("conf ............ unavailable");
}
}
let maybe_ema_price = eth_price_feed.get_ema_price_no_older_than(current_time, 60);
match maybe_ema_price {
Some(ema_price) => {
println!(
"ema_price ....... {} x 10^{}",
ema_price.price, ema_price.expo
);
println!(
"ema_conf ........ {} x 10^{}",
ema_price.conf, ema_price.expo
);
}
None => {
println!("ema_price ....... unavailable");
println!("ema_conf ........ unavailable");
}
}
println!();
thread::sleep(time::Duration::from_secs(1));
}
}