Skip to main content

Get Address Balance

You can retrieve the outputs for any given address using the Client.basicOutputIds(queryParameters) function. You can then use the retrieved outputs to calculate the balance.

The following code example will:

  1. Create a Client which will connect to the Shimmer Testnet.
  2. Create a SecretManager from a mnemonic.
  3. Get outputs for the generated address.
  4. Add the total amount of native tokens and outputs.
  5. Log the results from step 4 to console.

Code Example

Dotenv

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

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

//! cargo run --example 02_get_address_balance --release
//! In this example we will get the outputs of an address that have no additional unlock conditions and sum the amounts
//! and native tokens.

use iota_client::{
block::output::{NativeTokensBuilder, Output},
node_api::indexer::query_parameters::QueryParameter,
secret::{mnemonic::MnemonicSecretManager, SecretManager},
Client, Result,
};

#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv::dotenv().ok();

let node_url = std::env::var("NODE_URL").unwrap();

// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;

let secret_manager =
MnemonicSecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;

let token_supply = client.get_token_supply().await?;

// Generate the first address
let addresses = client
.get_addresses(&SecretManager::Mnemonic(secret_manager))
.with_account_index(0)
.with_range(0..1)
.finish()
.await?;

// Get output ids of outputs that can be controlled by this address without further unlock constraints
let output_ids = client
.basic_output_ids(vec![
QueryParameter::Address(addresses[0].clone()),
QueryParameter::HasExpiration(false),
QueryParameter::HasTimelock(false),
QueryParameter::HasStorageDepositReturn(false),
])
.await?;

// Get the outputs by their id
let outputs_responses = client.get_outputs(output_ids).await?;

// Calculate the total amount and native tokens
let mut total_amount = 0;
let mut total_native_tokens = NativeTokensBuilder::new();
for output_response in outputs_responses {
let output = Output::try_from_dto(&output_response.output, token_supply)?;

if let Some(native_tokens) = output.native_tokens() {
total_native_tokens.add_native_tokens(native_tokens.clone())?;
}
total_amount += output.amount();
}

println!(
"Outputs controlled by {} have: {:?}i and native tokens: {:?}",
addresses[0],
total_amount,
total_native_tokens.finish_vec()?
);
Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example 02_get_address_balance --release

Expected Output

Outputs controlled by rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy
have: 2609275800i and nativetokens: [
NativeToken{
token_id: TokenId(0x085c6b799750bdf7e5a5c81144465a0676bc11dab74b997444ca369949341720e80100000000),
amount: 100
},
NativeToken{
token_id: TokenId(0x0822ceb3166ad125d310e6660f5fc292356f87f2f9566e982ea22154cec3847b3f0100000000),
amount: 100
},
NativeToken{
token_id: TokenId(0x08f708a29e9619e847916de76c2e167e87a704c235dcbd7cda018865be7f561b5a0100000000),
amount: 50
},
NativeToken{
token_id: TokenId(0x0808fb702d67fdb320b5959f152c0f962630515d904c71ed09447c341a6cc171de0100000000),
amount: 100
},
NativeToken{
token_id: TokenId(0x08f1802858831220b282ccc4c557676d61f79833869de378ce9a81f736976ce39f0100000000),
amount: 50
}
]