From 974e7599435374249e9ac51255f241005d288549 Mon Sep 17 00:00:00 2001 From: Samuel Vanderwaal Date: Tue, 9 Aug 2022 23:10:28 -0800 Subject: [PATCH] refactor: various minor improvements (#174) * refactor: pretty print JSON files * refactor: sort lists by mint addresses --- src/cache/mod.rs | 29 ++++++++++++++++++----------- src/collections/items.rs | 6 ++++-- src/collections/migrate.rs | 24 ++++++++++++++++++++---- src/decode.rs | 4 ++-- src/snapshot/data.rs | 4 ++-- src/snapshot/methods.rs | 17 ++++++++++------- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 356767d9..99bfd51a 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -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; @@ -32,13 +35,20 @@ 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(self, writer: W) -> AnyResult<()> { - serde_json::to_writer(writer, &self)?; + pub fn write(&mut self, writer: W) -> AnyResult<()> { + self.sort_unstable_keys(); + serde_json::to_writer_pretty(writer, &self)?; Ok(()) } @@ -46,7 +56,7 @@ impl Cache { 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 { @@ -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); } } } @@ -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); diff --git a/src/collections/items.rs b/src/collections/items.rs index ff86a260..05cb662b 100644 --- a/src/collections/items.rs +++ b/src/collections/items.rs @@ -38,7 +38,7 @@ pub async fn get_collection_items_by_the_index_io( let res: RpcResponse = response.json().await?; - let mints: Vec = res + let mut mints: Vec = res .result .iter() .map(|nft| nft.metadata.mint.clone()) @@ -46,6 +46,8 @@ pub async fn get_collection_items_by_the_index_io( 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}"); @@ -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. diff --git a/src/collections/migrate.rs b/src/collections/migrate.rs index 4d40326d..8153cf68 100644 --- a/src/collections/migrate.rs +++ b/src/collections/migrate.rs @@ -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, @@ -32,13 +33,28 @@ impl Default for MigrateCache { } } +impl Deref for MigrateCache { + type Target = IndexMap; + + 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(self, writer: W) -> AnyResult<()> { - serde_json::to_writer(writer, &self)?; + pub fn write(&mut self, writer: W) -> AnyResult<()> { + self.sort_unstable_keys(); + serde_json::to_writer_pretty(writer, &self)?; Ok(()) } @@ -46,7 +62,7 @@ impl MigrateCache { 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 { @@ -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); } } } diff --git a/src/decode.rs b/src/decode.rs index dc20e4b3..924a64bc 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -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!( @@ -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 { diff --git a/src/snapshot/data.rs b/src/snapshot/data.rs index 495b0a0d..52d22ada 100644 --- a/src/snapshot/data.rs +++ b/src/snapshot/data.rs @@ -3,12 +3,12 @@ use super::common::*; pub const PARALLEL_LIMIT: usize = 50; pub type HolderResults = Vec>; -#[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)] diff --git a/src/snapshot/methods.rs b/src/snapshot/methods.rs index be474b83..693dde79 100644 --- a/src/snapshot/methods.rs +++ b/src/snapshot/methods.rs @@ -28,7 +28,7 @@ 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, @@ -36,8 +36,9 @@ pub fn snapshot_mints(client: &RpcClient, args: SnapshotMintsArgs) -> Result<()> 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(()) } @@ -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(()) } @@ -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(()) } @@ -319,7 +322,7 @@ pub async fn snapshot_indexed_holders( .map(|e| e.to_string()) .collect::>(); let f = File::create(format!("{}/{}_errors.json", output, creator))?; - serde_json::to_writer(&f, &errors)?; + serde_json::to_writer_pretty(&f, &errors)?; } // Unwrap sucessful @@ -327,7 +330,7 @@ pub async fn snapshot_indexed_holders( 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(()) } @@ -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(()) }