Skip to content

Commit

Permalink
chore: add create state Merkle tree script (#1485)
Browse files Browse the repository at this point in the history
* chore: add create state Merkle tree script

* fix: lint
  • Loading branch information
ananas-block authored Jan 18, 2025
1 parent 8b38899 commit 126575a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
38 changes: 38 additions & 0 deletions scripts/create-state-trees.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Base directory for keypairs
KEYPAIR_DIR="./target/tree-keypairs"

# Command template
CMD_TEMPLATE="cargo xtask create-state-tree --mt-pubkey {SMT} --nfq-pubkey {NFQ} --cpi-pubkey {CPI} --index {INDEX} --network devnet"

# Collect sorted key files for each type
SMT_KEYS=($(ls $KEYPAIR_DIR/smt*.json | sort))
NFQ_KEYS=($(ls $KEYPAIR_DIR/nfq*.json | sort))
CPI_KEYS=($(ls $KEYPAIR_DIR/cpi*.json | sort))

# Ensure equal number of keys for each type
if [[ ${#SMT_KEYS[@]} -ne ${#NFQ_KEYS[@]} || ${#NFQ_KEYS[@]} -ne ${#CPI_KEYS[@]} ]]; then
echo "Error: Mismatched number of SMT, NFQ, and CPI key files."
exit 1
fi

# Execute the command for each triple
for i in "${!SMT_KEYS[@]}"; do
SMT_KEY="${SMT_KEYS[i]}"
NFQ_KEY="${NFQ_KEYS[i]}"
CPI_KEY="${CPI_KEYS[i]}"
INDEX=$((i + 2))

# Replace placeholders in the command template
CMD=${CMD_TEMPLATE//\{SMT\}/"$SMT_KEY"}
CMD=${CMD//\{NFQ\}/"$NFQ_KEY"}
CMD=${CMD//\{CPI\}/"$CPI_KEY"}
CMD=${CMD//\{INDEX\}/"$INDEX"}

echo "Executing: $CMD"
eval "$CMD"

done

echo "All commands executed."
22 changes: 5 additions & 17 deletions xtask/src/create_state_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub struct Options {
#[clap(long)]
payer: Option<PathBuf>,
#[clap(long)]
path: PathBuf,
#[clap(long)]
mt_pubkey: String,
#[clap(long)]
nfq_pubkey: String,
Expand All @@ -40,33 +38,23 @@ pub async fn create_state_tree(options: Options) -> anyhow::Result<()> {
String::from("https://api.mainnet-beta.solana.com")
};
let mut rpc = SolanaRpcConnection::new(rpc_url, None);
let path = if let Some(path) = options.path.to_str() {
String::from(path)
} else {
String::from("./target/state-trees/")
};

let mut mt_keypairs: Vec<Keypair> = vec![];
let mut nfq_keypairs: Vec<Keypair> = vec![];
let mut cpi_keypairs: Vec<Keypair> = vec![];

println!(
"path mt_pubkey {:?}",
format!("{}-{}.json", path, options.mt_pubkey)
);

let mt_keypair = read_keypair_file(format!("{}{}.json", path, options.mt_pubkey)).unwrap();
let nfq_keypair = read_keypair_file(format!("{}{}.json", path, options.nfq_pubkey)).unwrap();
let cpi_keypair = read_keypair_file(format!("{}{}.json", path, options.cpi_pubkey)).unwrap();
let mt_keypair = read_keypair_file(options.mt_pubkey).unwrap();
let nfq_keypair = read_keypair_file(options.nfq_pubkey).unwrap();
let cpi_keypair = read_keypair_file(options.cpi_pubkey).unwrap();
println!("read mt: {:?}", mt_keypair.pubkey());
println!("read nfq: {:?}", nfq_keypair.pubkey());
println!("read cpi: {:?}", cpi_keypair.pubkey());
mt_keypairs.push(mt_keypair);
nfq_keypairs.push(nfq_keypair);
cpi_keypairs.push(cpi_keypair);

let payer = if let Some(payer) = options.payer {
read_keypair_file(&payer).unwrap_or_else(|_| panic!("{}{}.json", path, options.cpi_pubkey))
let payer = if let Some(payer) = options.payer.as_ref() {
read_keypair_file(payer).unwrap_or_else(|_| panic!("{:?}", options.payer))
} else {
// Construct the path to the keypair file in the user's home directory
let keypair_path: PathBuf = home_dir()
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum Command {
HashSet(hash_set::HashSetOptions),
/// Create state tree
/// Example:
/// cargo xtask create-state-tree --path ./target/state-trees/ --mt-pubkey smtAvYA5UbTRyKAkAj5kHs1CmrA42t6WkVLi4c6mA1f --nfq-pubkey nfqAroCRkcZBgsAJDNkptKpsSWyM6cgB9XpWNNiCEC4 --cpi-pubkey cpiAb2eNFf6MQeqMWEyEjSN3VJcD5hghujhmtdcMuZp --index 10
/// cargo xtask create-state-tree --mt-pubkey ./target/tree-keypairs/smtAvYA5UbTRyKAkAj5kHs1CmrA42t6WkVLi4c6mA1f.json --nfq-pubkey ./target/tree-keypairs/nfqAroCRkcZBgsAJDNkptKpsSWyM6cgB9XpWNNiCEC4.json --cpi-pubkey ./target/tree-keypairs/cpiAb2eNFf6MQeqMWEyEjSN3VJcD5hghujhmtdcMuZp.json --index 10 --network local
CreateStateTree(create_state_tree::Options),
}

Expand Down

0 comments on commit 126575a

Please sign in to comment.