Skip to content

Commit

Permalink
refactor: various minor improvements (#174)
Browse files Browse the repository at this point in the history
* refactor: pretty print JSON files

* refactor: sort lists by mint addresses
  • Loading branch information
samuelvanderwaal authored Aug 10, 2022
1 parent 4391b8b commit 974e759
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
29 changes: 18 additions & 11 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use log::info;
use serde::{Deserialize, Serialize};
use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;
use std::fs::{File, OpenOptions};
use std::path::Path;
use std::sync::Arc;
use std::{io::Write, ops::Deref};
use std::{
fs::{File, OpenOptions},
io::Write,
ops::{Deref, DerefMut},
path::Path,
sync::Arc,
};
use tokio::sync::Semaphore;

use crate::errors::ActionError;
Expand All @@ -32,21 +35,28 @@ impl Deref for Cache {
}
}

impl DerefMut for Cache {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Cache {
pub fn new() -> Self {
Cache(IndexMap::new())
}

pub fn write<W: Write>(self, writer: W) -> AnyResult<()> {
serde_json::to_writer(writer, &self)?;
pub fn write<W: Write>(&mut self, writer: W) -> AnyResult<()> {
self.sort_unstable_keys();
serde_json::to_writer_pretty(writer, &self)?;
Ok(())
}

pub fn update_errors(&mut self, errors: Vec<Result<(), ActionError>>) {
let errors = errors.iter().map(|r| r.as_ref()).map(Result::unwrap_err);

// Clear out old errors.
self.0.clear();
self.clear();

for error in errors {
match error {
Expand All @@ -55,7 +65,7 @@ impl Cache {
error: Some(error.to_string()),
};

self.0.insert(mint_address.to_string(), item);
self.insert(mint_address.to_string(), item);
}
}
}
Expand Down Expand Up @@ -162,9 +172,6 @@ pub trait Action {
new_value: args.new_value.clone(),
});

// Increment the counter and update the progress bar.
// pb.inc(1);

// Move the permit into the thread to take ownership of it and then drop it
// when the future is complete.
drop(permit);
Expand Down
6 changes: 4 additions & 2 deletions src/collections/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ pub async fn get_collection_items_by_the_index_io(

let res: RpcResponse = response.json().await?;

let mints: Vec<String> = res
let mut mints: Vec<String> = res
.result
.iter()
.map(|nft| nft.metadata.mint.clone())
.collect();

let file_name = format!("{collection_mint}_collection_items.json");
let f = File::create(&file_name).unwrap();

mints.sort_unstable();
serde_json::to_writer_pretty(f, &mints).unwrap();
println!("Data written to {file_name}");

Expand Down Expand Up @@ -133,7 +135,7 @@ pub async fn check_collection_items(
if debug {
println!("Writing debug file...");
let out = File::create(format!("{collection_mint}-debug-collections.json"))?;
serde_json::to_writer(out, &collections)?;
serde_json::to_writer_pretty(out, &collections)?;
}

// Check if there's the only one and correct collection parent associated with the mint list and that all items in the list belong to it.
Expand Down
24 changes: 20 additions & 4 deletions src/collections/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use crate::{parse::parse_keypair, snapshot::get_mint_accounts};
use borsh::BorshDeserialize;
use mpl_token_metadata::instruction::set_and_verify_sized_collection_item;
use std::ops::{Deref, DerefMut};

pub struct MigrateArgs {
pub client: RpcClient,
Expand All @@ -32,21 +33,36 @@ impl Default for MigrateCache {
}
}

impl Deref for MigrateCache {
type Target = IndexMap<String, CacheItem>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for MigrateCache {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl MigrateCache {
pub fn new() -> Self {
MigrateCache(IndexMap::new())
}

pub fn write<W: Write>(self, writer: W) -> AnyResult<()> {
serde_json::to_writer(writer, &self)?;
pub fn write<W: Write>(&mut self, writer: W) -> AnyResult<()> {
self.sort_unstable_keys();
serde_json::to_writer_pretty(writer, &self)?;
Ok(())
}

pub fn update_errors(&mut self, errors: Vec<Result<(), MigrateError>>) {
let errors = errors.iter().map(|r| r.as_ref()).map(Result::unwrap_err);

// Clear out old errors.
self.0.clear();
self.clear();

for error in errors {
match error {
Expand All @@ -55,7 +71,7 @@ impl MigrateCache {
error: Some(error.to_string()),
};

self.0.insert(mint_address.to_string(), item);
self.insert(mint_address.to_string(), item);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn decode_metadata_all(
};

debug!("Writing to file for mint account: {}", mint_account);
match serde_json::to_writer(&mut file, &json_metadata) {
match serde_json::to_writer_pretty(&mut file, &json_metadata) {
Ok(_) => (),
Err(err) => {
error!(
Expand Down Expand Up @@ -161,7 +161,7 @@ pub fn decode_metadata(
let metadata = decode(client, mint_account)?;
let json_metadata = decode_to_json(metadata, full)?;
let mut file = File::create(format!("{}/{}.json", output, mint_account))?;
serde_json::to_writer(&mut file, &json_metadata)?;
serde_json::to_writer_pretty(&mut file, &json_metadata)?;
} else if let Some(list_path) = list_path {
decode_metadata_all(client, list_path, full, output)?;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/snapshot/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use super::common::*;
pub const PARALLEL_LIMIT: usize = 50;
pub type HolderResults = Vec<Result<Holder>>;

#[derive(Debug, Serialize, Clone)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize)]
pub struct Holder {
pub owner_wallet: String,
pub associated_token_address: String,
pub mint_account: String,
pub metadata_account: String,
pub associated_token_address: String,
}

#[derive(Debug, Serialize)]
Expand Down
17 changes: 10 additions & 7 deletions src/snapshot/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ pub fn snapshot_mints(client: &RpcClient, args: SnapshotMintsArgs) -> Result<()>
));
};

let mint_accounts = get_mint_accounts(
let mut mint_addresses = get_mint_accounts(
client,
&args.creator,
args.position,
args.update_authority,
args.v2,
)?;

mint_addresses.sort_unstable();
let mut file = File::create(format!("{}/{}_mint_accounts.json", args.output, prefix))?;
serde_json::to_writer(&mut file, &mint_accounts)?;
serde_json::to_writer_pretty(&mut file, &mint_addresses)?;

Ok(())
}
Expand Down Expand Up @@ -67,8 +68,9 @@ pub async fn snapshot_indexed_mints(
mint_addresses.push(metadata.mint.to_string());
}

mint_addresses.sort_unstable();
let mut file = File::create(format!("{output}/{creator}_mint_accounts.json"))?;
serde_json::to_writer(&mut file, &mint_addresses)?;
serde_json::to_writer_pretty(&mut file, &mint_addresses)?;

Ok(())
}
Expand Down Expand Up @@ -257,8 +259,9 @@ pub fn snapshot_holders(
));
};

nft_holders.lock().unwrap().sort_unstable();
let mut file = File::create(format!("{}/{}_holders.json", output, prefix))?;
serde_json::to_writer(&mut file, &nft_holders)?;
serde_json::to_writer_pretty(&mut file, &nft_holders)?;

Ok(())
}
Expand Down Expand Up @@ -319,15 +322,15 @@ pub async fn snapshot_indexed_holders(
.map(|e| e.to_string())
.collect::<Vec<_>>();
let f = File::create(format!("{}/{}_errors.json", output, creator))?;
serde_json::to_writer(&f, &errors)?;
serde_json::to_writer_pretty(&f, &errors)?;
}

// Unwrap sucessful
let nft_holders: Vec<Holder> = successful_results.into_iter().map(Result::unwrap).collect();
println!("Found {} holders", nft_holders.len());

let mut file = File::create(format!("{output}/{creator}_holders.json"))?;
serde_json::to_writer(&mut file, &nft_holders)?;
serde_json::to_writer_pretty(&mut file, &nft_holders)?;

Ok(())
}
Expand Down Expand Up @@ -511,7 +514,7 @@ pub fn snapshot_cm_accounts(
};

let mut file = File::create(format!("{}/{}_accounts.json", output, update_authority))?;
serde_json::to_writer(&mut file, &candy_machine_program_accounts)?;
serde_json::to_writer_pretty(&mut file, &candy_machine_program_accounts)?;

Ok(())
}
Expand Down

0 comments on commit 974e759

Please sign in to comment.