Skip to content

Commit

Permalink
add subcommand to upload params
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Nov 20, 2024
1 parent 5ec4ba5 commit 7a9be2d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
43 changes: 43 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use signal_hook::{
};
use solana_cli_config::{Config, CONFIG_FILE};
use solana_client::rpc_client::RpcClient;
use solana_program::process_upload_program;
use solana_sdk::{
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
pubkey::Pubkey,
Expand Down Expand Up @@ -177,6 +178,26 @@ async fn main() -> anyhow::Result<()> {
.multiple(true)
.last(true)
.help("Arguments to pass to the underlying `cargo build-bpf` command")))
.subcommand(SubCommand::with_name("write")
.about("Write parameters to the otter-verify PDA account associated with the given program ID")
.arg(Arg::with_name("program-id")
.long("program-id")
.required(true)
.takes_value(true)
.help("The address of the program to close the PDA"))
.arg(Arg::with_name("git-url")
.long("git-url")
.required(true)
.takes_value(true)
.help("The HTTPS url of the repository to verify"))
.arg(Arg::with_name("commit")
.long("commit")
.takes_value(true)
.help("The optional commit hash of the repository to verify"))
.arg(Arg::with_name("flags")
.multiple(true)
.last(true)
.help("Additional flags and values to pass")))
.subcommand(SubCommand::with_name("close")
.about("Close the otter-verify PDA account associated with the given program ID")
.arg(Arg::with_name("program-id")
Expand Down Expand Up @@ -278,6 +299,28 @@ async fn main() -> anyhow::Result<()> {
)
.await
}
("write", Some(sub_m)) => {
let program_id = sub_m.value_of("program-id").unwrap();
let git_url = sub_m.value_of("git-url").unwrap();
let commit = sub_m.value_of("commit").map(|s| s.to_string());
// Check program id is valid
Pubkey::try_from(program_id).map_err(|_| anyhow!("Invalid program id"))?;

let flags: Vec<String> = sub_m
.values_of("flags")
.unwrap_or_default()
.map(|s| s.to_string())
.collect();

process_upload_program(
Pubkey::try_from(program_id)?,
git_url.to_string(),
&commit,
flags,
)
.await?;
Ok(())
}
("close", Some(sub_m)) => {
let program_id = sub_m.value_of("program-id").unwrap();
process_close(Pubkey::try_from(program_id)?).await
Expand Down
41 changes: 35 additions & 6 deletions src/solana_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::{

use borsh::{to_vec, BorshDeserialize, BorshSerialize};
use solana_sdk::{
instruction::AccountMeta, message::Message, pubkey::Pubkey, signature::Keypair, signer::Signer,
system_program, transaction::Transaction,
commitment_config::CommitmentConfig, instruction::AccountMeta, message::Message,
pubkey::Pubkey, signature::Keypair, signer::Signer, system_program, transaction::Transaction,
};

use crate::api::get_last_deployed_slot;
Expand Down Expand Up @@ -64,7 +64,12 @@ fn get_user_config() -> anyhow::Result<(Keypair, RpcClient)> {
let config_file = solana_cli_config::CONFIG_FILE
.as_ref()
.ok_or_else(|| anyhow!("Unable to get config file path"))?;
let cli_config: Config = Config::load(config_file)?;

let cli_config: Config = Config::load(config_file)
.map_err(|err| anyhow!(
"Unable to load config file {}: {}\nTry running `solana config set -um` to init a config if you haven't already.",
config_file, err
))?;

let signer = solana_clap_utils::keypair::keypair_from_path(
&Default::default(),
Expand Down Expand Up @@ -119,15 +124,34 @@ fn process_otter_verify_ixs(
tx.sign(&[&signer], connection.get_latest_blockhash()?);

let tx_id = connection
.send_and_confirm_transaction_with_spinner(&tx)
.send_and_confirm_transaction_with_spinner_and_commitment(
&tx,
CommitmentConfig::confirmed(),
)
.map_err(|err| {
println!("{:?}", err);
anyhow!("Failed to send transaction to the network.")
})?;
println!("Program uploaded successfully. Transaction ID: {}", tx_id);
println!(
"Program verification params uploaded successfully. Transaction ID: {}",
tx_id
);
Ok(())
}

pub async fn process_upload_program(
program_address: Pubkey,
git_url: String,
commit: &Option<String>,
args: Vec<String>,
) -> anyhow::Result<()> {
let user_config = get_user_config()?;
let connection = user_config.1;
let rpc_url = connection.url();

upload_program(git_url, commit, args, program_address, Some(rpc_url)).await
}

pub async fn upload_program(
git_url: String,
commit: &Option<String>,
Expand All @@ -136,7 +160,12 @@ pub async fn upload_program(
connection_url: Option<String>,
) -> anyhow::Result<()> {
if prompt_user_input(
"Do you want to upload the program verification to the Solana Blockchain? (y/n) ",
&format!("Do you want to upload these parameters for program verification to the Solana Blockchain?\nProgram ID: {}\nGit URL: {}\nCommit Hash: {}\nArgs: {:?}\n(y/n) ",
program_address,
git_url,
commit.as_ref().unwrap_or(&"None".to_string()),
args,
),
) {
println!("Uploading the program verification params to the Solana blockchain...");

Expand Down

0 comments on commit 7a9be2d

Please sign in to comment.