solana-pipkit 2.2.0

Pragmatic Rust utilities for Solana development - safety, speed, analytics, and DeFi
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Account graph traversal utilities.
//!
//! This module provides utilities for building and traversing
//! account relationship graphs on Solana.

use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
    account::Account,
    commitment_config::CommitmentConfig,
    pubkey::Pubkey,
};
use std::collections::{HashMap, HashSet, VecDeque};

use crate::{Result, ToolkitError};

/// Represents a node in the account graph.
#[derive(Debug, Clone)]
pub struct AccountNode {
    /// The account's public key.
    pub pubkey: Pubkey,
    /// The account's owner (program).
    pub owner: Pubkey,
    /// Lamports balance.
    pub lamports: u64,
    /// Data length.
    pub data_len: usize,
    /// Whether this is a program account.
    pub is_program: bool,
    /// Optional: parsed account type.
    pub account_type: Option<AccountNodeType>,
}

/// Type classification for account nodes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccountNodeType {
    /// System program owned account.
    SystemAccount,
    /// SPL Token account.
    TokenAccount {
        mint: Pubkey,
        owner: Pubkey,
        amount: u64,
    },
    /// SPL Token mint.
    TokenMint {
        supply: u64,
        decimals: u8,
    },
    /// Metaplex metadata account.
    Metadata { mint: Pubkey },
    /// Program account.
    Program,
    /// Associated token account.
    AssociatedTokenAccount {
        wallet: Pubkey,
        mint: Pubkey,
    },
    /// Unknown account type.
    Unknown,
}

/// Edge type in the account graph.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EdgeType {
    /// Program owns this account.
    OwnedBy,
    /// Token account for this mint.
    TokenAccountOf,
    /// Authority relationship.
    Authority,
    /// Metadata for token.
    MetadataOf,
    /// Associated token account relationship.
    AssociatedWith,
    /// Generic relationship.
    Related,
}

/// An edge in the account graph.
#[derive(Debug, Clone)]
pub struct AccountEdge {
    /// Source account.
    pub from: Pubkey,
    /// Target account.
    pub to: Pubkey,
    /// Type of relationship.
    pub edge_type: EdgeType,
}

/// Account graph for tracking relationships.
#[derive(Debug, Default)]
pub struct AccountGraph {
    /// All nodes in the graph.
    nodes: HashMap<Pubkey, AccountNode>,
    /// Edges indexed by source.
    edges_from: HashMap<Pubkey, Vec<AccountEdge>>,
    /// Edges indexed by target.
    edges_to: HashMap<Pubkey, Vec<AccountEdge>>,
}

impl AccountGraph {
    /// Create a new empty account graph.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a node to the graph.
    pub fn add_node(&mut self, node: AccountNode) {
        self.nodes.insert(node.pubkey, node);
    }

    /// Add an edge to the graph.
    pub fn add_edge(&mut self, edge: AccountEdge) {
        self.edges_from
            .entry(edge.from)
            .or_default()
            .push(edge.clone());
        self.edges_to.entry(edge.to).or_default().push(edge);
    }

    /// Get a node by pubkey.
    pub fn get_node(&self, pubkey: &Pubkey) -> Option<&AccountNode> {
        self.nodes.get(pubkey)
    }

    /// Get all edges from a node.
    pub fn edges_from(&self, pubkey: &Pubkey) -> &[AccountEdge] {
        self.edges_from.get(pubkey).map(|v| v.as_slice()).unwrap_or(&[])
    }

    /// Get all edges to a node.
    pub fn edges_to(&self, pubkey: &Pubkey) -> &[AccountEdge] {
        self.edges_to.get(pubkey).map(|v| v.as_slice()).unwrap_or(&[])
    }

    /// Get all nodes.
    pub fn nodes(&self) -> impl Iterator<Item = &AccountNode> {
        self.nodes.values()
    }

    /// Get the number of nodes.
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Get the number of edges.
    pub fn edge_count(&self) -> usize {
        self.edges_from.values().map(|v| v.len()).sum()
    }

    /// Find all accounts reachable from a starting node.
    pub fn find_reachable(&self, start: &Pubkey) -> HashSet<Pubkey> {
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(*start);

        while let Some(current) = queue.pop_front() {
            if visited.insert(current) {
                for edge in self.edges_from(&current) {
                    if !visited.contains(&edge.to) {
                        queue.push_back(edge.to);
                    }
                }
            }
        }

        visited
    }

