3
3
4
4
use {
5
5
self :: subscriber:: Subscriber ,
6
+ super :: key_store:: KeyStore ,
6
7
crate :: agent:: store:: global,
7
8
anyhow:: {
8
9
anyhow,
18
19
Deserialize ,
19
20
Serialize ,
20
21
} ,
21
- serde_with:: {
22
- serde_as,
23
- DisplayFromStr ,
24
- } ,
25
22
slog:: Logger ,
26
23
solana_client:: nonblocking:: rpc_client:: RpcClient ,
27
24
solana_sdk:: {
@@ -65,6 +62,9 @@ pub type PriceAccount = pyth_sdk_solana::state::PriceAccount;
65
62
pub struct Oracle {
66
63
config : Config ,
67
64
65
+ // The Key Store
66
+ key_store : KeyStore ,
67
+
68
68
// The Solana account data
69
69
data : Data ,
70
70
@@ -85,18 +85,11 @@ pub struct Oracle {
85
85
logger : Logger ,
86
86
}
87
87
88
- #[ serde_as]
89
88
#[ derive( Clone , Serialize , Deserialize , Debug ) ]
90
89
#[ serde( default ) ]
91
90
pub struct Config {
92
91
/// The commitment level to use when reading data from the RPC node.
93
92
pub commitment : CommitmentLevel ,
94
- /// Public key of the Oracle program.
95
- #[ serde_as( as = "DisplayFromStr" ) ]
96
- pub oracle_account_key : Pubkey ,
97
- /// Public key of the root mapping account.
98
- #[ serde_as( as = "DisplayFromStr" ) ]
99
- pub mapping_account_key : Pubkey ,
100
93
/// RPC endpoint to send requests to.
101
94
pub rpc_url : String ,
102
95
/// The interval with which to poll account information.
@@ -114,8 +107,6 @@ impl Default for Config {
114
107
fn default ( ) -> Self {
115
108
Self {
116
109
commitment : CommitmentLevel :: Confirmed ,
117
- oracle_account_key : Default :: default ( ) ,
118
- mapping_account_key : Default :: default ( ) ,
119
110
rpc_url : "http://localhost:8899" . to_string ( ) ,
120
111
poll_interval_duration : Duration :: from_secs ( 30 ) ,
121
112
subscriber_enabled : true ,
@@ -127,6 +118,7 @@ impl Default for Config {
127
118
128
119
pub fn spawn_oracle (
129
120
config : Config ,
121
+ key_store : KeyStore ,
130
122
global_store_update_tx : mpsc:: Sender < global:: Update > ,
131
123
logger : Logger ,
132
124
) -> Vec < JoinHandle < ( ) > > {
@@ -135,12 +127,23 @@ pub fn spawn_oracle(
135
127
// Create and spawn the account subscriber
136
128
let ( updates_tx, updates_rx) = mpsc:: channel ( config. updates_channel_capacity ) ;
137
129
if config. subscriber_enabled {
138
- let subscriber = Subscriber :: new ( config. subscriber . clone ( ) , updates_tx, logger. clone ( ) ) ;
130
+ let subscriber = Subscriber :: new (
131
+ config. subscriber . clone ( ) ,
132
+ key_store. program_key . clone ( ) ,
133
+ updates_tx,
134
+ logger. clone ( ) ,
135
+ ) ;
139
136
jhs. push ( tokio:: spawn ( async move { subscriber. run ( ) . await } ) ) ;
140
137
}
141
138
142
139
// Create and spawn the Oracle
143
- let mut oracle = Oracle :: new ( config, updates_rx, global_store_update_tx, logger) ;
140
+ let mut oracle = Oracle :: new (
141
+ config,
142
+ key_store,
143
+ updates_rx,
144
+ global_store_update_tx,
145
+ logger,
146
+ ) ;
144
147
jhs. push ( tokio:: spawn ( async move { oracle. run ( ) . await } ) ) ;
145
148
146
149
jhs
@@ -149,6 +152,7 @@ pub fn spawn_oracle(
149
152
impl Oracle {
150
153
pub fn new (
151
154
config : Config ,
155
+ key_store : KeyStore ,
152
156
updates_rx : mpsc:: Receiver < ( Pubkey , solana_sdk:: account:: Account ) > ,
153
157
global_store_tx : mpsc:: Sender < global:: Update > ,
154
158
logger : Logger ,
@@ -163,6 +167,7 @@ impl Oracle {
163
167
164
168
Oracle {
165
169
config,
170
+ key_store,
166
171
data : Default :: default ( ) ,
167
172
rpc_client,
168
173
poll_interval,
@@ -200,7 +205,7 @@ impl Oracle {
200
205
. cloned ( )
201
206
. collect :: < HashSet < _ > > ( ) ;
202
207
self . data . mapping_accounts = self
203
- . fetch_mapping_accounts ( self . config . mapping_account_key )
208
+ . fetch_mapping_accounts ( self . key_store . mapping_key )
204
209
. await ?;
205
210
info ! ( self . logger, "fetched mapping accounts" ; "new" => format!( "{:?}" , self
206
211
. data
@@ -449,10 +454,6 @@ mod subscriber {
449
454
Deserialize ,
450
455
Serialize ,
451
456
} ,
452
- serde_with:: {
453
- serde_as,
454
- DisplayFromStr ,
455
- } ,
456
457
slog:: Logger ,
457
458
solana_sdk:: {
458
459
account:: Account ,
@@ -469,29 +470,23 @@ mod subscriber {
469
470
} ,
470
471
} ;
471
472
472
- #[ serde_as]
473
473
#[ derive( Clone , Serialize , Deserialize , Debug ) ]
474
474
#[ serde( default ) ]
475
475
pub struct Config {
476
476
/// Commitment level used to read account data
477
- pub commitment : CommitmentLevel ,
478
- /// Public key of the root account to monitor. Note that all
479
- /// accounts owned by this account are also monitored.
480
- #[ serde_as( as = "DisplayFromStr" ) ]
481
- pub account_key : Pubkey ,
477
+ pub commitment : CommitmentLevel ,
482
478
/// HTTP RPC endpoint
483
- pub rpc_url : String ,
479
+ pub rpc_url : String ,
484
480
/// WSS RPC endpoint
485
- pub wss_url : String ,
481
+ pub wss_url : String ,
486
482
}
487
483
488
484
impl Default for Config {
489
485
fn default ( ) -> Self {
490
486
Self {
491
- commitment : CommitmentLevel :: Confirmed ,
492
- account_key : Default :: default ( ) ,
493
- rpc_url : "http://localhost:8899" . to_string ( ) ,
494
- wss_url : "ws://localhost:8900" . to_string ( ) ,
487
+ commitment : CommitmentLevel :: Confirmed ,
488
+ rpc_url : "http://localhost:8899" . to_string ( ) ,
489
+ wss_url : "ws://localhost:8900" . to_string ( ) ,
495
490
}
496
491
}
497
492
}
@@ -501,7 +496,11 @@ mod subscriber {
501
496
pub struct Subscriber {
502
497
config : Config ,
503
498
504
- // Channel on which updates are sent
499
+ /// Public key of the root account to monitor. Note that all
500
+ /// accounts owned by this account are also monitored.
501
+ account_key : Pubkey ,
502
+
503
+ /// Channel on which updates are sent
505
504
updates_tx : mpsc:: Sender < ( Pubkey , solana_sdk:: account:: Account ) > ,
506
505
507
506
logger : Logger ,
@@ -510,11 +509,13 @@ mod subscriber {
510
509
impl Subscriber {
511
510
pub fn new (
512
511
config : Config ,
512
+ account_key : Pubkey ,
513
513
updates_tx : mpsc:: Sender < ( Pubkey , solana_sdk:: account:: Account ) > ,
514
514
logger : Logger ,
515
515
) -> Self {
516
516
Subscriber {
517
517
config,
518
+ account_key,
518
519
updates_tx,
519
520
logger,
520
521
}
@@ -549,7 +550,7 @@ mod subscriber {
549
550
& self ,
550
551
) -> Result < broadcast:: Receiver < ( Pubkey , solana_sdk:: account:: Account ) > > {
551
552
let shadow = BlockchainShadow :: new_for_program (
552
- & self . config . account_key ,
553
+ & self . account_key ,
553
554
SyncOptions {
554
555
network : solana_shadow:: Network :: Custom (
555
556
self . config . rpc_url . clone ( ) ,
0 commit comments