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};
#[derive(Debug, Clone)]
pub struct AccountNode {
pub pubkey: Pubkey,
pub owner: Pubkey,
pub lamports: u64,
pub data_len: usize,
pub is_program: bool,
pub account_type: Option<AccountNodeType>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccountNodeType {
SystemAccount,
TokenAccount {
mint: Pubkey,
owner: Pubkey,
amount: u64,
},
TokenMint {
supply: u64,
decimals: u8,
},
Metadata { mint: Pubkey },
Program,
AssociatedTokenAccount {
wallet: Pubkey,
mint: Pubkey,
},
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EdgeType {
OwnedBy,
TokenAccountOf,
Authority,
MetadataOf,
AssociatedWith,
Related,
}
#[derive(Debug, Clone)]
pub struct AccountEdge {
pub from: Pubkey,
pub to: Pubkey,
pub edge_type: EdgeType,
}
#[derive(Debug, Default)]
pub struct AccountGraph {
nodes: HashMap<Pubkey, AccountNode>,
edges_from: HashMap<Pubkey, Vec<AccountEdge>>,
edges_to: HashMap<Pubkey, Vec<AccountEdge>>,
}
impl AccountGraph {
pub fn new() -> Self {
Self::default()
}
pub fn add_node(&mut self, node: AccountNode) {
self.nodes.insert(node.pubkey, node);
}
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);
}
pub fn get_node(&self, pubkey: &Pubkey) -> Option<&AccountNode> {
self.nodes.get(pubkey)
}
pub fn edges_from(&self, pubkey: &Pubkey) -> &[AccountEdge] {
self.edges_from.get(pubkey).map(|v| v.as_slice()).unwrap_or(&[])
}
pub fn edges_to(&self, pubkey: &Pubkey) -> &[AccountEdge] {
self.edges_to.get(pubkey).map(|v| v.as_slice()).unwrap_or(&[])
}
pub fn nodes(&self) -> impl Iterator<Item = &AccountNode> {
self.nodes.values()
}
pub fn node_count(&self) -> usize {
self.nodes.len()
}
pub fn edge_count(&self) -> usize {
self.edges_from.values().map(|v| v.len()).sum()
}
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(¤t) {
if !visited.contains(&edge.to) {
queue.push_back(edge.to);
}
}
}
}
visited
}
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(¤t) {
if !visited.contains(&edge.from) {
queue.push_back(edge.from);
}
}
}
}
visited
}
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()
}
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()
}
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()
}
pub fn total_lamports(&self) -> u64 {
self.nodes.values().map(|n| n.lamports).sum()
}
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
}
}
pub struct AccountGraphBuilder {
client: RpcClient,
}
impl AccountGraphBuilder {
pub fn new(rpc_url: &str) -> Self {
Self {
client: RpcClient::new_with_commitment(
rpc_url.to_string(),
CommitmentConfig::confirmed(),
),
}
}
pub fn from_client(client: RpcClient) -> Self {
Self { client }
}
pub async fn build_from_accounts(&self, pubkeys: &[Pubkey]) -> Result<AccountGraph> {
let mut graph = AccountGraph::new();
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);
}
}
self.build_edges(&mut graph);
Ok(graph)
}
pub async fn build_token_account_graph(&self, wallet: &Pubkey) -> Result<AccountGraph> {
let mut graph = AccountGraph::new();
let token_program = spl_token::id();
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);
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);
}
}
}
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);
}
}
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);
}
}
self.build_edges(&mut graph);
Ok(graph)
}
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),
}
}
fn classify_account(&self, account: &Account) -> AccountNodeType {
let owner = account.owner;
if owner == solana_sdk::system_program::id() {
return AccountNodeType::SystemAccount;
}
if owner == spl_token::id() {
if account.data.len() == 165 {
if let Ok(token_account) = self.parse_token_account(&account.data) {
return token_account;
}
} else if account.data.len() == 82 {
if let Ok(mint) = self.parse_mint(&account.data) {
return mint;
}
}
}
if owner == mpl_token_metadata::ID && !account.data.is_empty() {
if account.data[0] == 4 && account.data.len() >= 33 {
if let Ok(mint) = Pubkey::try_from(&account.data[1..33]) {
return AccountNodeType::Metadata { mint };
}
}
}
if account.executable {
return AccountNodeType::Program;
}
AccountNodeType::Unknown
}
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 })
}
fn parse_mint(&self, data: &[u8]) -> Result<AccountNodeType> {
if data.len() < 45 {
return Err(ToolkitError::InvalidAccountData("Data too short".to_string()));
}
let supply = u64::from_le_bytes(
data[36..44].try_into().map_err(|_| ToolkitError::InvalidAccountData("Invalid supply".to_string()))?
);
let decimals = data[44];
Ok(AccountNodeType::TokenMint { supply, decimals })
}
fn build_edges(&self, graph: &mut AccountGraph) {
let nodes: Vec<_> = graph.nodes.values().cloned().collect();
for node in &nodes {
if graph.nodes.contains_key(&node.owner) {
graph.add_edge(AccountEdge {
from: node.pubkey,
to: node.owner,
edge_type: EdgeType::OwnedBy,
});
}
if let Some(AccountNodeType::TokenAccount { mint, owner, .. }) = &node.account_type {
if graph.nodes.contains_key(mint) {
graph.add_edge(AccountEdge {
from: node.pubkey,
to: *mint,
edge_type: EdgeType::TokenAccountOf,
});
}
if graph.nodes.contains_key(owner) {
graph.add_edge(AccountEdge {
from: node.pubkey,
to: *owner,
edge_type: EdgeType::Authority,
});
}
}
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,
});
}
}
}
}
}
pub mod utils {
use super::*;
pub fn find_closeable_accounts(graph: &AccountGraph) -> Vec<&AccountNode> {
graph
.nodes()
.filter(|n| {
matches!(
&n.account_type,
Some(AccountNodeType::TokenAccount { amount: 0, .. })
)
})
.collect()
}
pub fn total_recoverable_rent(graph: &AccountGraph) -> u64 {
find_closeable_accounts(graph)
.iter()
.map(|n| n.lamports)
.sum()
}
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
}
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);
}
}