    /// Find all accounts that can reach a target node.
    pub fn find_reaching(&self, target: &Pubkey) -> HashSet<Pubkey> {
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(*target);

        while let Some(current) = queue.pop_front() {
            if visited.insert(current) {
                for edge in self.edges_to(&current) {
                    if !visited.contains(&edge.from) {
                        queue.push_back(edge.from);
                    }
                }
            }
        }

        visited
    }

    /// Get all nodes of a specific type.
    pub fn nodes_of_type(&self, node_type: &AccountNodeType) -> Vec<&AccountNode> {
        self.nodes
            .values()
            .filter(|n| n.account_type.as_ref() == Some(node_type))
            .collect()
    }

    /// Find all token accounts for a specific mint.
    pub fn token_accounts_for_mint(&self, mint: &Pubkey) -> Vec<&AccountNode> {
        self.nodes
            .values()
            .filter(|n| {
                matches!(&n.account_type, Some(AccountNodeType::TokenAccount { mint: m, .. }) if m == mint)
            })
            .collect()
    }

    /// Find all token accounts owned by a wallet.
    pub fn token_accounts_for_wallet(&self, wallet: &Pubkey) -> Vec<&AccountNode> {
        self.nodes
            .values()
            .filter(|n| {
                matches!(&n.account_type, Some(AccountNodeType::TokenAccount { owner: o, .. }) if o == wallet)
            })
            .collect()
    }

    /// Calculate total lamports in the graph.
    pub fn total_lamports(&self) -> u64 {
        self.nodes.values().map(|n| n.lamports).sum()
    }

    /// Get nodes sorted by lamports (descending).
    pub fn nodes_by_lamports(&self) -> Vec<&AccountNode> {
        let mut nodes: Vec<_> = self.nodes.values().collect();
        nodes.sort_by(|a, b| b.lamports.cmp(&a.lamports));
        nodes
    }
}

/// Account graph builder for constructing graphs from on-chain data.
pub struct AccountGraphBuilder {
    client: RpcClient,
}

impl AccountGraphBuilder {
    /// Create a new graph builder.
    pub fn new(rpc_url: &str) -> Self {
        Self {
            client: RpcClient::new_with_commitment(
                rpc_url.to_string(),
                CommitmentConfig::confirmed(),
            ),
        }
    }

    /// Create from an existing RPC client.
    pub fn from_client(client: RpcClient) -> Self {
        Self { client }
    }

    /// Build a graph from a list of account pubkeys.
    pub async fn build_from_accounts(&self, pubkeys: &[Pubkey]) -> Result<AccountGraph> {
        let mut graph = AccountGraph::new();

        // Fetch all accounts in batch
        let accounts = self.client.get_multiple_accounts(pubkeys).await?;

        for (pubkey, maybe_account) in pubkeys.iter().zip(accounts.iter()) {
            if let Some(account) = maybe_account {
                let node = self.create_node(*pubkey, account);
                graph.add_node(node);
            }
        }

        // Build edges based on account relationships
        self.build_edges(&mut graph);

        Ok(graph)
    }

    /// Build a graph of all token accounts for a wallet.
    pub async fn build_token_account_graph(&self, wallet: &Pubkey) -> Result<AccountGraph> {
        let mut graph = AccountGraph::new();
        let token_program = spl_token::id();

        // Get all token accounts
        let accounts = self
            .client
            .get_token_accounts_by_owner(
                wallet,
                solana_client::rpc_request::TokenAccountsFilter::ProgramId(token_program),
            )
            .await?;

        let mut pubkeys = Vec::new();
        let mut mints = HashSet::new();

        for keyed_account in &accounts {
            let pubkey = keyed_account.pubkey.parse::<Pubkey>().map_err(|e| {
                ToolkitError::Custom(format!("Failed to parse pubkey: {}", e))
            })?;
            pubkeys.push(pubkey);

            // Parse token account to get mint
            if let Some(account) = keyed_account.account.decode::<Account>() {
                if account.data.len() >= 32 {
                    let mint = Pubkey::try_from(&account.data[0..32])
                        .map_err(|_| ToolkitError::InvalidAccountData("Invalid mint".to_string()))?;
                    mints.insert(mint);
                }
            }
        }

        // Fetch token accounts
        let token_accounts = self.client.get_multiple_accounts(&pubkeys).await?;

        for (pubkey, maybe_account) in pubkeys.iter().zip(token_accounts.iter()) {
            if let Some(account) = maybe_account {
                let node = self.create_node(*pubkey, account);
                graph.add_node(node);
            }
        }

        // Fetch and add mints
        let mint_pubkeys: Vec<_> = mints.into_iter().collect();
        let mint_accounts = self.client.get_multiple_accounts(&mint_pubkeys).await?;

        for (pubkey, maybe_account) in mint_pubkeys.iter().zip(mint_accounts.iter()) {
            if let Some(account) = maybe_account {
                let node = self.create_node(*pubkey, account);
                graph.add_node(node);
            }
        }

        // Build edges
        self.build_edges(&mut graph);

        Ok(graph)
    }

