Cli: Add binary to decode voter accounts

This commit is contained in:
Christian Kamm 2022-02-28 10:00:52 +01:00
parent 849aeba40a
commit c510f25b87
5 changed files with 190 additions and 5 deletions

79
Cargo.lock generated
View File

@ -117,7 +117,7 @@ checksum = "ac83f085b2be8b3a3412989cf96cf7f683561db7d357c5aa4aa11d48bbb22213"
dependencies = [
"anchor-syn",
"anyhow",
"heck",
"heck 0.3.3",
"proc-macro2",
"quote",
"syn",
@ -206,7 +206,7 @@ checksum = "3be7bfb6991d79cce3495fb6ce0892f58a5c75a74c8d1c2fc6f62926066eb9f4"
dependencies = [
"anyhow",
"bs58 0.3.1",
"heck",
"heck 0.3.3",
"proc-macro2",
"proc-macro2-diagnostics",
"quote",
@ -219,9 +219,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.51"
version = "1.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd"
[[package]]
name = "arrayref"
@ -520,6 +520,36 @@ dependencies = [
"chrono",
]
[[package]]
name = "clap"
version = "3.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5177fac1ab67102d8989464efd043c6ff44191b1557ec1ddd489b4f7e1447e77"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01d42c94ce7c2252681b5fed4d3627cc807b13dfc033246bd05d5b252399000e"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "combine"
version = "3.8.1"
@ -1126,6 +1156,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -1616,6 +1652,15 @@ dependencies = [
"thiserror",
]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]]
name = "ouroboros"
version = "0.13.0"
@ -2847,6 +2892,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.4.1"
@ -2951,6 +3002,12 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]]
name = "thiserror"
version = "1.0.30"
@ -3272,6 +3329,20 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "voter-stake-registry-cli"
version = "0.2.0"
dependencies = [
"anchor-lang",
"anyhow",
"base64 0.13.0",
"clap",
"log",
"serde",
"serde_json",
"voter-stake-registry",
]
[[package]]
name = "walkdir"
version = "2.3.2"

View File

@ -1,4 +1,5 @@
[workspace]
members = [
"programs/*"
"programs/*",
"cli/"
]

16
cli/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "voter-stake-registry-cli"
version = "0.2.0"
edition = "2021"
[dependencies]
voter-stake-registry = { path = "../programs/voter-stake-registry", features = ["no-entrypoint"] }
anchor-lang = "0.20.1"
serde = "^1.0"
serde_json = "^1.0"
log = "0.4.14"
base64 = "0.13.0"
anyhow = "1.0.55"
clap = { version = "3.1.2", features = ["derive"] }

74
cli/src/decode.rs Normal file
View File

@ -0,0 +1,74 @@
use anchor_lang::Discriminator;
use anyhow::{anyhow, bail, Result};
use serde::Serialize;
use std::collections::HashMap;
use std::io;
use std::io::BufRead;
use voter_stake_registry::state::*;
//
// Output format declarations. These are built from the decoded
// accounts and then converted to JSON.
//
#[derive(Serialize)]
struct DisplayDepositEntry {
allow_clawback: bool,
mint_index: u8,
unlocked: u64,
locked: u64,
}
#[derive(Serialize)]
struct DisplayVoter {
voter_authority: String,
registrar: String,
deposit_entries: Vec<DisplayDepositEntry>,
}
/// Decode a Voter account and print its JSON to stdout
fn decode_voter(data: &[u8]) -> Result<()> {
let mut data = data;
let voter: Voter = anchor_lang::AccountDeserialize::try_deserialize(&mut data)?;
let now_ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_secs() as i64;
let ser = DisplayVoter {
voter_authority: voter.voter_authority.to_string(),
registrar: voter.registrar.to_string(),
deposit_entries: voter
.deposits
.iter()
.filter(|d| d.is_used)
.map(|d| DisplayDepositEntry {
allow_clawback: d.allow_clawback,
mint_index: d.voting_mint_config_idx,
unlocked: d.amount_unlocked(now_ts),
locked: d.amount_locked(now_ts),
})
.collect(),
};
println!("{}", serde_json::to_string(&ser)?);
Ok(())
}
// Read a sequence of base64 encoded accounts from stdin
// and write their decoded versions back out as JSON.
pub fn decode_account() -> Result<()> {
let account_types = HashMap::from([(Voter::discriminator(), &decode_voter)]);
for line in io::stdin().lock().lines() {
let data = base64::decode(line?)?;
if data.len() < 8 {
bail!("data length {} too small for discriminator", data.len());
}
let discr = &data[0..8];
let handler = account_types
.get(discr)
.ok_or_else(|| anyhow!("discriminator {:?} not recognized", discr))?;
handler(&data)?;
}
Ok(())
}

23
cli/src/main.rs Normal file
View File

@ -0,0 +1,23 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
mod decode;
#[derive(Parser, Debug)]
#[clap(version)]
struct Args {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
DecodeAccount,
}
fn main() -> Result<()> {
let args = Args::parse();
match args.command {
Command::DecodeAccount => decode::decode_account(),
}
}