Rust SDK - Fetch Data
You can use the Aptos client to get on-chain data using a variety of helper
functions. High-level helpers such as ledger_info, get_balance, and view
sit on Aptos. Lower-level REST calls go through aptos.fullnode().
Here’s an example showing how to fetch common data you may need in your application:
use aptos_sdk::{Aptos, AptosConfig};
let aptos = Aptos::new(AptosConfig::devnet())?;
let ledger_info = aptos.ledger_info().await?;let balance = aptos.get_balance(address).await?;let account = aptos.fullnode().get_account(address).await?;let resources = aptos.fullnode().get_account_resources(address).await?;Using Move View Functions
Section titled “Using Move View Functions”You can call view functions which return custom data from on-chain by using
aptos.view.
For example, you can look up the current timestamp or an APT coin balance:
let timestamp = aptos .view("0x1::timestamp::now_seconds", vec![], vec![]) .await?;
let balance = aptos .view( "0x1::coin::balance", vec!["0x1::aptos_coin::AptosCoin".to_string()], vec![serde_json::json!(address.to_string())], ) .await?;Using Indexer Data
Section titled “Using Indexer Data”The Aptos client can query both network data from
fullnodes and the
Indexer
API when the indexer feature is enabled (it is on by default).
use aptos_sdk::{Aptos, AptosConfig, types::AccountAddress};
let aptos = Aptos::new( AptosConfig::devnet() .with_indexer_url("https://api.devnet.aptoslabs.com/v1/graphql")?,)?;
let indexer = aptos .indexer() .ok_or_else(|| anyhow::anyhow!("Indexer client not available"))?;
let balances = indexer .get_fungible_asset_balances(AccountAddress::ONE) .await?;
let my_query = r#"query { ledger_infos { chain_id } }"#;let result: serde_json::Value = indexer.query(my_query, None).await?;