Skip to content

Commit

Permalink
feat: update cli to work better with ms authorities and explicit fee …
Browse files Browse the repository at this point in the history
…payers
  • Loading branch information
jkbpvsc committed Nov 29, 2023
1 parent a6f3949 commit 78e2c73
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 174 deletions.
64 changes: 51 additions & 13 deletions clients/rust/marginfi-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,17 @@ pub struct GlobalOptions {
pub skip_confirmation: bool,
}

pub enum CliSigner {
Keypair(Keypair),
Multisig(Pubkey),
}

impl CliSigner {
pub fn pubkey(&self) -> Pubkey {
match self {
CliSigner::Keypair(keypair) => keypair.pubkey(),
CliSigner::Multisig(pubkey) => *pubkey,
}
}
#[derive(Copy, Clone, Debug)]
pub enum TxMode {
DryRun,
Multisig,
Normal,
}

pub struct Config {
pub cluster: Cluster,
pub signer: CliSigner,
pub fee_payer: Keypair,
pub multisig: Option<Pubkey>,
pub program_id: Pubkey,
pub commitment: CommitmentConfig,
pub dry_run: bool,
Expand All @@ -63,6 +57,50 @@ pub struct Config {
pub lip_program: Program,
}

impl Config {
/// Use this only for transations that have a separate fee payer and authority.
pub fn explicit_fee_payer(&self) -> Pubkey {
self.fee_payer.pubkey()
}

/// Either the fee payer or the multisig authority.
pub fn authority(&self) -> Pubkey {
if let Some(multisig) = &self.multisig {
*multisig
} else {
self.fee_payer.pubkey()
}
}

pub fn get_tx_mode(&self) -> TxMode {
if self.dry_run {
TxMode::DryRun
} else if self.multisig.is_some() {
TxMode::Multisig
} else {
TxMode::Normal
}
}

pub fn get_signers(&self, explicit_fee_payer: bool) -> Vec<&Keypair> {
if explicit_fee_payer || self.multisig.is_none() {
vec![&self.fee_payer]
} else {
vec![]
}
}

/// Get the authority keypair for signing transactions.
/// This errors if the authority is a multisig.
pub fn get_authority_keypair(&self) -> anyhow::Result<&Keypair> {
if self.multisig.is_none() {
Ok(&self.fee_payer)
} else {
Err(anyhow::anyhow!("Cannot get authority keypair for multisig"))
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountEntry {
// Base58 pubkey string.
Expand Down
13 changes: 3 additions & 10 deletions clients/rust/marginfi-cli/src/processor/emissions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use {
crate::{
config::{CliSigner, Config},
profile::Profile,
},
crate::{config::Config, profile::Profile},
anchor_client::anchor_lang::{AnchorSerialize, InstructionData, ToAccountMetas},
anyhow::Result,
marginfi::state::marginfi_account::MarginfiAccount,
Expand All @@ -23,11 +20,7 @@ pub fn claim_all_emissions_for_bank(

let group = profile.marginfi_group.expect("group not set");

let signing_keypairs = if let CliSigner::Keypair(keypair) = &config.signer {
vec![keypair]
} else {
vec![]
};
let signing_keypairs = config.get_signers(false);

let marginfi_accounts =
config
Expand Down Expand Up @@ -73,7 +66,7 @@ pub fn claim_all_emissions_for_bank(
for (i, ixs) in ixs_batches.enumerate() {
let blockhash = rpc_client.get_latest_blockhash()?;

let message = Message::new(ixs, Some(&config.signer.pubkey()));
let message = Message::new(ixs, Some(&config.authority()));
let mut transaction = Transaction::new_unsigned(message);
transaction.partial_sign(&signing_keypairs, blockhash);

Expand Down
Loading

0 comments on commit 78e2c73

Please sign in to comment.