Skip to content

Commit

Permalink
Merge pull request samuelvanderwaal#93 from samuelvanderwaal/feat/add…
Browse files Browse the repository at this point in the history
…-snapshot-holders-mint-list

feat: added the feature to fetch holders when passed a mint list file
  • Loading branch information
samuelvanderwaal authored Feb 22, 2022
2 parents 65a37ec + a8482f3 commit f2b7eb7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ pub enum SnapshotSubcommands {
#[structopt(long = "v2")]
v2: bool,

/// Path to JSON file with list of mint accounts to sign
#[structopt(short, long)]
mint_accounts_file: Option<String>,

/// Path to directory to save output files.
#[structopt(short, long, default_value = ".")]
output: String,
Expand Down
3 changes: 2 additions & 1 deletion src/process_subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ pub fn process_snapshot(client: &RpcClient, commands: SnapshotSubcommands) -> Re
SnapshotSubcommands::Holders {
update_authority,
candy_machine_id,
mint_accounts_file,
v2,
output,
} => snapshot_holders(&client, &update_authority, &candy_machine_id, v2, &output),
} => snapshot_holders(&client, &update_authority, &candy_machine_id, &mint_accounts_file, v2, &output),
SnapshotSubcommands::CMAccounts {
update_authority,
output,
Expand Down
61 changes: 56 additions & 5 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use std::{
sync::{Arc, Mutex},
};

use crate::constants::*;
use crate::derive::derive_cmv2_pda;
use crate::limiter::create_rate_limiter;
use crate::parse::{first_creator_is_verified, is_only_one_option};
use crate::spinner::*;
use crate::{constants::*, decode::get_metadata_pda};

#[derive(Debug, Serialize, Clone)]
struct Holder {
Expand Down Expand Up @@ -131,6 +131,7 @@ pub fn snapshot_holders(
client: &RpcClient,
update_authority: &Option<String>,
candy_machine_id: &Option<String>,
mint_accounts_file: &Option<String>,
v2: bool,
output: &String,
) -> Result<()> {
Expand All @@ -150,9 +151,13 @@ pub fn snapshot_holders(
} else {
get_cm_creator_accounts(client, &candy_machine_id)?
}
} else if let Some(mint_accounts_file) = mint_accounts_file {
let file = File::open(mint_accounts_file)?;
let mint_accounts: Vec<String> = serde_json::from_reader(&file)?;
get_mint_account_infos(client, mint_accounts)?
} else {
return Err(anyhow!(
"Must specify either --update-authority or --candy-machine-id"
"Must specify either --update-authority or --candy-machine-id or --mint-accounts-file"
));
};
spinner.finish_with_message("Getting accounts...Done!");
Expand Down Expand Up @@ -245,12 +250,14 @@ pub fn snapshot_holders(
});

let prefix = if let Some(update_authority) = update_authority {
update_authority
update_authority.clone()
} else if let Some(candy_machine_id) = candy_machine_id {
candy_machine_id
candy_machine_id.clone()
} else if let Some(mint_accounts_file) = mint_accounts_file {
str::replace(mint_accounts_file, ".json", "")
} else {
return Err(anyhow!(
"Must specify either --update-authority or --candy-machine-id"
"Must specify either --update-authority or --candy-machine-id or --mint-accounts-file"
));
};

Expand All @@ -260,6 +267,50 @@ pub fn snapshot_holders(
Ok(())
}

fn get_mint_account_infos(
client: &RpcClient,
mint_accounts: Vec<String>,
) -> Result<Vec<(Pubkey, Account)>> {
let use_rate_limit = *USE_RATE_LIMIT.read().unwrap();
let handle = create_rate_limiter();

let address_account_pairs: Arc<Mutex<Vec<(Pubkey, Account)>>> =
Arc::new(Mutex::new(Vec::new()));

mint_accounts.par_iter().for_each(|mint_account| {
let mut handle = handle.clone();
if use_rate_limit {
handle.wait();
}

let mint_pubkey = match Pubkey::from_str(mint_account) {
Ok(pubkey) => pubkey,
Err(_) => {
error!("Invalid mint address {}", mint_account);
return;
}
};

let metadata_pubkey = get_metadata_pda(mint_pubkey);

let account_info = match client.get_account(&metadata_pubkey) {
Ok(account) => account,
Err(_) => {
error!("Error in fetching metadata for mint {}", mint_account);
return;
}
};

address_account_pairs
.lock()
.unwrap()
.push((mint_pubkey, account_info))
});

let res = address_account_pairs.lock().unwrap().clone();
Ok(res)
}

fn get_mints_by_update_authority(
client: &RpcClient,
update_authority: &String,
Expand Down

0 comments on commit f2b7eb7

Please sign in to comment.