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.
You can request test funds from the Shimmer Testnet Faucet.
Code Example
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Sync the account with the node to get the latest transactions.
- Request the account's balance.
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.
- Rust
- Nodejs
- Python
- Java
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
/**
* This example gets the balance for an account
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('Alice');
const addressObject = await account.addresses();
console.log('Addresses before:', addressObject);
// Always sync before calling getBalance()
const synced = await account.sync();
console.log('Syncing... - ', synced);
console.log('Available balance', await account.getBalance());
// Use the Faucet to send testnet tokens to your address:
console.log("Fill your address with the Faucet: https://faucet.testnet.shimmer.network")
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 3-check-balance.js
from iota_wallet import IotaWallet
# This example checks the balance of an account.
wallet = IotaWallet('./alice-database')
account = wallet.get_account('Alice')
# Sync account with the node
response = account.sync()
print(f'Synced: {response}')
# Just calculate the balance with the known state
balance = account.get_balance()
print(f'Balance: {balance}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 2-check-balance.py
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import org.iota.Wallet;
import org.iota.types.*;
import org.iota.types.account_methods.SyncAccount;
import org.iota.types.exceptions.InitializeWalletException;
import org.iota.types.exceptions.WalletException;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class CheckBalance {
public static void main(String[] args) throws WalletException, InterruptedException, InitializeWalletException {
// This example assumes that a wallet has already been created using the ´SetupWallet.java´ example.
// If you haven't run the ´SetupWallet.java´ example yet, you must run it first to be able to load the wallet as shown below:
Wallet wallet = new Wallet(new WalletConfig()
.withClientOptions(new ClientConfig().withNodes(Env.NODE))
.withSecretManager(new StrongholdSecretManager(Env.STRONGHOLD_PASSWORD, null, Env.STRONGHOLD_VAULT_PATH))
.withCoinType(CoinType.Shimmer)
.withStoragePath(Env.STORAGE_PATH)
);
// Get account and sync it with the registered node to ensure that its balances are up-to-date.
AccountHandle a = wallet.getAccount(new AccountAlias(Env.ACCOUNT_NAME));
a.syncAccount(new SyncAccount().withOptions(new SyncOptions()));
// Get the balance.
AccountBalance balance = a.getBalance();
// Print the balance.
System.out.println(balance);
// In case you are done and don't need the wallet instance anymore you can destroy the instance to clean up memory.
// For this, check out the ´DestroyWallet.java´ example.
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
AccountBalance{
base_coin: BaseCoinBalance{
total: 100000000000,
available: 100000000000
},
required_storage_deposit: 213000,
native_tokens: [
],
nfts: [
],
aliases: [
],
foundries: [
],
potentially_locked_outputs: {
}
}
Synced: {
'baseCoin': {
'total': '3490057203',
'available': '3490057203'
},
'requiredStorageDeposit': '98250300',
'nativeTokens': [
{
'tokenId': '0x08b83d49922e341d2cb45159707cfafdc9dc8fdb9d119543480dbaa5773eed8c4a0100000000',
'total': '0x64',
'available': '0x64'
}
],
'nfts': [
'0xceae643ff7c112a3adce8f55f7953ba0707ade21256a7a09068c0b47f7c62c5b',
'0x1e808b7c6e603aaeb5f718881a74fedae72981ac7d5f0294eb561cad0e653566',
'0x77133189021f50d8d66e0678e553af9f46a832a24239653d3555edb8dc859e1f',
'0x17f97185f80fa56eab974de6b7bbb80fa812d4e8e37090d166a0a41da129cebc',
'0x1b670afba8d59a445cbaf167f1fda05879362e3ea034f5c4a0979fbeb5a3964b',
'0x3f0e11e9d9f48a57d0fba43d7d1158ee673cb8055f80a5ce45ad174c962c0d8a',
'0xdc8be91d779aac048aa9001ab99ecf12cf62a4701185a95f6206a1a201bfbe7c'
],
'aliases': [
],
'foundries': [
],
'potentiallyLockedOutputs': {
'0x8dd4f722a18f3b5822216e856a98ea480fbfaa205f10c08d36dc5ab21efcc8355600': False
}[
...
]
]
Balance: {
'baseCoin': {
'total': '3490057203',
'available': '3490057203'
},
'requiredStorageDeposit': '98250300',
'nativeTokens': [
{
'tokenId': '0x08b83d49922e341d2cb45159707cfafdc9dc8fdb9d119543480dbaa5773eed8c4a0100000000',
'total': '0x64',
'available': '0x64'
}
],
'nfts': [
'0xceae643ff7c112a3adce8f55f7953ba0707ade21256a7a09068c0b47f7c62c5b',
'0x1e808b7c6e603aaeb5f718881a74fedae72981ac7d5f0294eb561cad0e653566',
'0x77133189021f50d8d66e0678e553af9f46a832a24239653d3555edb8dc859e1f',
'0x17f97185f80fa56eab974de6b7bbb80fa812d4e8e37090d166a0a41da129cebc',
'0x1b670afba8d59a445cbaf167f1fda05879362e3ea034f5c4a0979fbeb5a3964b',
'0x3f0e11e9d9f48a57d0fba43d7d1158ee673cb8055f80a5ce45ad174c962c0d8a',
'0xdc8be91d779aac048aa9001ab99ecf12cf62a4701185a95f6206a1a201bfbe7c'
],
'aliases': [
],
'foundries': [
],
'potentiallyLockedOutputs': {
'0xa10af6cc21fa0b1b67962af7082eb25d8b4aafe50fedda469179f469ab5e33be5300': False,
[...]
}
]
{
"baseCoin": {
"total": "2096947200",
"available": "2096947200"
},
"requiredStorageDeposit": "323500",
"nativeTokens": [
{
"tokenId": "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0100000000",
"total": "0x3233",
"available": "0x3233"
},
{
"tokenId": "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0200000000",
"total": "0x3233",
"available": "0x3233"
},
{
"tokenId": "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"total": "0x17",
"available": "0x17"
}
],
"nfts": [
"0x60b3a254d7704773c298882b7b93f70d12e1cc4a0b91d0b1037f6ea64d76b249",
"0xf95f4d5344217a2ba19a6c19a47f97d267edf8c4d76a7b8c08072ad35acbebbe"
],
"aliases": [
"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef"
],
"foundries": [
"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0100000000"
],
"potentiallyLockedOutputs": {}
}