Checking balances and coins
First, one should remember that, with UTXOs, each coin is unique. Each UTXO corresponds to a unique coin, and said coin has a corresponding amount (the same way a dollar bill has either 10$ or 5$ face value). So, when you want to query the balance for a given asset ID, you want to query the sum of the amount in each unspent coin. This querying is done very easily with a wallet:
let asset_id: AssetId = BASE_ASSET_ID;
let balance: u64 = wallet.get_asset_balance(&asset_id).await?;
If you want to query all the balances (i.e., get the balance for each asset ID in that wallet), then it is as simple as:
let balances: HashMap<String, u64> = wallet.get_balances().await?;
The return type is a HashMap
, where the key is the asset ID's hex string, and the value is the corresponding balance. For example, we can get the base asset balance with:
let asset_id_key = format!("{asset_id:#x}");
let asset_balance = balances.get(&asset_id_key).unwrap();