|
| 1 | +// Use the Solana client library to pull the addresses of all relevant accounts from PythNet so we |
| 2 | +// can test locally. |
| 3 | + |
| 4 | +// #![feature(proc_macro_hygiene)] |
| 5 | + |
| 6 | +use { |
| 7 | + serde_json::json, |
| 8 | + solana_client::rpc_client::RpcClient, |
| 9 | + solana_pyth::PYTH_PID, |
| 10 | + solana_sdk::pubkey::Pubkey, |
| 11 | + std::str::FromStr, |
| 12 | + std::io::Write, |
| 13 | +}; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + let client = RpcClient::new("http://pythnet.rpcpool.com/".to_string()); |
| 17 | + let pythnet = Pubkey::from_str("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH").unwrap(); |
| 18 | + let wormhole = Pubkey::from_str("H3fxXJ86ADW2PNuDDmZJg6mzTtPxkYCpNuQUTgmJ7AjU").unwrap(); |
| 19 | + |
| 20 | + // Create a folder called `accounts` in the current directory, if it already exists that is OK |
| 21 | + // but only if the folder is empty. |
| 22 | + std::fs::create_dir_all("accounts").unwrap(); |
| 23 | + |
| 24 | + // Download all PythNet accounts into .json files in the current directory. |
| 25 | + { |
| 26 | + let pythnet_accounts = client.get_program_accounts(&pythnet).map_err(|e| { |
| 27 | + println!("{e}"); |
| 28 | + e |
| 29 | + }); |
| 30 | + |
| 31 | + pythnet_accounts |
| 32 | + .unwrap() |
| 33 | + .into_iter() |
| 34 | + .for_each(|(pubkey, _account)| { |
| 35 | + // This writes the account as JSON into a file that solana-test-validator can read into |
| 36 | + // the ledger. Each account should be written into a file named `<pubkey>.json` |
| 37 | + let account = client.get_account(&pubkey).unwrap(); |
| 38 | + |
| 39 | + // Now write to <pubkey>.json. |
| 40 | + std::fs::write( |
| 41 | + format!("accounts/{pubkey}.json"), |
| 42 | + json!({ |
| 43 | + "pubkey": pubkey.to_string(), |
| 44 | + "account": { |
| 45 | + "lamports": account.lamports, |
| 46 | + "data": [ |
| 47 | + base64::encode(&account.data), |
| 48 | + "base64" |
| 49 | + ], |
| 50 | + "owner": account.owner.to_string(), |
| 51 | + "executable": account.executable, |
| 52 | + "rentEpoch": account.rent_epoch, |
| 53 | + } |
| 54 | + }) |
| 55 | + .to_string(), |
| 56 | + ) |
| 57 | + .unwrap(); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + // Download the Wormhole program only into a .json file in the current directory. Instead of |
| 62 | + // getting the program accounts we just want the wormhole one itself. |
| 63 | + { |
| 64 | + let wormhole_account = client.get_account(&wormhole).unwrap(); |
| 65 | + |
| 66 | + // Now write to wormhole.json. |
| 67 | + std::fs::write( |
| 68 | + format!("accounts/{wormhole}.json"), |
| 69 | + json!({ |
| 70 | + "pubkey": wormhole.to_string(), |
| 71 | + "account": { |
| 72 | + "lamports": wormhole_account.lamports, |
| 73 | + "data": [ |
| 74 | + base64::encode(&wormhole_account.data), |
| 75 | + "base64" |
| 76 | + ], |
| 77 | + "owner": wormhole_account.owner.to_string(), |
| 78 | + "executable": wormhole_account.executable, |
| 79 | + "rentEpoch": wormhole_account.rent_epoch, |
| 80 | + } |
| 81 | + }) |
| 82 | + .to_string(), |
| 83 | + ) |
| 84 | + .unwrap(); |
| 85 | + } |
| 86 | + |
| 87 | + // Same for the Pyth program. |
| 88 | + { |
| 89 | + let pyth_account = client.get_account(&pythnet).unwrap(); |
| 90 | + |
| 91 | + // Now write to pyth.json. |
| 92 | + std::fs::write( |
| 93 | + format!("accounts/{pythnet}.json"), |
| 94 | + json!({ |
| 95 | + "pubkey": pythnet.to_string(), |
| 96 | + "account": { |
| 97 | + "lamports": pyth_account.lamports, |
| 98 | + "data": [ |
| 99 | + base64::encode(&pyth_account.data), |
| 100 | + "base64" |
| 101 | + ], |
| 102 | + "owner": pyth_account.owner.to_string(), |
| 103 | + "executable": pyth_account.executable, |
| 104 | + "rentEpoch": pyth_account.rent_epoch, |
| 105 | + } |
| 106 | + }) |
| 107 | + .to_string(), |
| 108 | + ) |
| 109 | + .unwrap(); |
| 110 | + } |
| 111 | + |
| 112 | + // Write names of AccumulatorState accounts to pdas.txt |
| 113 | + { |
| 114 | + let mut file = std::fs::File::create("pdas.txt").unwrap(); |
| 115 | + for i in (0..10_000u32) { |
| 116 | + let (accumulator_account, _) = Pubkey::find_program_address( |
| 117 | + &[b"AccumulatorState", &PYTH_PID, &i.to_be_bytes()], |
| 118 | + &solana_sdk::system_program::id(), |
| 119 | + ); |
| 120 | + file.write_all(format!("{}\n", accumulator_account).as_bytes()) |
| 121 | + .unwrap(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments