-
Notifications
You must be signed in to change notification settings - Fork 39
/
check_balance.rs
43 lines (35 loc) · 1.41 KB
/
check_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
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we sync the account and get the balance.
//!
//! Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
//! running the `./how_tos/accounts_and_addresses/create_account.rs` example!
//!
//! Rename `.env.example` to `.env` first, then run the command:
//! ```sh
//! cargo run --release --all-features --example check_balance
//! ```
use iota_sdk::{wallet::Result, Wallet};
#[tokio::main]
async fn main() -> Result<()> {
// This example uses secrets in environment variables for simplicity which should not be done in production.
dotenvy::dotenv().ok();
for var in ["WALLET_DB_PATH", "EXPLORER_URL"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
let wallet = Wallet::builder()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
let account = wallet.get_account("Alice").await?;
// Sync and get the balance
let balance = account.sync(None).await?;
println!("{balance:#?}");
println!("ADDRESSES:");
let explorer_url = std::env::var("EXPLORER_URL").ok();
let prepended = explorer_url.map(|url| format!("{url}/addr/")).unwrap_or_default();
for address in account.addresses().await? {
println!(" - {prepended}{}", address.address());
}
Ok(())
}