    /// Create a node from account data.
    fn create_node(&self, pubkey: Pubkey, account: &Account) -> AccountNode {
        let account_type = self.classify_account(account);

        AccountNode {
            pubkey,
            owner: account.owner,
            lamports: account.lamports,
            data_len: account.data.len(),
            is_program: account.executable,
            account_type: Some(account_type),
        }
    }

    /// Classify an account based on its data and owner.
    fn classify_account(&self, account: &Account) -> AccountNodeType {
        let owner = account.owner;

        // System program account
        if owner == solana_sdk::system_program::id() {
            return AccountNodeType::SystemAccount;
        }

        // Token program accounts
        if owner == spl_token::id() {
            // Token account (165 bytes) vs Mint (82 bytes)
            if account.data.len() == 165 {
                // Parse token account
                if let Ok(token_account) = self.parse_token_account(&account.data) {
                    return token_account;
                }
            } else if account.data.len() == 82 {
                // Parse mint
                if let Ok(mint) = self.parse_mint(&account.data) {
                    return mint;
                }
            }
        }

        // Metaplex metadata (variable length, starts with specific discriminator)
        if owner == mpl_token_metadata::ID && !account.data.is_empty() {
            if account.data[0] == 4 && account.data.len() >= 33 {
                // Metadata account
                if let Ok(mint) = Pubkey::try_from(&account.data[1..33]) {
                    return AccountNodeType::Metadata { mint };
                }
            }
        }

        // Program account
        if account.executable {
            return AccountNodeType::Program;
        }

        AccountNodeType::Unknown
    }

    /// Parse SPL token account data.
    fn parse_token_account(&self, data: &[u8]) -> Result<AccountNodeType> {
        if data.len() < 72 {
            return Err(ToolkitError::InvalidAccountData("Data too short".to_string()));
        }

        let mint = Pubkey::try_from(&data[0..32])
            .map_err(|_| ToolkitError::InvalidAccountData("Invalid mint".to_string()))?;
        let owner = Pubkey::try_from(&data[32..64])
            .map_err(|_| ToolkitError::InvalidAccountData("Invalid owner".to_string()))?;
        let amount = u64::from_le_bytes(
            data[64..72].try_into().map_err(|_| ToolkitError::InvalidAccountData("Invalid amount".to_string()))?
        );

        Ok(AccountNodeType::TokenAccount { mint, owner, amount })
    }

    /// Parse SPL mint data.
    fn parse_mint(&self, data: &[u8]) -> Result<AccountNodeType> {
        if data.len() < 45 {
            return Err(ToolkitError::InvalidAccountData("Data too short".to_string()));
        }

        // Supply is at offset 36-44
        let supply = u64::from_le_bytes(
            data[36..44].try_into().map_err(|_| ToolkitError::InvalidAccountData("Invalid supply".to_string()))?
        );
        // Decimals at offset 44
        let decimals = data[44];

        Ok(AccountNodeType::TokenMint { supply, decimals })
    }

