From 063fcbfe2265bbd69dc71e09fecb92fa1934373c Mon Sep 17 00:00:00 2001 From: cavemanloverboy <93507302+cavemanloverboy@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:12:47 +0200 Subject: [PATCH] Cli improvements (#232) * add delete profile feature to mfi cli * add profile renaming * optional feed id * add cu price option for add_bank ix * fix: add optional feed id for bank creation with seed * fix: obsolete option name * delete only if new name --------- Co-authored-by: man0s <95379755+losman0s@users.noreply.github.com> --- clients/rust/marginfi-cli/src/config.rs | 3 ++ clients/rust/marginfi-cli/src/entrypoint.rs | 19 ++++++++++-- .../rust/marginfi-cli/src/processor/mod.rs | 30 +++++++++++++++++-- clients/rust/marginfi-cli/src/profile.rs | 25 ++++++++++++++++ 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/clients/rust/marginfi-cli/src/config.rs b/clients/rust/marginfi-cli/src/config.rs index cfc29e6c..20066f54 100644 --- a/clients/rust/marginfi-cli/src/config.rs +++ b/clients/rust/marginfi-cli/src/config.rs @@ -37,6 +37,9 @@ pub struct GlobalOptions { default_value_t = false )] pub skip_confirmation: bool, + + #[clap(global = true, long)] + pub compute_unit_price: Option, } #[derive(Copy, Clone, Debug)] diff --git a/clients/rust/marginfi-cli/src/entrypoint.rs b/clients/rust/marginfi-cli/src/entrypoint.rs index 5013eee6..1956d0d7 100644 --- a/clients/rust/marginfi-cli/src/entrypoint.rs +++ b/clients/rust/marginfi-cli/src/entrypoint.rs @@ -115,7 +115,9 @@ pub enum GroupCommand { #[clap(long)] borrow_limit_ui: u64, #[clap(long)] - pyth_oracle: Pubkey, + oracle_key: Pubkey, + #[clap(long)] + feed_id: Option, #[clap(long)] optimal_utilization_rate: f64, #[clap(long)] @@ -337,6 +339,8 @@ pub enum ProfileCommand { Update { name: String, #[clap(long)] + new_name: Option, + #[clap(long)] cluster: Option, #[clap(long)] keypair_path: Option, @@ -353,6 +357,9 @@ pub enum ProfileCommand { #[clap(long)] account: Option, }, + Delete { + name: String, + }, } #[derive(Debug, Parser)] @@ -491,9 +498,11 @@ fn profile(subcmd: ProfileCommand) -> Result<()> { commitment, group, name, + new_name, account, } => processor::configure_profile( name, + new_name, cluster, keypair_path, multisig, @@ -503,6 +512,7 @@ fn profile(subcmd: ProfileCommand) -> Result<()> { group, account, ), + ProfileCommand::Delete { name } => processor::delete_profile(name), } } @@ -539,7 +549,8 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> { asset_weight_maint, liability_weight_init, liability_weight_maint, - pyth_oracle, + oracle_key, + feed_id, optimal_utilization_rate, plateau_interest_rate, max_interest_rate, @@ -557,7 +568,8 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> { profile, bank_mint, seed, - pyth_oracle, + oracle_key, + feed_id, oracle_type, asset_weight_init, asset_weight_maint, @@ -574,6 +586,7 @@ fn group(subcmd: GroupCommand, global_options: &GlobalOptions) -> Result<()> { protocol_ir_fee, risk_tier, oracle_max_age, + global_options.compute_unit_price, ), GroupCommand::HandleBankruptcy { accounts } => { diff --git a/clients/rust/marginfi-cli/src/processor/mod.rs b/clients/rust/marginfi-cli/src/processor/mod.rs index 0bcca4eb..e94dde18 100644 --- a/clients/rust/marginfi-cli/src/processor/mod.rs +++ b/clients/rust/marginfi-cli/src/processor/mod.rs @@ -301,6 +301,7 @@ pub fn group_add_bank( bank_mint: Pubkey, seed: bool, oracle_key: Pubkey, + feed_id: Option, oracle_setup: crate::OracleTypeArg, asset_weight_init: f64, asset_weight_maint: f64, @@ -317,6 +318,7 @@ pub fn group_add_bank( protocol_ir_fee: f64, risk_tier: crate::RiskTierArg, oracle_max_age: u16, + compute_unit_price: Option, ) -> Result<()> { let rpc_client = config.mfi_program.rpc(); @@ -373,6 +375,7 @@ pub fn group_add_bank( bank_mint, token_program, oracle_key, + feed_id, asset_weight_init, asset_weight_maint, liability_weight_init, @@ -392,6 +395,7 @@ pub fn group_add_bank( token_program, &bank_keypair, oracle_key, + feed_id, asset_weight_init, asset_weight_maint, liability_weight_init, @@ -405,7 +409,9 @@ pub fn group_add_bank( )? }; - let mut ixs = vec![ComputeBudgetInstruction::set_compute_unit_price(1)]; + let mut ixs = vec![ComputeBudgetInstruction::set_compute_unit_price( + compute_unit_price.unwrap_or(1), + )]; ixs.extend(add_bank_ixs); let recent_blockhash = rpc_client.get_latest_blockhash().unwrap(); @@ -430,6 +436,7 @@ fn create_bank_ix_with_seed( bank_mint: Pubkey, token_program: Pubkey, oracle_key: Pubkey, + feed_id: Option, asset_weight_init: WrappedI80F48, asset_weight_maint: WrappedI80F48, liability_weight_init: WrappedI80F48, @@ -522,7 +529,7 @@ fn create_bank_ix_with_seed( interest_rate_config, operational_state: BankOperationalState::Operational, oracle_setup: oracle_setup.into(), - oracle_keys: create_oracle_key_array(oracle_key), + oracle_keys: create_oracle_key_array(feed_id.unwrap_or(oracle_key)), risk_tier: risk_tier.into(), oracle_max_age, ..BankConfig::default() @@ -546,6 +553,7 @@ fn create_bank_ix( token_program: Pubkey, bank_keypair: &Keypair, oracle_key: Pubkey, + feed_id: Option, asset_weight_init: WrappedI80F48, asset_weight_maint: WrappedI80F48, liability_weight_init: WrappedI80F48, @@ -617,7 +625,7 @@ fn create_bank_ix( interest_rate_config, operational_state: BankOperationalState::Operational, oracle_setup: oracle_setup.into(), - oracle_keys: create_oracle_key_array(oracle_key), + oracle_keys: create_oracle_key_array(feed_id.unwrap_or(oracle_key)), risk_tier: risk_tier.into(), oracle_max_age, ..BankConfig::default() @@ -1672,6 +1680,7 @@ pub fn list_profiles() -> Result<()> { #[allow(clippy::too_many_arguments)] pub fn configure_profile( name: String, + new_name: Option, cluster: Option, keypair_path: Option, multisig: Option, @@ -1682,7 +1691,9 @@ pub fn configure_profile( account: Option, ) -> Result<()> { let mut profile = profile::load_profile_by_name(&name)?; + let using_new_name = new_name.is_some(); profile.config( + new_name, cluster, keypair_path, multisig, @@ -1693,9 +1704,20 @@ pub fn configure_profile( account, )?; + if using_new_name { + if let Err(e) = profile::delete_profile_by_name(&name) { + println!("failed to delete old profile {name}: {e:?}"); + return Err(e); + } + } + Ok(()) } +pub fn delete_profile(name: String) -> Result<()> { + profile::delete_profile_by_name(&name) +} + // -------------------------------------------------------------------------------------------------------------------- // Marginfi Accounts // -------------------------------------------------------------------------------------------------------------------- @@ -1821,6 +1843,7 @@ pub fn marginfi_account_use( None, None, None, + None, Some(marginfi_account_pk), )?; @@ -2291,6 +2314,7 @@ pub fn marginfi_account_create(profile: &Profile, config: &Config) -> Result<()> None, None, None, + None, Some(marginfi_account_key.pubkey()), )?; diff --git a/clients/rust/marginfi-cli/src/profile.rs b/clients/rust/marginfi-cli/src/profile.rs index 73a3bcba..56d6bb5d 100644 --- a/clients/rust/marginfi-cli/src/profile.rs +++ b/clients/rust/marginfi-cli/src/profile.rs @@ -115,6 +115,7 @@ impl Profile { #[allow(clippy::too_many_arguments)] pub fn config( &mut self, + new_name: Option, cluster: Option, keypair_path: Option, multisig: Option, @@ -124,6 +125,10 @@ impl Profile { group: Option, account: Option, ) -> Result<()> { + if let Some(name) = new_name { + self.name = name; + } + if let Some(cluster) = cluster { self.cluster = cluster; } @@ -226,6 +231,26 @@ pub fn load_profile_by_name(name: &str) -> Result { Ok(profile) } +pub fn delete_profile_by_name(name: &str) -> Result<()> { + let cli_config_dir = get_cli_config_dir(); + let profile_file = cli_config_dir.join("profiles").join(format!("{name}.json")); + + if !profile_file.exists() { + return Err(anyhow!("Profile {} does not exist", name)); + } + + match fs::remove_file(profile_file) { + Ok(()) => { + println!("successfully deleted profile {name}"); + Ok(()) + } + Err(e) => { + println!("failed to delete profile {name}: {e:?}"); + Err(e.into()) + } + } +} + pub fn get_cli_config_dir() -> PathBuf { home_dir() .expect("$HOME not set")