#[macro_use] extern crate lazy_static; #[macro_use] extern crate serde_derive; pub mod parse_account_data; pub mod parse_nonce; pub mod parse_token; pub mod parse_vote; use crate::parse_account_data::{parse_account_data, ParsedAccount}; use solana_sdk::{account::Account, clock::Epoch, pubkey::Pubkey}; use std::str::FromStr; /// A duplicate representation of an Account for pretty JSON serialization #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(rename_all = "camelCase")] pub struct UiAccount { pub lamports: u64, pub data: UiAccountData, pub owner: String, pub executable: bool, pub rent_epoch: Epoch, } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase", untagged)] pub enum UiAccountData { Binary(String), Json(ParsedAccount), } impl From> for UiAccountData { fn from(data: Vec) -> Self { Self::Binary(bs58::encode(data).into_string()) } } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(rename_all = "camelCase")] pub enum UiAccountEncoding { Binary, JsonParsed, } impl UiAccount { pub fn encode(account: Account, encoding: UiAccountEncoding) -> Self { let data = match encoding { UiAccountEncoding::Binary => account.data.into(), UiAccountEncoding::JsonParsed => { if let Ok(parsed_data) = parse_account_data(&account.owner, &account.data) { UiAccountData::Json(parsed_data) } else { account.data.into() } } }; UiAccount { lamports: account.lamports, data, owner: account.owner.to_string(), executable: account.executable, rent_epoch: account.rent_epoch, } } pub fn decode(&self) -> Option { let data = match &self.data { UiAccountData::Json(_) => None, UiAccountData::Binary(blob) => bs58::decode(blob).into_vec().ok(), }?; Some(Account { lamports: self.lamports, data, owner: Pubkey::from_str(&self.owner).ok()?, executable: self.executable, rent_epoch: self.rent_epoch, }) } }