|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use dirs::home_dir; |
| 5 | +use light_batched_merkle_tree::initialize_address_tree::InitAddressTreeAccountsInstructionData; |
| 6 | +use light_client::rpc::{LightClient, LightClientConfig, Rpc}; |
| 7 | +use light_program_test::accounts::address_tree_v2::create_batch_address_merkle_tree; |
| 8 | +use solana_sdk::signature::{read_keypair_file, write_keypair_file, Keypair, Signer}; |
| 9 | + |
| 10 | +#[derive(Debug, Parser)] |
| 11 | +pub struct Options { |
| 12 | + #[clap(long)] |
| 13 | + payer: Option<PathBuf>, |
| 14 | + #[clap(long)] |
| 15 | + mt_pubkey: Option<String>, |
| 16 | + #[clap(long)] |
| 17 | + /// mainnet, devnet, local, default: mainnet |
| 18 | + #[clap(long)] |
| 19 | + network: Option<String>, |
| 20 | + /// mainnet, devnet, local, default: mainnet |
| 21 | + #[clap(long, default_value = "false")] |
| 22 | + new: bool, |
| 23 | + /// mainnet, testnet |
| 24 | + #[clap(long)] |
| 25 | + config: Option<String>, |
| 26 | +} |
| 27 | + |
| 28 | +pub async fn create_batch_address_tree(options: Options) -> anyhow::Result<()> { |
| 29 | + let rpc_url = if let Some(network) = options.network { |
| 30 | + if network == "local" { |
| 31 | + String::from("http://127.0.0.1:8899") |
| 32 | + } else if network == "devnet" { |
| 33 | + String::from("https://api.devnet.solana.com") |
| 34 | + } else if network == "mainnet" { |
| 35 | + String::from("https://api.mainnet-beta.solana.com") |
| 36 | + } else { |
| 37 | + network.to_string() |
| 38 | + } |
| 39 | + } else { |
| 40 | + String::from("https://api.mainnet-beta.solana.com") |
| 41 | + }; |
| 42 | + let mut rpc = LightClient::new(LightClientConfig { |
| 43 | + url: rpc_url, |
| 44 | + commitment_config: None, |
| 45 | + fetch_active_tree: false, |
| 46 | + with_indexer: false, |
| 47 | + }) |
| 48 | + .await |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + let mut mt_keypairs: Vec<Keypair> = vec![]; |
| 52 | + |
| 53 | + if options.new { |
| 54 | + let mt_keypair = Keypair::new(); |
| 55 | + println!("new mt: {:?}", mt_keypair.pubkey()); |
| 56 | + |
| 57 | + write_keypair_file(&mt_keypair, format!("./target/mt-{}", mt_keypair.pubkey())).unwrap(); |
| 58 | + |
| 59 | + mt_keypairs.push(mt_keypair); |
| 60 | + } else { |
| 61 | + let mt_keypair = read_keypair_file(options.mt_pubkey.unwrap()).unwrap(); |
| 62 | + println!("read mt: {:?}", mt_keypair.pubkey()); |
| 63 | + mt_keypairs.push(mt_keypair); |
| 64 | + } |
| 65 | + |
| 66 | + let payer = if let Some(payer) = options.payer.as_ref() { |
| 67 | + read_keypair_file(payer).unwrap_or_else(|_| panic!("{:?}", options.payer)) |
| 68 | + } else { |
| 69 | + // Construct the path to the keypair file in the user's home directory |
| 70 | + let keypair_path: PathBuf = home_dir() |
| 71 | + .expect("Could not find home directory") |
| 72 | + .join(".config/solana/id.json"); |
| 73 | + read_keypair_file(keypair_path.clone()) |
| 74 | + .unwrap_or_else(|_| panic!("Keypair not found in default path {:?}", keypair_path)) |
| 75 | + }; |
| 76 | + println!("read payer: {:?}", payer.pubkey()); |
| 77 | + |
| 78 | + let config = if let Some(config) = options.config { |
| 79 | + if config == "testnet" { |
| 80 | + InitAddressTreeAccountsInstructionData::testnet_default() |
| 81 | + } else { |
| 82 | + unimplemented!() |
| 83 | + } |
| 84 | + } else { |
| 85 | + InitAddressTreeAccountsInstructionData::default() |
| 86 | + }; |
| 87 | + |
| 88 | + for merkle_tree_keypair in mt_keypairs.iter() { |
| 89 | + println!( |
| 90 | + "creating address Merkle tree: \n\tmt {:?}", |
| 91 | + merkle_tree_keypair.pubkey(), |
| 92 | + ); |
| 93 | + let balance = rpc.get_balance(&payer.pubkey()).await.unwrap(); |
| 94 | + println!("Payer balance: {:?}", balance); |
| 95 | + let tx_hash = |
| 96 | + create_batch_address_merkle_tree(&mut rpc, &payer, merkle_tree_keypair, config) |
| 97 | + .await |
| 98 | + .unwrap(); |
| 99 | + |
| 100 | + println!("tx_hash: {:?}", tx_hash); |
| 101 | + } |
| 102 | + Ok(()) |
| 103 | +} |
0 commit comments