solana_transaction_status/
parse_accounts.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
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
use solana_sdk::{
    message::{v0::LoadedMessage, Message},
    reserved_account_keys::ReservedAccountKeys,
};
pub use solana_transaction_status_client_types::{ParsedAccount, ParsedAccountSource};

pub fn parse_legacy_message_accounts(message: &Message) -> Vec<ParsedAccount> {
    let reserved_account_keys = ReservedAccountKeys::new_all_activated().active;
    let mut accounts: Vec<ParsedAccount> = vec![];
    for (i, account_key) in message.account_keys.iter().enumerate() {
        accounts.push(ParsedAccount {
            pubkey: account_key.to_string(),
            writable: message.is_maybe_writable(i, Some(&reserved_account_keys)),
            signer: message.is_signer(i),
            source: Some(ParsedAccountSource::Transaction),
        });
    }
    accounts
}

pub fn parse_v0_message_accounts(message: &LoadedMessage) -> Vec<ParsedAccount> {
    let mut accounts: Vec<ParsedAccount> = vec![];
    for (i, account_key) in message.account_keys().iter().enumerate() {
        let source = if i < message.static_account_keys().len() {
            ParsedAccountSource::Transaction
        } else {
            ParsedAccountSource::LookupTable
        };
        accounts.push(ParsedAccount {
            pubkey: account_key.to_string(),
            writable: message.is_writable(i),
            signer: message.is_signer(i),
            source: Some(source),
        });
    }
    accounts
}

#[cfg(test)]
mod test {
    use {
        super::*,
        solana_sdk::{
            message::{v0, v0::LoadedAddresses, MessageHeader},
            pubkey::Pubkey,
            reserved_account_keys::ReservedAccountKeys,
        },
    };

    #[test]
    fn test_parse_legacy_message_accounts() {
        let pubkey0 = Pubkey::new_unique();
        let pubkey1 = Pubkey::new_unique();
        let pubkey2 = Pubkey::new_unique();
        let pubkey3 = Pubkey::new_unique();
        let message = Message {
            header: MessageHeader {
                num_required_signatures: 2,
                num_readonly_signed_accounts: 1,
                num_readonly_unsigned_accounts: 1,
            },
            account_keys: vec![pubkey0, pubkey1, pubkey2, pubkey3],
            ..Message::default()
        };

        assert_eq!(
            parse_legacy_message_accounts(&message),
            vec![
                ParsedAccount {
                    pubkey: pubkey0.to_string(),
                    writable: true,
                    signer: true,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey1.to_string(),
                    writable: false,
                    signer: true,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey2.to_string(),
                    writable: true,
                    signer: false,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey3.to_string(),
                    writable: false,
                    signer: false,
                    source: Some(ParsedAccountSource::Transaction),
                },
            ]
        );
    }

    #[test]
    fn test_parse_v0_message_accounts() {
        let pubkey0 = Pubkey::new_unique();
        let pubkey1 = Pubkey::new_unique();
        let pubkey2 = Pubkey::new_unique();
        let pubkey3 = Pubkey::new_unique();
        let pubkey4 = Pubkey::new_unique();
        let pubkey5 = Pubkey::new_unique();
        let message = LoadedMessage::new(
            v0::Message {
                header: MessageHeader {
                    num_required_signatures: 2,
                    num_readonly_signed_accounts: 1,
                    num_readonly_unsigned_accounts: 1,
                },
                account_keys: vec![pubkey0, pubkey1, pubkey2, pubkey3],
                ..v0::Message::default()
            },
            LoadedAddresses {
                writable: vec![pubkey4],
                readonly: vec![pubkey5],
            },
            &ReservedAccountKeys::empty_key_set(),
        );

        assert_eq!(
            parse_v0_message_accounts(&message),
            vec![
                ParsedAccount {
                    pubkey: pubkey0.to_string(),
                    writable: true,
                    signer: true,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey1.to_string(),
                    writable: false,
                    signer: true,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey2.to_string(),
                    writable: true,
                    signer: false,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey3.to_string(),
                    writable: false,
                    signer: false,
                    source: Some(ParsedAccountSource::Transaction),
                },
                ParsedAccount {
                    pubkey: pubkey4.to_string(),
                    writable: true,
                    signer: false,
                    source: Some(ParsedAccountSource::LookupTable),
                },
                ParsedAccount {
                    pubkey: pubkey5.to_string(),
                    writable: false,
                    signer: false,
                    source: Some(ParsedAccountSource::LookupTable),
                },
            ]
        );
    }
}