use crate::{Client, Result};
use crypto::keys::slip10::Seed;
pub struct GetBalanceBuilder<'a> {
client: &'a Client,
seed: &'a Seed,
account_index: usize,
initial_address_index: usize,
gap_limit: usize,
}
impl<'a> GetBalanceBuilder<'a> {
pub fn new(client: &'a Client, seed: &'a Seed) -> Self {
Self {
client,
seed,
account_index: 0,
initial_address_index: 0,
gap_limit: super::ADDRESS_GAP_RANGE,
}
}
pub fn with_account_index(mut self, account_index: usize) -> Self {
self.account_index = account_index;
self
}
pub fn with_initial_address_index(mut self, initial_address_index: usize) -> Self {
self.initial_address_index = initial_address_index;
self
}
pub fn with_gap_limit(mut self, gap_limit: usize) -> Self {
self.gap_limit = gap_limit;
self
}
pub async fn finish(self) -> Result<u64> {
let mut index = self.initial_address_index;
let mut balance = 0;
let mut found_zero_balance = 0;
loop {
let addresses = self
.client
.get_addresses(self.seed)
.with_account_index(self.account_index)
.with_range(index..index + self.gap_limit)
.get_all()
.await?;
for (address, _) in addresses {
let address_balance = self.client.get_address().balance(&address).await?;
match address_balance.balance {
0 => found_zero_balance += 1,
_ => {
balance += address_balance.balance;
found_zero_balance = 0;
}
}
}
if found_zero_balance >= self.gap_limit * 2 {
break;
}
index += self.gap_limit;
}
Ok(balance)
}
}