solana_transaction_crawler/lib.rs
1//! # Solana Transaction Crawler Library
2//! This library is used to crawl a series of transactions for a given account address and extract the data from it.
3//! It operates by allowing you to build a Crawler object and then compose a series of filters
4//! for transactions and instructions onto it.
5//!
6//! Using this library requires access to a Solana RPC node with full historical data, otherwise the crawling will only
7//! work with very new data.
8//!
9//! ## Recommended Client Dependencies
10//! ```rust,ignore
11//! [dependencies]
12//! anyhow = "1.0.58"
13//! solana-client = "~1.9.29"
14//! solana-program = "~1.9.29"
15//! tokio = "~1.14.1"
16//! ```
17//!
18//! ## Examples
19//!
20//! Construct filters to get mints from a candy machine.
21//!
22//! ```rust,ignore
23//! use anyhow::Result;
24//! use solana_client::rpc_client::RpcClient;
25//! use solana_transaction_crawler::{
26//! crawler::{Crawler, IxAccount},
27//! filters::{IxNumberAccounts, SuccessfulTxFilter, TxHasProgramId},
28//! };
29//! use solana_program::pubkey;
30//!
31//! #[tokio::main]
32//! async fn main() -> Result<()> {
33//! // Set up Solana RPC client.
34//! let client = RpcClient::new("https://rpc.ankr.com/solana");
35//!
36//! // The pubkey of the account you want to crawl for transactions. In this example we use the DeGods CMV1 pubkey.
37//! let candy_machine_id = pubkey!("9MynErYQ5Qi6obp4YwwdoDmXkZ1hYVtPUqYmJJ3rZ9Kn");
38//!
39//! // Create the Crawler object.
40//! let mut crawler = Crawler::new(client, candy_machine_id);
41//!
42//! // Define filters.
43//!
44//! // We only want to crawl transactions that have the program id of the Candy Machine V1 program.
45//! let has_program_id = TxHasProgramId::new("cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ");
46//!
47//! // This filter gives us only successful transactions, filtering out any with errors.
48//! let successful_tx = SuccessfulTxFilter;
49//!
50//! // We know the mintNFT instruction has exactly 14 accounts and is the only instruction on that program with that number of accounts.
51//! let ix_num_accounts = IxNumberAccounts::EqualTo(14);
52//!
53//! // Specify accounts we want to retrieve from the transaction.
54//! // 'Unparsed' means we have to specify the actual index of the account in the instruction.
55//! // The mint account is the sixth account in the instruction.
56//! let mint_account = IxAccount::unparsed("mint", 5);
57//!
58//! // Add filters to Crawler
59//! crawler
60//! .add_tx_filter(has_program_id)
61//! .add_tx_filter(successful_tx)
62//! .add_ix_filter(ix_num_accounts)
63//! .add_account_index(mint_account);
64//!
65//! // Run the Crawler.
66//! let crawled_accounts = crawler.run().await?;
67//!
68//! // We labeled our account "mint" so we look it up by the label.
69//! let mint_addresses = &crawled_accounts["mint"];
70//! println!("Items found: {:?}", mint_addresses.len());
71//!
72//! Ok(())
73//! }
74//!```
75//! For common use cases, there are pre-built methods that can be used. For example,
76//! to get all the mints from a Candy Machine V2 instance:
77//!
78//! ```rust,ignore
79//! use anyhow::Result;
80//! use solana_client::rpc_client::RpcClient;
81//! use solana_transaction_crawler::crawler::Crawler;
82//! use solana_program::pubkey;
83//!
84//! #[tokio::main]
85//! async fn main() -> Result<()> {
86//! // Set up Solana RPC client.
87//! let client = RpcClient::new("https://rpc.ankr.com/solana");
88//!
89//! // The pubkey of the candy machine v2 account you want to crawl for transactions.
90//! let candy_machine_id = pubkey!("CMaBroFssWYUXxxwmASEQXF9RtBiyq3XwA1Qi1ViTTV");
91//!
92//! // Create the Crawler object.
93//! let crawled_accounts = Crawler::get_cmv2_mints(client, candy_machine_id).await?;
94//!
95//! // This method returns both 'mint' and 'metadata' accounts under the respective labels.
96//! let mint_addresses = &crawled_accounts["mint"];
97//! let metadata_addresses = &crawled_accounts["metadata"];
98//! println!("Mint Items found: {:?}", mint_addresses.len());
99//! println!("Metadata Items found: {:?}", metadata_addresses.len());
100//!
101//! Ok(())
102//! }
103//! ```
104//!
105
106pub mod constants;
107pub mod crawler;
108pub mod errors;
109pub mod filters;