wallet_balance/
wallet_balance.rs

1//! Wallet Balance Example
2//!
3//! This example demonstrates how to retrieve a wallet's balance for specific assets.
4//! It shows how to:
5//! - Initialize a Privy client with app credentials
6//! - Query wallet balance for specific assets and chains
7//! - Include currency conversion data in responses
8//!
9//! ## Required Environment Variables
10//! - `PRIVY_APP_ID`: Your Privy app ID
11//! - `PRIVY_APP_SECRET`: Your Privy app secret
12//! - `PRIVY_WALLET_ID`: The wallet ID to check balance for
13//!
14//! ## Usage
15//! ```bash
16//! cargo run --example wallet_balance
17//! ```
18
19use anyhow::Result;
20use privy_rs::{
21    PrivyClient,
22    generated::types::{
23        GetWalletBalanceAsset, GetWalletBalanceAssetString, GetWalletBalanceChain,
24        GetWalletBalanceChainString, GetWalletBalanceIncludeCurrency,
25    },
26};
27use tracing_subscriber::EnvFilter;
28
29#[tokio::main]
30async fn main() -> Result<()> {
31    tracing_subscriber::fmt()
32        .with_env_filter(
33            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
34        )
35        .init();
36
37    // Get wallet ID from environment and initialize client
38    let wallet_id =
39        std::env::var("PRIVY_WALLET_ID").expect("PRIVY_WALLET_ID environment variable not set");
40    let client = PrivyClient::new_from_env()?;
41
42    tracing::info!(
43        "initialized privy client from environment, wallet_id: {}",
44        wallet_id
45    );
46
47    // Get SOL balance on Solana
48    let balance = client
49        .wallets()
50        .balance()
51        .get(
52            &wallet_id,
53            &GetWalletBalanceAsset::String(GetWalletBalanceAssetString::Sol),
54            &GetWalletBalanceChain::String(GetWalletBalanceChainString::Solana),
55            Some(GetWalletBalanceIncludeCurrency::Usd),
56        )
57        .await?;
58
59    tracing::info!("got wallet balance: {:?}", balance);
60
61    Ok(())
62}