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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use anyhow::Result;
use iota_crypto_preview::Kerl;
use iota_signing_preview::IotaSeed;
use crate::response::Input;
use crate::Client;
pub struct GetInputsBuilder<'a> {
seed: &'a IotaSeed<Kerl>,
index: u64,
security: u8,
threshold: u64,
}
impl<'a> GetInputsBuilder<'a> {
pub(crate) fn new(seed: &'a IotaSeed<Kerl>) -> Self {
Self {
seed: seed,
index: 0,
security: 2,
threshold: 0,
}
}
pub fn index(mut self, index: u64) -> Self {
self.index = index;
self
}
pub fn security(mut self, security: u8) -> Self {
self.security = security;
self
}
pub fn threshold(mut self, threshold: u64) -> Self {
self.threshold = threshold;
self
}
pub async fn generate(self) -> Result<(u64, Vec<Input>)> {
if self.threshold == 0 {
return Ok((0, Vec::default()));
}
let mut index = self.index;
let mut total = 0;
let mut inputs = Vec::new();
let mut zero_balance_warning = 5;
while zero_balance_warning != 0 {
let (next_index, address) = Client::get_new_address(self.seed)
.index(index)
.security(self.security)
.generate()
.await?;
let balance = Client::get_balances()
.addresses(&[address.clone()])
.send()
.await?
.balances[0];
if balance == 0 {
zero_balance_warning -= 1;
} else {
zero_balance_warning = 5;
}
total += balance;
index = next_index;
inputs.push(Input {
address,
balance,
index,
});
index += 1;
if total >= self.threshold {
return Ok((total, inputs));
}
}
Err(anyhow!("Cannot find enough inputs to satisify threshold"))
}
}