solana_account_decoder/
parse_address_lookup_table.rs

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
use {
    crate::parse_account_data::{ParsableAccount, ParseAccountError},
    solana_sdk::{address_lookup_table::state::AddressLookupTable, instruction::InstructionError},
};

pub fn parse_address_lookup_table(
    data: &[u8],
) -> Result<LookupTableAccountType, ParseAccountError> {
    AddressLookupTable::deserialize(data)
        .map(|address_lookup_table| {
            LookupTableAccountType::LookupTable(address_lookup_table.into())
        })
        .or_else(|err| match err {
            InstructionError::UninitializedAccount => Ok(LookupTableAccountType::Uninitialized),
            _ => Err(ParseAccountError::AccountNotParsable(
                ParsableAccount::AddressLookupTable,
            )),
        })
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", tag = "type", content = "info")]
pub enum LookupTableAccountType {
    Uninitialized,
    LookupTable(UiLookupTable),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct UiLookupTable {
    pub deactivation_slot: String,
    pub last_extended_slot: String,
    pub last_extended_slot_start_index: u8,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authority: Option<String>,
    pub addresses: Vec<String>,
}

impl<'a> From<AddressLookupTable<'a>> for UiLookupTable {
    fn from(address_lookup_table: AddressLookupTable) -> Self {
        Self {
            deactivation_slot: address_lookup_table.meta.deactivation_slot.to_string(),
            last_extended_slot: address_lookup_table.meta.last_extended_slot.to_string(),
            last_extended_slot_start_index: address_lookup_table
                .meta
                .last_extended_slot_start_index,
            authority: address_lookup_table
                .meta
                .authority
                .map(|authority| authority.to_string()),
            addresses: address_lookup_table
                .addresses
                .iter()
                .map(|address| address.to_string())
                .collect(),
        }
    }
}

#[cfg(test)]
mod test {
    use {
        super::*,
        solana_sdk::{
            address_lookup_table::state::{LookupTableMeta, LOOKUP_TABLE_META_SIZE},
            pubkey::Pubkey,
        },
        std::borrow::Cow,
    };

    #[test]
    fn test_parse_address_lookup_table() {
        let authority = Pubkey::new_unique();
        let deactivation_slot = 1;
        let last_extended_slot = 2;
        let last_extended_slot_start_index = 3;
        let lookup_table_meta = LookupTableMeta {
            deactivation_slot,
            last_extended_slot,
            last_extended_slot_start_index,
            authority: Some(authority),
            ..LookupTableMeta::default()
        };
        let num_addresses = 42;
        let mut addresses = Vec::with_capacity(num_addresses);
        addresses.resize_with(num_addresses, Pubkey::new_unique);
        let lookup_table = AddressLookupTable {
            meta: lookup_table_meta,
            addresses: Cow::Owned(addresses),
        };
        let lookup_table_data = AddressLookupTable::serialize_for_tests(lookup_table).unwrap();

        let parsing_result = parse_address_lookup_table(&lookup_table_data).unwrap();
        if let LookupTableAccountType::LookupTable(ui_lookup_table) = parsing_result {
            assert_eq!(
                ui_lookup_table.deactivation_slot,
                deactivation_slot.to_string()
            );
            assert_eq!(
                ui_lookup_table.last_extended_slot,
                last_extended_slot.to_string()
            );
            assert_eq!(
                ui_lookup_table.last_extended_slot_start_index,
                last_extended_slot_start_index
            );
            assert_eq!(ui_lookup_table.authority, Some(authority.to_string()));
            assert_eq!(ui_lookup_table.addresses.len(), num_addresses);
        }

        assert_eq!(
            parse_address_lookup_table(&[0u8; LOOKUP_TABLE_META_SIZE]).unwrap(),
            LookupTableAccountType::Uninitialized
        );
        assert!(parse_address_lookup_table(&[]).is_err());
    }
}