iotaledger/iota.rs

How to get the balance by address?

Closed this issue · 2 comments

How do I get the balance by an address? I found an example with a SecretManager but I really just need the balance and I dont want to create a SecretManager for this use case.

Balance for addresses were supported on V 1.3.0.

There isn't a clear definition for what the balance is with the new outputs and unlock conditions, but we have an example which gets the basic outputs with address unlock condition alone and returns the amount + native tokens:

// 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()?
);

Other output types and outputs with other unlock conditions might be also part of what you understand under balance, so they would then need to be requested and processed also

Thanks @thibault-martinez, worked as expected.