-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy path02_get_address_balance.rs
72 lines (59 loc) · 2.4 KB
/
02_get_address_balance.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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_response = 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_response.items).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(())
}