-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindexer_queries.rs
More file actions
159 lines (143 loc) · 5.71 KB
/
Copy pathindexer_queries.rs
File metadata and controls
159 lines (143 loc) · 5.71 KB
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
//! Example: Indexer Queries
//!
//! This example demonstrates how to:
//! 1. Query fungible asset balances
//! 2. Get account tokens (NFTs)
//! 3. Fetch transaction history
//!
//! Run with: `cargo run --example indexer_queries --features "ed25519,indexer"`
use aptos_sdk::{Aptos, AptosConfig, api::PaginationParams, types::AccountAddress};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("=== Indexer Queries Example ===\n");
// Setup
let aptos = Aptos::new(AptosConfig::devnet())?;
println!("Connected to devnet");
// Get the indexer client (requires "indexer" feature)
let indexer = aptos.indexer().ok_or_else(|| {
anyhow::anyhow!("Indexer not available. Make sure the 'indexer' feature is enabled.")
})?;
// Use a known address with activity (Aptos Framework)
let known_address = AccountAddress::ONE; // 0x1
println!("Querying data for address: {known_address}");
// 1. Get fungible asset balances
println!("\n--- 1. Fungible Asset Balances ---");
{
match indexer.get_fungible_asset_balances(known_address).await {
Ok(balances) => {
println!("Found {} fungible asset balance(s):", balances.len());
for (i, balance) in balances.iter().take(5).enumerate() {
println!(" {}. Asset: {}", i + 1, balance.asset_type);
println!(" Amount: {}", balance.amount);
if let Some(meta) = &balance.metadata {
println!(" Name: {} ({})", meta.name, meta.symbol);
}
}
if balances.len() > 5 {
println!(" ... and {} more", balances.len() - 5);
}
}
Err(e) => println!(" Could not fetch balances: {e}"),
}
}
// 2. Get account tokens (NFTs) with pagination
println!("\n--- 2. Account Tokens (NFTs) ---");
{
let pagination = Some(PaginationParams {
limit: 5,
offset: 0,
});
match indexer
.get_account_tokens_paginated(known_address, pagination)
.await
{
Ok(page) => {
let tokens = page.items;
if tokens.is_empty() {
println!(" No tokens found for this address");
} else {
println!("Found {} token(s):", tokens.len());
for (i, token) in tokens.iter().enumerate() {
println!(" {}. Token ID: {}", i + 1, token.token_data_id);
println!(" Amount: {}", token.amount);
if let Some(data) = &token.current_token_data {
println!(" Name: {}", data.token_name);
if let Some(collection) = &data.current_collection {
println!(" Collection: {}", collection.collection_name);
}
}
}
}
}
Err(e) => println!(" Could not fetch tokens: {e}"),
}
}
// 3. Get recent transactions
println!("\n--- 3. Recent Transactions ---");
{
match indexer
.get_account_transactions(known_address, Some(5))
.await
{
Ok(transactions) => {
println!("Recent {} transaction(s):", transactions.len());
for (i, txn) in transactions.iter().enumerate() {
println!(" {}. Version: {}", i + 1, txn.transaction_version);
if !txn.coin_activities.is_empty() {
println!(" Coin activities: {}", txn.coin_activities.len());
for activity in txn.coin_activities.iter().take(2) {
let amount = activity.amount.as_deref().unwrap_or("N/A");
println!(
" - {}: {} ({})",
activity.activity_type, amount, activity.coin_type
);
}
}
}
}
Err(e) => println!(" Could not fetch transactions: {e}"),
}
}
// 4. Custom GraphQL query
println!("\n--- 4. Custom GraphQL Query ---");
{
// Custom query to get processor status
let query = r"
query GetProcessorStatus {
processor_status(limit: 3) {
processor
last_success_version
}
}
";
#[derive(Debug, serde::Deserialize)]
struct ProcessorStatus {
processor: String,
last_success_version: i64,
}
#[derive(Debug, serde::Deserialize)]
struct ProcessorStatusResponse {
processor_status: Vec<ProcessorStatus>,
}
match indexer.query::<ProcessorStatusResponse>(query, None).await {
Ok(response) => {
println!("Processor status:");
for status in response.processor_status.iter().take(3) {
println!(
" {}: version {}",
status.processor, status.last_success_version
);
}
}
Err(e) => println!(" Query failed: {e}"),
}
}
println!("\n--- Summary ---");
println!("The indexer provides fast access to:");
println!(" • Fungible asset (FA) balances");
println!(" • NFT/token ownership");
println!(" • Transaction history with coin activities");
println!(" • Custom GraphQL queries");
println!("\n=== Indexer Queries Example Complete ===");
Ok(())
}