2020-06-30 22:55:11 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
|
|
|
pub mod parse_account_data;
|
|
|
|
pub mod parse_nonce;
|
2020-07-24 17:45:21 -06:00
|
|
|
pub mod parse_token;
|
2020-06-30 22:55:11 -06:00
|
|
|
pub mod parse_vote;
|
|
|
|
|
2020-08-07 11:37:39 -06:00
|
|
|
use crate::parse_account_data::{parse_account_data, AccountAdditionalData, ParsedAccount};
|
2020-06-30 22:55:11 -06:00
|
|
|
use solana_sdk::{account::Account, clock::Epoch, pubkey::Pubkey};
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2020-08-07 11:37:39 -06:00
|
|
|
pub type StringAmount = String;
|
|
|
|
|
2020-06-30 22:55:11 -06:00
|
|
|
/// 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),
|
2020-08-05 00:59:10 -06:00
|
|
|
Json(ParsedAccount),
|
2020-08-08 22:40:13 -07:00
|
|
|
Binary64(String),
|
2020-06-30 22:55:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<u8>> for UiAccountData {
|
|
|
|
fn from(data: Vec<u8>) -> Self {
|
|
|
|
Self::Binary(bs58::encode(data).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum UiAccountEncoding {
|
|
|
|
Binary,
|
|
|
|
JsonParsed,
|
2020-08-08 22:40:13 -07:00
|
|
|
Binary64,
|
2020-06-30 22:55:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UiAccount {
|
2020-08-07 11:37:39 -06:00
|
|
|
pub fn encode(
|
|
|
|
account: Account,
|
|
|
|
encoding: UiAccountEncoding,
|
|
|
|
additional_data: Option<AccountAdditionalData>,
|
|
|
|
) -> Self {
|
2020-06-30 22:55:11 -06:00
|
|
|
let data = match encoding {
|
|
|
|
UiAccountEncoding::Binary => account.data.into(),
|
2020-08-08 22:40:13 -07:00
|
|
|
UiAccountEncoding::Binary64 => UiAccountData::Binary64(base64::encode(account.data)),
|
2020-06-30 22:55:11 -06:00
|
|
|
UiAccountEncoding::JsonParsed => {
|
2020-08-07 11:37:39 -06:00
|
|
|
if let Ok(parsed_data) =
|
|
|
|
parse_account_data(&account.owner, &account.data, additional_data)
|
|
|
|
{
|
2020-06-30 22:55:11 -06:00
|
|
|
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<Account> {
|
|
|
|
let data = match &self.data {
|
|
|
|
UiAccountData::Json(_) => None,
|
|
|
|
UiAccountData::Binary(blob) => bs58::decode(blob).into_vec().ok(),
|
2020-08-08 22:40:13 -07:00
|
|
|
UiAccountData::Binary64(blob) => base64::decode(blob).ok(),
|
2020-06-30 22:55:11 -06:00
|
|
|
}?;
|
|
|
|
Some(Account {
|
|
|
|
lamports: self.lamports,
|
|
|
|
data,
|
|
|
|
owner: Pubkey::from_str(&self.owner).ok()?,
|
|
|
|
executable: self.executable,
|
|
|
|
rent_epoch: self.rent_epoch,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|