Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion svmgov/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ enum Commands {
/// Verbose vote list
#[arg(long, help = "List votes verbose", default_value_t = false)]
verbose: bool,

#[arg(
long,
help = "Filter votes by specific voter pubkey. If not provided, shows all votes for the proposal",
)]
pubkey: Option<String>,
},
}

Expand Down Expand Up @@ -288,8 +294,9 @@ async fn handle_command(cli: Cli) -> Result<()> {
Commands::ListVotes {
proposal_id,
verbose,
pubkey,
} => {
commands::list_votes(cli.rpc_url, proposal_id, *verbose).await?;
commands::list_votes(cli.rpc_url, &proposal_id, *verbose, pubkey.clone()).await?;
}
}

Expand Down
24 changes: 15 additions & 9 deletions svmgov/src/utils/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,31 @@ pub async fn list_proposals(

pub async fn list_votes(
rpc_url: Option<String>,
proposal_id: &String,
proposal_id: &str,
verbose: bool,
voter_pubkey: Option<String>,
) -> Result<()> {
// Parse the proposal ID into a Pubkey
let proposal_pubkey = Pubkey::from_str(&proposal_id)
let proposal_pubkey = Pubkey::from_str(proposal_id)
.map_err(|_| anyhow!("Invalid proposal ID: {}", proposal_id))?;
// Create a mock Payer
let mock_payer = Arc::new(Keypair::new());

// Create the Anchor client
let mock_payer = Arc::new(Keypair::new());
let program = anchor_client_setup(rpc_url, mock_payer)?;

// Rpc filter to get Vote accounts for this proposal
let filter = vec![RpcFilterType::Memcmp(Memcmp::new(
let mut filters = vec![RpcFilterType::Memcmp(Memcmp::new(
40,
MemcmpEncodedBytes::Bytes(proposal_pubkey.to_bytes().to_vec()),
))];

let votes = program.accounts::<Vote>(filter).await?;
if let Some(voter_key) = voter_pubkey {
let voter_pubkey = Pubkey::from_str(&voter_key)
.map_err(|_| anyhow!("Invalid voter pubkey: {}", voter_key))?;
filters.push(RpcFilterType::Memcmp(Memcmp::new(
8,
MemcmpEncodedBytes::Bytes(voter_pubkey.to_bytes().to_vec()),
)));
}

let votes = program.accounts::<Vote>(filters).await?;

if verbose {
for vote in votes {
Expand Down
14 changes: 2 additions & 12 deletions svmgov/src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,15 @@ impl fmt::Display for Proposal {
impl fmt::Display for Vote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let validator_str = self.validator.to_string();
let short_validator = format!(
"{}...{}",
&validator_str[..4],
&validator_str[validator_str.len() - 4..]
);
let proposal_str = self.proposal.to_string();
let short_proposal = format!(
"{}...{}",
&proposal_str[..4],
&proposal_str[proposal_str.len() - 4..]
);
let timestamp = Utc
.timestamp_opt(self.vote_timestamp, 0)
.single()
.unwrap_or_default();
let formatted_timestamp = timestamp.format("%Y-%m-%d %H:%M:%S UTC").to_string();

writeln!(f, "{:<15} {}", "Validator:", short_validator)?;
writeln!(f, "{:<15} {}", "Proposal:", short_proposal)?;
writeln!(f, "{:<15} {}", "Validator:", validator_str)?;
writeln!(f, "{:<15} {}", "Proposal:", proposal_str)?;
writeln!(
f,
"{:<15} {} bp ({:.2}%)",
Expand Down