This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathget_accounts.rs
138 lines (121 loc) · 3.94 KB
/
get_accounts.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// example usage of pyth-client account structure
// bootstrap all product and pricing accounts from root mapping account
use pyth_client::{
PriceType,
PriceStatus,
CorpAction,
load_mapping,
load_product,
load_price
};
use solana_client::{
rpc_client::RpcClient
};
use solana_program::{
pubkey::Pubkey
};
use std::{
str::FromStr
};
fn get_price_type( ptype: &PriceType ) -> &'static str
{
match ptype {
PriceType::Unknown => "unknown",
PriceType::Price => "price",
}
}
fn get_status( st: &PriceStatus ) -> &'static str
{
match st {
PriceStatus::Unknown => "unknown",
PriceStatus::Trading => "trading",
PriceStatus::Halted => "halted",
PriceStatus::Auction => "auction",
}
}
fn get_corp_act( cact: &CorpAction ) -> &'static str
{
match cact {
CorpAction::NoCorpAct => "nocorpact",
}
}
fn main() {
// get pyth mapping account
let url = "http://api.devnet.solana.com";
let key = "BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2";
let clnt = RpcClient::new( url.to_string() );
let mut akey = Pubkey::from_str( key ).unwrap();
loop {
// get Mapping account from key
let map_data = clnt.get_account_data( &akey ).unwrap();
let map_acct = load_mapping( &map_data ).unwrap();
// iget and print each Product in Mapping directory
let mut i = 0;
for prod_akey in &map_acct.products {
let prod_pkey = Pubkey::new( &prod_akey.val );
let prod_data = clnt.get_account_data( &prod_pkey ).unwrap();
let prod_acct = load_product( &prod_data ).unwrap();
// print key and reference data for this Product
println!( "product_account .. {:?}", prod_pkey );
for (key, val) in prod_acct.iter() {
if key.len() > 0 {
println!( " {:.<16} {}", key, val );
}
}
// print all Prices that correspond to this Product
if prod_acct.px_acc.is_valid() {
let mut px_pkey = Pubkey::new( &prod_acct.px_acc.val );
loop {
let pd = clnt.get_account_data( &px_pkey ).unwrap();
let pa = load_price( &pd ).unwrap();
println!( " price_account .. {:?}", px_pkey );
let maybe_price = pa.get_current_price();
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");
}
}
println!( " price_type ... {}", get_price_type(&pa.ptype));
println!( " exponent ..... {}", pa.expo );
println!( " status ....... {}", get_status(&pa.get_current_price_status()));
println!( " corp_act ..... {}", get_corp_act(&pa.agg.corp_act));
println!( " num_qt ....... {}", pa.num_qt );
println!( " valid_slot ... {}", pa.valid_slot );
println!( " publish_slot . {}", pa.agg.pub_slot );
let maybe_ema_price = pa.get_ema_price();
match maybe_ema_price {
Some(ema_price) => {
println!( " ema_price ......... {} x 10^{}", ema_price.price, ema_price.expo );
println!( " ema_confidence ......... {} x 10^{}", ema_price.conf, ema_price.expo );
}
None => {
println!( " ema_price ......... unavailable");
println!( " ema_confidence ......... unavailable");
}
}
// go to next price account in list
if pa.next.is_valid() {
px_pkey = Pubkey::new( &pa.next.val );
} else {
break;
}
}
}
// go to next product
i += 1;
if i == map_acct.num {
break;
}
}
// go to next Mapping account in list
if !map_acct.next.is_valid() {
break;
}
akey = Pubkey::new( &map_acct.next.val );
}
}