This section provides practical examples of how to use the SWquery SDK to query and manage on-chain data. These examples are designed to help you quickly integrate the SDK into your applications.
1. Setting Up the SDK
Initialize the SWquery client in your Rust application:
use swquery::SWqueryClient;
fn main() {
// Create an instance of the SWquery client
let client = SWqueryClient::new("your_api_key");
println!("SWquery SDK initialized!");
}
2. Querying a Wallet
Perform a basic natural language query on a single wallet:
use swquery::SWqueryClient;
fn main() {
let client = SWqueryClient::new("your_api_key");
let wallet_address = "YourWalletAddressHere";
let query = "List all transactions over 1 SOL in the past week";
match client.query_wallet(wallet_address, query) {
Ok(result) => println!("Query Result: {:?}", result),
Err(error) => eprintln!("Error: {:?}", error),
}
}
3. Querying Multiple Wallets
Query transactions across multiple wallets and combine results:
use swquery::SWqueryClient;
fn main() {
let client = SWqueryClient::new("your_api_key");
let wallets = vec!["WalletAddress1", "WalletAddress2"];
let query = "Find all transactions received in December 2024";
match client.query_multiple_wallets(wallets, query) {
Ok(results) => println!("Combined Results: {:?}", results),
Err(error) => eprintln!("Error: {:?}", error),
}
}
4. Custom Prompt Queries
Tailor the SDK’s behavior by providing custom natural language prompts:
use swquery::SWqueryClient;
fn main() {
let client = SWqueryClient::new("your_api_key");
let wallet_address = "YourWalletAddressHere";
let custom_prompt = "Summarize all transactions from staking rewards";
match client.query_wallet_with_prompt(wallet_address, custom_prompt) {
Ok(result) => println!("Custom Query Result: {:?}", result),
Err(error) => eprintln!("Error: {:?}", error),
}
}
5. Handling Errors Gracefully
Ensure smooth operation by handling errors during queries:
use swquery::SWqueryClient;
fn main() {
let client = SWqueryClient::new("your_api_key");
let wallet_address = "YourWalletAddressHere";
let query = "Invalid query example";
match client.query_wallet(wallet_address, query) {
Ok(result) => println!("Query Result: {:?}", result),
Err(error) => match error {
swquery::Error::WalletNotFound => eprintln!("Error: Wallet not found"),
swquery::Error::QueryParsingFailed => eprintln!("Error: Failed to parse query"),
_ => eprintln!("Error: An unexpected error occurred"),
},
}
}
6. Visualizing Query Results
Integrate query results with your application’s visualization layer. For example:
use swquery::SWqueryClient;
fn main() {
let client = SWqueryClient::new("your_api_key");
let wallet_address = "YourWalletAddressHere";
let query = "List all transactions over 5 SOL in the last month";
match client.query_wallet(wallet_address, query) {
Ok(results) => {
// Simulating visualization with a simple print
for transaction in results.transactions {
println!(
"Date: {}, Amount: {}, Type: {}",
transaction.date, transaction.amount, transaction.transaction_type
);
}
}
Err(error) => eprintln!("Error: {:?}", error),
}
}
7. Debugging and Logs
Enable detailed logs for easier debugging during development:
use swquery::SWqueryClient;
fn main() {
let mut client = SWqueryClient::new("your_api_key");
// Enable logging
client.enable_logging(true);
let wallet_address = "YourWalletAddressHere";
let query = "Fetch transactions with amounts greater than 10 SOL";
match client.query_wallet(wallet_address, query) {
Ok(result) => println!("Query Result: {:?}", result),
Err(error) => eprintln!("Error: {:?}", error),
}
}