-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathpyth_simulator.rs
590 lines (531 loc) · 20 KB
/
pyth_simulator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use {
crate::{
accounts::{
MappingAccount,
PERMISSIONS_SEED,
},
c_oracle_header::{
PC_PROD_ACC_SIZE,
PC_PTYPE_PRICE,
},
deserialize::load,
instruction::{
AddPriceArgs,
AddPublisherArgs,
CommandHeader,
OracleCommand,
UpdPermissionsArgs,
UpdPriceArgs,
},
},
bytemuck::{
bytes_of,
Pod,
},
serde::{
Deserialize,
Serialize,
},
solana_program::{
bpf_loader_upgradeable::{
self,
UpgradeableLoaderState,
},
clock::Clock,
hash::Hash,
instruction::{
AccountMeta,
Instruction,
},
native_token::LAMPORTS_PER_SOL,
pubkey::Pubkey,
rent::Rent,
stake_history::Epoch,
system_instruction,
system_program,
sysvar::SysvarId,
},
solana_program_test::{
read_file,
BanksClientError,
ProgramTest,
ProgramTestBanksClientExt,
ProgramTestContext,
ProgramTestError,
},
solana_sdk::{
account::Account,
signature::{
Keypair,
Signer,
},
transaction::Transaction,
},
std::{
collections::HashMap,
fs::File,
iter::once,
mem::size_of,
path::PathBuf,
},
};
/// Simulator for the state of the pyth program on Solana. You can run solana transactions against
/// this struct to test how pyth instructions execute in the Solana runtime.
pub struct PythSimulator {
program_id: Pubkey,
context: ProgramTestContext,
/// Hash used to submit the last transaction. The hash must be advanced for each new
/// transaction; otherwise, replayed transactions in different states can return stale
/// results.
last_blockhash: Hash,
programdata_id: Pubkey,
pub upgrade_authority: Keypair,
pub genesis_keypair: Keypair,
}
pub struct Quote {
pub price: i64,
pub confidence: u64,
pub status: u32,
}
#[derive(Debug, Deserialize, Serialize)]
struct ProductMetadata {
symbol: String,
asset_type: String,
country: String,
quote_currency: String,
tenor: String,
}
impl PythSimulator {
/// Deploys the oracle program as upgradable
pub async fn new() -> PythSimulator {
let target_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../../target");
let oracle_program_binary_path = PathBuf::from(target_dir).join("deploy/pyth_oracle.so");
let mut bpf_data = read_file(&oracle_program_binary_path);
let mut program_test = ProgramTest::default();
let program_key = Pubkey::new_unique();
// This PDA is the actual address in the real world
// https://docs.rs/solana-program/1.6.4/solana_program/bpf_loader_upgradeable/index.html
let (programdata_key, _) =
Pubkey::find_program_address(&[&program_key.to_bytes()], &bpf_loader_upgradeable::id());
let upgrade_authority_keypair = Keypair::new();
let program_deserialized = UpgradeableLoaderState::Program {
programdata_address: programdata_key,
};
let programdata_deserialized = UpgradeableLoaderState::ProgramData {
slot: 1,
upgrade_authority_address: Some(upgrade_authority_keypair.pubkey()),
};
// Program contains a pointer to progradata
let program_vec = bincode::serialize(&program_deserialized).unwrap();
// Programdata contains a header and the binary of the program
let mut programdata_vec = bincode::serialize(&programdata_deserialized).unwrap();
programdata_vec.append(&mut bpf_data);
let program_account = Account {
lamports: Rent::default().minimum_balance(program_vec.len()),
data: program_vec,
owner: bpf_loader_upgradeable::ID,
executable: true,
rent_epoch: Epoch::default(),
};
let programdata_account = Account {
lamports: Rent::default().minimum_balance(programdata_vec.len()),
data: programdata_vec,
owner: bpf_loader_upgradeable::ID,
executable: false,
rent_epoch: Epoch::default(),
};
// Add accounts to program test, now the program is deploy as upgradable
program_test.add_account(program_key, program_account);
program_test.add_account(programdata_key, programdata_account);
// Start validator
let context = program_test.start_with_context().await;
let genesis_keypair = copy_keypair(&context.payer);
let last_blockhash = context.last_blockhash;
let mut result = PythSimulator {
program_id: program_key,
context,
last_blockhash,
programdata_id: programdata_key,
upgrade_authority: upgrade_authority_keypair,
genesis_keypair: copy_keypair(&genesis_keypair),
};
// Transfer money to upgrade_authority so it can call the instructions
result
.airdrop(&result.upgrade_authority.pubkey(), 1000 * LAMPORTS_PER_SOL)
.await
.unwrap();
result
.upd_permissions(
UpdPermissionsArgs {
header: OracleCommand::UpdPermissions.into(),
master_authority: genesis_keypair.pubkey(),
data_curation_authority: genesis_keypair.pubkey(),
security_authority: genesis_keypair.pubkey(),
},
©_keypair(&result.upgrade_authority),
)
.await
.unwrap();
result
}
/// Process a transaction containing `instructions` signed by `signers`.
/// `payer` is used to pay for and sign the transaction.
async fn process_ixs(
&mut self,
instructions: &[Instruction],
signers: &Vec<&Keypair>,
payer: &Keypair,
) -> Result<(), BanksClientError> {
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
let blockhash = self
.context
.banks_client
.get_new_latest_blockhash(&self.last_blockhash)
.await
.unwrap();
self.last_blockhash = blockhash;
transaction.partial_sign(&[payer], self.last_blockhash);
transaction.partial_sign(signers, self.last_blockhash);
self.context
.banks_client
.process_transaction(transaction)
.await
}
/// Create an account owned by the pyth program containing `size` bytes.
/// The account will be created with enough lamports to be rent-exempt.
pub async fn create_pyth_account(&mut self, size: usize) -> Keypair {
let keypair = Keypair::new();
let rent = Rent::minimum_balance(&Rent::default(), size);
let instruction = system_instruction::create_account(
&self.genesis_keypair.pubkey(),
&keypair.pubkey(),
rent,
size as u64,
&self.program_id,
);
self.process_ixs(
&[instruction],
&vec![&keypair],
©_keypair(&self.genesis_keypair),
)
.await
.unwrap();
keypair
}
/// Initialize a mapping account (using the init_mapping instruction), returning the keypair
/// associated with the newly-created account.
pub async fn init_mapping(&mut self) -> Result<Keypair, BanksClientError> {
let mapping_keypair = self.create_pyth_account(size_of::<MappingAccount>()).await;
let cmd: CommandHeader = OracleCommand::InitMapping.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(mapping_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![&mapping_keypair],
©_keypair(&self.genesis_keypair),
)
.await
.map(|_| mapping_keypair)
}
/// Initialize a product account and add it to an existing mapping account (using the
/// add_product instruction). Returns the keypair associated with the newly-created account.
pub async fn add_product(
&mut self,
mapping_keypair: &Keypair,
) -> Result<Keypair, BanksClientError> {
let product_keypair = self.create_pyth_account(PC_PROD_ACC_SIZE as usize).await;
let cmd: CommandHeader = OracleCommand::AddProduct.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(mapping_keypair.pubkey(), true),
AccountMeta::new(product_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![mapping_keypair, &product_keypair],
©_keypair(&self.genesis_keypair),
)
.await
.map(|_| product_keypair)
}
/// Delete a product account (using the del_product instruction).
pub async fn del_product(
&mut self,
mapping_keypair: &Keypair,
product_keypair: &Keypair,
) -> Result<(), BanksClientError> {
let cmd: CommandHeader = OracleCommand::DelProduct.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(mapping_keypair.pubkey(), true),
AccountMeta::new(product_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![mapping_keypair, product_keypair],
©_keypair(&self.genesis_keypair),
)
.await
}
/// Initialize a price account and add it to an existing product account (using the add_price
/// instruction). Returns the keypair associated with the newly-created account.
pub async fn add_price(
&mut self,
product_keypair: &Keypair,
expo: i32,
) -> Result<Keypair, BanksClientError> {
let price_keypair = self
.create_pyth_account(size_of::<crate::accounts::PriceAccount>())
.await;
let cmd = AddPriceArgs {
header: OracleCommand::AddPrice.into(),
exponent: expo,
price_type: PC_PTYPE_PRICE,
};
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(product_keypair.pubkey(), true),
AccountMeta::new(price_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![product_keypair, &price_keypair],
©_keypair(&self.genesis_keypair),
)
.await
.map(|_| price_keypair)
}
/// Add a publisher to a price account (using the add_publisher instruction).
pub async fn add_publisher(
&mut self,
price_keypair: &Keypair,
publisher: Pubkey,
) -> Result<(), BanksClientError> {
let cmd = AddPublisherArgs {
header: OracleCommand::AddPublisher.into(),
publisher,
};
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(price_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![price_keypair],
©_keypair(&self.genesis_keypair),
)
.await
}
/// Update price of a component price account (using the upd_price instruction).
pub async fn upd_price(
&mut self,
publisher: &Keypair,
price_account: Pubkey,
quote: Quote,
) -> Result<(), BanksClientError> {
self.upd_price_batch(
publisher,
&HashMap::from_iter(once(("".to_string(), price_account))),
&HashMap::from_iter(once(("".to_string(), quote))),
)
.await
}
/// Update price in multiple price account atomically (using the upd_price instruction)
pub async fn upd_price_batch(
&mut self,
publisher: &Keypair,
price_accounts: &HashMap<String, Pubkey>,
quotes: &HashMap<String, Quote>,
) -> Result<(), BanksClientError> {
let slot = self.context.banks_client.get_sysvar::<Clock>().await?.slot;
let mut instructions: Vec<Instruction> = vec![];
for (key, price_account) in price_accounts {
let cmd = UpdPriceArgs {
header: OracleCommand::UpdPrice.into(),
status: quotes[key].status,
unused_: 0,
price: quotes[key].price,
confidence: quotes[key].confidence,
publishing_slot: slot,
};
instructions.push(Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(publisher.pubkey(), true),
AccountMeta::new(*price_account, false),
AccountMeta::new(Clock::id(), false),
],
));
}
self.process_ixs(&instructions, &vec![publisher], publisher)
.await
}
// /// Delete a price account from an existing product account (using the del_price instruction).
pub async fn del_price(
&mut self,
product_keypair: &Keypair,
price_keypair: &Keypair,
) -> Result<(), BanksClientError> {
let cmd: CommandHeader = OracleCommand::DelPrice.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![
AccountMeta::new(self.genesis_keypair.pubkey(), true),
AccountMeta::new(product_keypair.pubkey(), true),
AccountMeta::new(price_keypair.pubkey(), true),
AccountMeta::new(self.get_permissions_pubkey(), false),
],
);
self.process_ixs(
&[instruction],
&vec![product_keypair, price_keypair],
©_keypair(&self.genesis_keypair),
)
.await
}
/// Update permissions (using the upd_permissions intruction) and return the pubkey of the
/// permissions account
pub async fn upd_permissions(
&mut self,
cmd_args: UpdPermissionsArgs,
payer: &Keypair,
) -> Result<Pubkey, BanksClientError> {
let permissions_pubkey = self.get_permissions_pubkey();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd_args),
vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new_readonly(self.programdata_id, false),
AccountMeta::new(permissions_pubkey, false),
AccountMeta::new_readonly(system_program::id(), false),
],
);
self.process_ixs(&[instruction], &vec![], payer)
.await
.map(|_| permissions_pubkey)
}
pub async fn truncate_account(&mut self, key: Pubkey, size: usize) {
let mut account = self.get_account(key).await.unwrap();
account.data.truncate(size);
self.context.set_account(&key, &account.into());
}
pub async fn resize_mapping(
&mut self,
mapping_keypair: &Keypair,
) -> Result<(), BanksClientError> {
let cmd: CommandHeader = OracleCommand::ResizeMapping.into();
let instruction = Instruction::new_with_bytes(
self.program_id,
bytes_of(&cmd),
vec![AccountMeta::new(mapping_keypair.pubkey(), false)],
);
self.process_ixs(
&[instruction],
&vec![],
©_keypair(&self.genesis_keypair),
)
.await
}
/// Get the account at `key`. Returns `None` if no such account exists.
pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> {
self.context.banks_client.get_account(key).await.unwrap()
}
/// Get the content of an account as a value of type `T`. This function returns a copy of the
/// account data -- you cannot mutate the result to mutate the on-chain account data.
/// Returns None if the account does not exist. Panics if the account data cannot be read as a
/// `T`.
pub async fn get_account_data_as<T: Pod>(&mut self, key: Pubkey) -> Option<T> {
self.get_account(key)
.await
.map(|x| *load::<T>(&x.data).unwrap())
}
pub fn is_owned_by_oracle(&self, account: &Account) -> bool {
account.owner == self.program_id
}
pub async fn airdrop(&mut self, to: &Pubkey, lamports: u64) -> Result<(), BanksClientError> {
let instruction =
system_instruction::transfer(&self.genesis_keypair.pubkey(), to, lamports);
self.process_ixs(
&[instruction],
&vec![],
©_keypair(&self.genesis_keypair),
)
.await
}
pub fn get_permissions_pubkey(&self) -> Pubkey {
let (permissions_pubkey, __bump) =
Pubkey::find_program_address(&[PERMISSIONS_SEED.as_bytes()], &self.program_id);
permissions_pubkey
}
/// Setup 3 product accounts with 1 price account each and add a publisher to all of them.
/// Returns the mapping of product symbol to price account pubkey.
/// TODO : this fixture doesn't set the product metadata
pub async fn setup_product_fixture(
&mut self,
publishers: &[Pubkey],
security_authority: Pubkey,
) -> HashMap<String, Pubkey> {
let result_file =
File::open("./test_data/publish/products.json").expect("Test file not found");
for publisher in publishers {
self.airdrop(publisher, 100 * LAMPORTS_PER_SOL)
.await
.unwrap();
}
self.airdrop(&security_authority, 100 * LAMPORTS_PER_SOL)
.await
.unwrap();
let product_metadatas: HashMap<String, ProductMetadata> =
serde_json::from_reader(&result_file).unwrap();
let mut price_accounts: HashMap<String, Pubkey> = HashMap::new();
let mapping_keypair: Keypair = self.init_mapping().await.unwrap();
for symbol in product_metadatas.keys() {
let product_keypair = self.add_product(&mapping_keypair).await.unwrap();
let price_keypair = self.add_price(&product_keypair, -5).await.unwrap();
for publisher in publishers.iter() {
self.add_publisher(&price_keypair, *publisher)
.await
.unwrap();
}
price_accounts.insert(symbol.to_string(), price_keypair.pubkey());
}
price_accounts
}
/// Advance clock to slot `slot`.
pub async fn warp_to_slot(&mut self, slot: u64) -> Result<(), ProgramTestError> {
self.context.warp_to_slot(slot)
}
}
pub fn copy_keypair(keypair: &Keypair) -> Keypair {
Keypair::from_bytes(&keypair.to_bytes()).unwrap()
}