1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
pub mod parse_token;
pub mod parse_token_extension;

use std::str::FromStr;

use serde_json::Value;
use solana_sdk::{
    account::{ReadableAccount, WritableAccount},
    clock::Epoch,
    instruction::InstructionError,
    pubkey::Pubkey,
};
// TODO:
// use spl_token_2022::extension::{self, BaseState, ExtensionType, StateWithExtensions};
use thiserror::Error;

pub type StringAmount = String;
pub type StringDecimals = String;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UiDataSliceConfig {
    pub offset: usize,
    pub length: usize,
}

fn slice_data(data: &[u8], data_slice_config: Option<UiDataSliceConfig>) -> &[u8] {
    if let Some(UiDataSliceConfig { offset, length }) = data_slice_config {
        if offset >= data.len() {
            &[]
        } else if length > data.len() - offset {
            &data[offset..]
        } else {
            &data[offset..offset + length]
        }
    } else {
        data
    }
}

/// A duplicate representation of an Account for pretty JSON serialization
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[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, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum UiAccountData {
    LegacyBinary(String), // Legacy. Retained for RPC backwards compatibility
    Json(ParsedAccount),
    Binary(String, UiAccountEncoding),
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum UiAccountEncoding {
    Binary, // Legacy. Retained for RPC backwards compatibility
    Base58,
    Base64,
    JsonParsed,
    // NOTE: Not supported in WASM
    // #[serde(rename = "base64+zstd")]
    // Base64Zstd,
}

pub const MAX_BASE58_BYTES: usize = 128;

impl UiAccount {
    fn encode_bs58<T: ReadableAccount>(
        account: &T,
        data_slice_config: Option<UiDataSliceConfig>,
    ) -> String {
        if account.data().len() <= MAX_BASE58_BYTES {
            bs58::encode(slice_data(account.data(), data_slice_config)).into_string()
        } else {
            "error: data too large for bs58 encoding".to_string()
        }
    }

    pub fn encode<T: ReadableAccount>(
        pubkey: &Pubkey,
        account: &T,
        encoding: UiAccountEncoding,
        additional_data: Option<AccountAdditionalData>,
        data_slice_config: Option<UiDataSliceConfig>,
    ) -> Self {
        let data = match encoding {
            UiAccountEncoding::Binary => {
                let data = Self::encode_bs58(account, data_slice_config);
                UiAccountData::LegacyBinary(data)
            }
            UiAccountEncoding::Base58 => {
                let data = Self::encode_bs58(account, data_slice_config);
                UiAccountData::Binary(data, encoding)
            }
            UiAccountEncoding::Base64 => UiAccountData::Binary(
                base64::encode(slice_data(account.data(), data_slice_config)),
                encoding,
            ),
            // NOTE: Not supported in WASM
            // UiAccountEncoding::Base64Zstd => {
            //     let mut encoder = zstd::stream::write::Encoder::new(Vec::new(), 0).unwrap();
            //     match encoder
            //         .write_all(slice_data(account.data(), data_slice_config))
            //         .and_then(|()| encoder.finish())
            //     {
            //         Ok(zstd_data) => UiAccountData::Binary(base64::encode(zstd_data), encoding),
            //         Err(_) => UiAccountData::Binary(
            //             base64::encode(slice_data(account.data(), data_slice_config)),
            //             UiAccountEncoding::Base64,
            //         ),
            //     }
            // }
            UiAccountEncoding::JsonParsed => {
                if let Ok(parsed_data) =
                    parse_account_data(pubkey, account.owner(), account.data(), additional_data)
                {
                    UiAccountData::Json(parsed_data)
                } else {
                    UiAccountData::Binary(
                        base64::encode(&account.data()),
                        UiAccountEncoding::Base64,
                    )
                }
            }
        };
        UiAccount {
            lamports: account.lamports(),
            data,
            owner: account.owner().to_string(),
            executable: account.executable(),
            rent_epoch: account.rent_epoch(),
        }
    }

    pub fn decode<T: WritableAccount>(&self) -> Option<T> {
        let data = match &self.data {
            UiAccountData::Json(_) => None,
            UiAccountData::LegacyBinary(blob) => bs58::decode(blob).into_vec().ok(),
            UiAccountData::Binary(blob, encoding) => match encoding {
                UiAccountEncoding::Base58 => bs58::decode(blob).into_vec().ok(),
                UiAccountEncoding::Base64 => base64::decode(blob).ok(),
                // NOTE: Not supported in WASM
                // UiAccountEncoding::Base64Zstd => base64::decode(blob).ok().and_then(|zstd_data| {
                //     let mut data = vec![];
                //     zstd::stream::read::Decoder::new(zstd_data.as_slice())
                //         .and_then(|mut reader| reader.read_to_end(&mut data))
                //         .map(|_| data)
                //         .ok()
                // }),
                UiAccountEncoding::Binary | UiAccountEncoding::JsonParsed => None,
            },
        }?;
        Some(T::create(
            self.lamports,
            data,
            Pubkey::from_str(&self.owner).ok()?,
            self.executable,
            self.rent_epoch,
        ))
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ParsedAccount {
    pub program: String,
    pub parsed: Value,
    pub space: u64,
}

#[derive(Default)]
pub struct AccountAdditionalData {
    pub spl_token_decimals: Option<u8>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ParsableAccount {
    BpfUpgradeableLoader,
    Config,
    Nonce,
    SplToken,
    SplToken2022,
    Stake,
    Sysvar,
    Vote,
}

#[derive(Error, Debug)]
pub enum ParseAccountError {
    #[error("{0:?} account not parsable")]
    AccountNotParsable(ParsableAccount),

    #[error("Program not parsable")]
    ProgramNotParsable,

    #[error("Additional data required to parse: {0}")]
    AdditionalDataMissing(String),

    #[error("Instruction error")]
    InstructionError(#[from] InstructionError),

    #[error("Serde json error")]
    SerdeJsonError(#[from] serde_json::error::Error),
}

pub fn parse_account_data(
    _pubkey: &Pubkey,
    _program_id: &Pubkey,
    _data: &[u8],
    _additional_data: Option<AccountAdditionalData>,
) -> Result<ParsedAccount, ParseAccountError> {
    // TODO:
    Err(ParseAccountError::ProgramNotParsable)
    // let program_name = PARSABLE_PROGRAM_IDS
    //     .get(program_id)
    //     .ok_or(ParseAccountError::ProgramNotParsable)?;
    // let additional_data = additional_data.unwrap_or_default();
    // let parsed_json = match program_name {
    //     ParsableAccount::BpfUpgradeableLoader => {
    //         serde_json::to_value(parse_bpf_upgradeable_loader(data)?)?
    //     }
    //     ParsableAccount::Config => serde_json::to_value(parse_config(data, pubkey)?)?,
    //     ParsableAccount::Nonce => serde_json::to_value(parse_nonce(data)?)?,
    //     ParsableAccount::SplToken | ParsableAccount::SplToken2022 => {
    //         serde_json::to_value(parse_token(data, additional_data.spl_token_decimals)?)?
    //     }
    //     ParsableAccount::Stake => serde_json::to_value(parse_stake(data)?)?,
    //     ParsableAccount::Sysvar => serde_json::to_value(parse_sysvar(data, pubkey)?)?,
    //     ParsableAccount::Vote => serde_json::to_value(parse_vote(data)?)?,
    // };
    // Ok(ParsedAccount {
    //     program: format!("{:?}", program_name).to_kebab_case(),
    //     parsed: parsed_json,
    //     space: data.len() as u64,
    // })
}