|
| 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(ð_price_key).unwrap(); |
| 23 | + let eth_price = load_price(ð_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