Skip to main content

Check Balance

You can retrieve the balance for any account you created by calling the Account.balance() function. Bear in mind that you should always sync the account with the node to get the latest transactions.

Online Faucet

You can request test funds from the Shimmer Testnet Faucet.

Code Example

The following example will:

  1. Create an account manager.
  2. Get Alice's account which was created in the first guide.
  3. Sync the account with the node to get the latest transactions.
  4. Request the account's balance.
Unlock Conditions

Outputs may have multiple UnlockConditions, which may require returning some or all of the transferred amount. The outputs could also expire if not claimed in time, or may not be unlockable for a predefined period.

To get outputs with only the AddressUnlockCondition, you should synchronize with the option syncOnlyMostBasicOutputs: true.

If you are synchronizing outputs with other unlock conditions, you should check the unlock conditions carefully before crediting users any balance.

You can find an example illustrating how to check if an output has only the address unlock condition, where the address belongs to the account in the Check Unlock Conditions how-to guide.

Dotenv

This example uses dotenv, which is not safe for use in production environments.

// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example get_balance --release
// In this example we sync the account and get the balance
// Rename `.env.example` to `.env` first

use iota_wallet::{account_manager::AccountManager, Result};

#[tokio::main]
async fn main() -> Result<()> {
// Create the account manager
let manager = AccountManager::builder().finish().await?;

// Get the account we generated with `01_create_wallet`
let account = manager.get_account("Alice").await?;

// Sync and get the balance
let _account_balance = account.sync(None).await?;
// If already synced, just get the balance
let account_balance = account.balance().await?;

println!("{account_balance:?}");

Ok(())
}

Run the example by running the following command:

cargo run --example get_balance --release

Expected Output

AccountBalance{
base_coin: BaseCoinBalance{
total: 100000000000,
available: 100000000000
},
required_storage_deposit: 213000,
native_tokens: [

],
nfts: [

],
aliases: [

],
foundries: [

],
potentially_locked_outputs: {

}
}