iota_client/api/
balance.rs1use crate::{Client, Result};
5use crypto::keys::slip10::Seed;
6
7pub struct GetBalanceBuilder<'a> {
9 client: &'a Client,
10 seed: &'a Seed,
11 account_index: usize,
12 initial_address_index: usize,
13 gap_limit: usize,
14}
15
16impl<'a> GetBalanceBuilder<'a> {
17 pub fn new(client: &'a Client, seed: &'a Seed) -> Self {
19 Self {
20 client,
21 seed,
22 account_index: 0,
23 initial_address_index: 0,
24 gap_limit: super::ADDRESS_GAP_RANGE,
25 }
26 }
27
28 pub fn with_account_index(mut self, account_index: usize) -> Self {
30 self.account_index = account_index;
31 self
32 }
33
34 pub fn with_initial_address_index(mut self, initial_address_index: usize) -> Self {
36 self.initial_address_index = initial_address_index;
37 self
38 }
39
40 pub fn with_gap_limit(mut self, gap_limit: usize) -> Self {
43 self.gap_limit = gap_limit;
44 self
45 }
46
47 pub async fn finish(self) -> Result<u64> {
49 let mut index = self.initial_address_index;
50
51 let mut balance = 0;
53 let mut found_zero_balance = 0;
55 loop {
56 let addresses = self
57 .client
58 .get_addresses(self.seed)
59 .with_account_index(self.account_index)
60 .with_range(index..index + self.gap_limit)
61 .get_all()
62 .await?;
63
64 for (address, _) in addresses {
65 let address_balance = self.client.get_address().balance(&address).await?;
66 match address_balance.balance {
67 0 => found_zero_balance += 1,
68 _ => {
69 balance += address_balance.balance;
70 found_zero_balance = 0;
72 }
73 }
74 }
75 if found_zero_balance >= self.gap_limit * 2 {
77 break;
78 }
79 index += self.gap_limit;
80 }
81
82 Ok(balance)
83 }
84}