Skip to content

Commit

Permalink
Cli improvements (#232)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
cavemanloverboy and losman0s authored Aug 22, 2024
1 parent f0feb6e commit 063fcbf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
3 changes: 3 additions & 0 deletions clients/rust/marginfi-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub struct GlobalOptions {
default_value_t = false
)]
pub skip_confirmation: bool,

#[clap(global = true, long)]
pub compute_unit_price: Option<u64>,
}

#[derive(Copy, Clone, Debug)]
Expand Down
19 changes: 16 additions & 3 deletions clients/rust/marginfi-cli/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pubkey>,
#[clap(long)]
optimal_utilization_rate: f64,
#[clap(long)]
Expand Down Expand Up @@ -337,6 +339,8 @@ pub enum ProfileCommand {
Update {
name: String,
#[clap(long)]
new_name: Option<String>,
#[clap(long)]
cluster: Option<Cluster>,
#[clap(long)]
keypair_path: Option<String>,
Expand All @@ -353,6 +357,9 @@ pub enum ProfileCommand {
#[clap(long)]
account: Option<Pubkey>,
},
Delete {
name: String,
},
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -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,
Expand All @@ -503,6 +512,7 @@ fn profile(subcmd: ProfileCommand) -> Result<()> {
group,
account,
),
ProfileCommand::Delete { name } => processor::delete_profile(name),
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 } => {
Expand Down
30 changes: 27 additions & 3 deletions clients/rust/marginfi-cli/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ pub fn group_add_bank(
bank_mint: Pubkey,
seed: bool,
oracle_key: Pubkey,
feed_id: Option<Pubkey>,
oracle_setup: crate::OracleTypeArg,
asset_weight_init: f64,
asset_weight_maint: f64,
Expand All @@ -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<u64>,
) -> Result<()> {
let rpc_client = config.mfi_program.rpc();

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -430,6 +436,7 @@ fn create_bank_ix_with_seed(
bank_mint: Pubkey,
token_program: Pubkey,
oracle_key: Pubkey,
feed_id: Option<Pubkey>,
asset_weight_init: WrappedI80F48,
asset_weight_maint: WrappedI80F48,
liability_weight_init: WrappedI80F48,
Expand Down Expand Up @@ -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()
Expand All @@ -546,6 +553,7 @@ fn create_bank_ix(
token_program: Pubkey,
bank_keypair: &Keypair,
oracle_key: Pubkey,
feed_id: Option<Pubkey>,
asset_weight_init: WrappedI80F48,
asset_weight_maint: WrappedI80F48,
liability_weight_init: WrappedI80F48,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1672,6 +1680,7 @@ pub fn list_profiles() -> Result<()> {
#[allow(clippy::too_many_arguments)]
pub fn configure_profile(
name: String,
new_name: Option<String>,
cluster: Option<Cluster>,
keypair_path: Option<String>,
multisig: Option<Pubkey>,
Expand All @@ -1682,7 +1691,9 @@ pub fn configure_profile(
account: Option<Pubkey>,
) -> 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,
Expand All @@ -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
// --------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1821,6 +1843,7 @@ pub fn marginfi_account_use(
None,
None,
None,
None,
Some(marginfi_account_pk),
)?;

Expand Down Expand Up @@ -2291,6 +2314,7 @@ pub fn marginfi_account_create(profile: &Profile, config: &Config) -> Result<()>
None,
None,
None,
None,
Some(marginfi_account_key.pubkey()),
)?;

Expand Down
25 changes: 25 additions & 0 deletions clients/rust/marginfi-cli/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl Profile {
#[allow(clippy::too_many_arguments)]
pub fn config(
&mut self,
new_name: Option<String>,
cluster: Option<Cluster>,
keypair_path: Option<String>,
multisig: Option<Pubkey>,
Expand All @@ -124,6 +125,10 @@ impl Profile {
group: Option<Pubkey>,
account: Option<Pubkey>,
) -> Result<()> {
if let Some(name) = new_name {
self.name = name;
}

if let Some(cluster) = cluster {
self.cluster = cluster;
}
Expand Down Expand Up @@ -226,6 +231,26 @@ pub fn load_profile_by_name(name: &str) -> Result<Profile> {
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")
Expand Down

0 comments on commit 063fcbf

Please sign in to comment.