token-cli: convert multisig display to UiMultisig

This commit is contained in:
hanako mumei 2022-09-09 10:58:01 -07:00 committed by hana
parent bdc3087533
commit f8d1503acf
2 changed files with 20 additions and 41 deletions

View File

@ -4,7 +4,7 @@ use clap::{
}; };
use serde::Serialize; use serde::Serialize;
use solana_account_decoder::{ use solana_account_decoder::{
parse_token::{TokenAccountType, UiAccountState}, parse_token::{parse_token, TokenAccountType, UiAccountState},
UiAccountData, UiAccountData,
}; };
use solana_clap_utils::{ use solana_clap_utils::{
@ -1645,43 +1645,19 @@ async fn command_account_info(config: &Config<'_>, address: Pubkey) -> CommandRe
async fn command_display(config: &Config<'_>, address: Pubkey) -> CommandResult { async fn command_display(config: &Config<'_>, address: Pubkey) -> CommandResult {
let account = config.get_account_checked(&address).await?; let account = config.get_account_checked(&address).await?;
let token_data = parse_token(&account.data, None);
let cli_output = if let Ok(multisig) = Multisig::unpack(&account.data) { if let Ok(TokenAccountType::Multisig(multisig)) = token_data {
let n = multisig.n as usize; let cli_output = CliMultisig {
if n > multisig.signers.len() {
return Err(format!(
"Multisig {} malformed: {} signers required, but only {} declared",
address,
n,
multisig.signers.len()
)
.into());
}
CliMultisig {
address: address.to_string(), address: address.to_string(),
program_id: config.program_id.to_string(), program_id: config.program_id.to_string(),
m: multisig.m, multisig,
n: multisig.n, };
signers: multisig
.signers Ok(config.output_format.formatted_string(&cli_output))
.iter()
.enumerate()
.filter_map(|(i, signer)| {
if i < n {
Some(signer.to_string())
} else {
None
}
})
.collect(),
}
} else { } else {
panic!("unimplemented"); panic!("unimplemented");
}; }
Ok(config.output_format.formatted_string(&cli_output))
} }
async fn command_gc( async fn command_gc(

View File

@ -1,7 +1,9 @@
use crate::{config::Config, sort::UnsupportedAccount}; use crate::{config::Config, sort::UnsupportedAccount};
use console::Emoji; use console::Emoji;
use serde::{Deserialize, Serialize, Serializer}; use serde::{Deserialize, Serialize, Serializer};
use solana_account_decoder::parse_token::{UiAccountState, UiTokenAccount, UiTokenAmount}; use solana_account_decoder::parse_token::{
UiAccountState, UiMultisig, UiTokenAccount, UiTokenAmount,
};
use solana_cli_output::{display::writeln_name_value, OutputFormat, QuietDisplay, VerboseDisplay}; use solana_cli_output::{display::writeln_name_value, OutputFormat, QuietDisplay, VerboseDisplay};
use std::fmt::{self, Display}; use std::fmt::{self, Display};
@ -149,9 +151,8 @@ impl fmt::Display for CliWalletAddress {
pub(crate) struct CliMultisig { pub(crate) struct CliMultisig {
pub(crate) address: String, pub(crate) address: String,
pub(crate) program_id: String, pub(crate) program_id: String,
pub(crate) m: u8, #[serde(flatten)]
pub(crate) n: u8, pub(crate) multisig: UiMultisig,
pub(crate) signers: Vec<String>,
} }
impl QuietDisplay for CliMultisig {} impl QuietDisplay for CliMultisig {}
@ -159,16 +160,18 @@ impl VerboseDisplay for CliMultisig {}
impl fmt::Display for CliMultisig { impl fmt::Display for CliMultisig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let m = self.multisig.num_required_signers;
let n = self.multisig.num_valid_signers;
writeln!(f)?; writeln!(f)?;
writeln_name_value(f, "Type:", "Multisig")?; writeln_name_value(f, "Type:", "Multisig")?;
writeln_name_value(f, "Address:", &self.address)?; writeln_name_value(f, "Address:", &self.address)?;
writeln_name_value(f, "Program:", &self.program_id)?; writeln_name_value(f, "Program:", &self.program_id)?;
writeln_name_value(f, "M/N:", &format!("{}/{}", self.m, self.n))?; writeln_name_value(f, "M/N:", &format!("{}/{}", m, n))?;
writeln_name_value(f, "Signers:", " ")?; writeln_name_value(f, "Signers:", " ")?;
let width = if self.n >= 9 { 4 } else { 3 }; let width = if n >= 9 { 4 } else { 3 };
for i in 0..self.n as usize { for i in 0..n as usize {
let title = format!("{1:>0$}:", width, i + 1); let title = format!("{1:>0$}:", width, i + 1);
let pubkey = &self.signers[i]; let pubkey = &self.multisig.signers[i];
writeln_name_value(f, &title, pubkey)?; writeln_name_value(f, &title, pubkey)?;
} }
Ok(()) Ok(())