    /// Build edges based on account relationships.
    fn build_edges(&self, graph: &mut AccountGraph) {
        let nodes: Vec<_> = graph.nodes.values().cloned().collect();

        for node in &nodes {
            // Add owner edge
            if graph.nodes.contains_key(&node.owner) {
                graph.add_edge(AccountEdge {
                    from: node.pubkey,
                    to: node.owner,
                    edge_type: EdgeType::OwnedBy,
                });
            }

            // Add token-specific edges
            if let Some(AccountNodeType::TokenAccount { mint, owner, .. }) = &node.account_type {
                // Edge to mint
                if graph.nodes.contains_key(mint) {
                    graph.add_edge(AccountEdge {
                        from: node.pubkey,
                        to: *mint,
                        edge_type: EdgeType::TokenAccountOf,
                    });
                }

                // Edge to wallet owner
                if graph.nodes.contains_key(owner) {
                    graph.add_edge(AccountEdge {
                        from: node.pubkey,
                        to: *owner,
                        edge_type: EdgeType::Authority,
                    });
                }
            }

            // Add metadata edges
            if let Some(AccountNodeType::Metadata { mint }) = &node.account_type {
                if graph.nodes.contains_key(mint) {
                    graph.add_edge(AccountEdge {
                        from: node.pubkey,
                        to: *mint,
                        edge_type: EdgeType::MetadataOf,
                    });
                }
            }
        }
    }
}

/// Utility functions for account graph operations.
pub mod utils {
    use super::*;

    /// Find all closeable accounts in a graph (empty token accounts).
    pub fn find_closeable_accounts(graph: &AccountGraph) -> Vec<&AccountNode> {
        graph
            .nodes()
            .filter(|n| {
                matches!(
                    &n.account_type,
                    Some(AccountNodeType::TokenAccount { amount: 0, .. })
                )
            })
            .collect()
    }

    /// Calculate total recoverable rent from closeable accounts.
    pub fn total_recoverable_rent(graph: &AccountGraph) -> u64 {
        find_closeable_accounts(graph)
            .iter()
            .map(|n| n.lamports)
            .sum()
    }

    /// Group accounts by their owner program.
    pub fn group_by_owner(graph: &AccountGraph) -> HashMap<Pubkey, Vec<&AccountNode>> {
        let mut groups: HashMap<Pubkey, Vec<&AccountNode>> = HashMap::new();
        for node in graph.nodes() {
            groups.entry(node.owner).or_default().push(node);
        }
        groups
    }

    /// Find accounts with the largest balances.
    pub fn top_accounts_by_balance(graph: &AccountGraph, limit: usize) -> Vec<&AccountNode> {
        let mut nodes = graph.nodes_by_lamports();
        nodes.truncate(limit);
        nodes
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_account_graph_new() {
        let graph = AccountGraph::new();
        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_add_node() {
        let mut graph = AccountGraph::new();
        let pubkey = Pubkey::new_unique();

        graph.add_node(AccountNode {
            pubkey,
            owner: solana_sdk::system_program::id(),
            lamports: 1000,
            data_len: 0,
            is_program: false,
            account_type: Some(AccountNodeType::SystemAccount),
        });

        assert_eq!(graph.node_count(), 1);
        assert!(graph.get_node(&pubkey).is_some());
    }

    #[test]
    fn test_add_edge() {
        let mut graph = AccountGraph::new();
        let from = Pubkey::new_unique();
        let to = Pubkey::new_unique();

        graph.add_edge(AccountEdge {
            from,
            to,
            edge_type: EdgeType::OwnedBy,
        });

        assert_eq!(graph.edge_count(), 1);
        assert_eq!(graph.edges_from(&from).len(), 1);
        assert_eq!(graph.edges_to(&to).len(), 1);
    }

    #[test]
    fn test_find_reachable() {
        let mut graph = AccountGraph::new();
        let a = Pubkey::new_unique();
        let b = Pubkey::new_unique();
        let c = Pubkey::new_unique();

        graph.add_edge(AccountEdge {
            from: a,
            to: b,
            edge_type: EdgeType::Related,
        });
        graph.add_edge(AccountEdge {
            from: b,
            to: c,
            edge_type: EdgeType::Related,
        });

        let reachable = graph.find_reachable(&a);
        assert!(reachable.contains(&a));
        assert!(reachable.contains(&b));
        assert!(reachable.contains(&c));
    }

    #[test]
    fn test_total_lamports() {
        let mut graph = AccountGraph::new();

        for i in 0..3 {
            graph.add_node(AccountNode {
                pubkey: Pubkey::new_unique(),
                owner: solana_sdk::system_program::id(),
                lamports: 1000 * (i + 1) as u64,
                data_len: 0,
                is_program: false,
                account_type: None,
            });
        }

        assert_eq!(graph.total_lamports(), 6000);
    }
}