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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use primitive_types::U256;
use crate::{
types::block::output::{unlock_condition::UnlockCondition, FoundryId, NativeTokensBuilder, Output, Rent},
wallet::account::{
handle::AccountHandle,
operations::helpers::time::can_output_be_unlocked_forever_from_now_on,
types::{AccountBalance, NativeTokensBalance},
OutputsToClaim,
},
};
impl AccountHandle {
/// Get the AccountBalance
pub async fn balance(&self) -> crate::wallet::Result<AccountBalance> {
log::debug!("[BALANCE] get balance");
let mut account_balance = AccountBalance::default();
#[cfg(feature = "participation")]
{
account_balance.base_coin.voting_power = self.get_voting_power().await?;
}
let unlockable_outputs_with_multiple_unlock_conditions = self
.get_unlockable_outputs_with_additional_unlock_conditions(OutputsToClaim::All)
.await?;
let account_addresses = self.addresses().await?;
let network_id = self.client.get_network_id().await?;
let rent_structure = self.client.get_rent_structure().await?;
let local_time = self.client.get_time_checked().await?;
let mut total_rent_amount = 0;
let mut total_native_tokens = NativeTokensBuilder::new();
let account = self.read().await;
let relevant_unspent_outputs = account
.unspent_outputs
.values()
// Check if output is from the network we're currently connected to
.filter(|data| data.network_id == network_id)
.map(|data| (&data.output_id, &data.output));
for (output_id, output) in relevant_unspent_outputs {
let rent = output.rent_cost(&rent_structure);
// Add alias and foundry outputs here because they can't have a [`StorageDepositReturnUnlockCondition`]
// or time related unlock conditions
match output {
Output::Alias(output) => {
// Add amount
account_balance.base_coin.total += output.amount();
// Add storage deposit
account_balance.required_storage_deposit.alias += rent;
if !account.locked_outputs.contains(output_id) {
total_rent_amount += rent;
}
// Add native tokens
total_native_tokens.add_native_tokens(output.native_tokens().clone())?;
let alias_id = output.alias_id_non_null(output_id);
account_balance.aliases.push(alias_id);
}
Output::Foundry(output) => {
// Add amount
account_balance.base_coin.total += output.amount();
// Add storage deposit
account_balance.required_storage_deposit.foundry += rent;
if !account.locked_outputs.contains(output_id) {
total_rent_amount += rent;
}
// Add native tokens
total_native_tokens.add_native_tokens(output.native_tokens().clone())?;
account_balance.foundries.push(output.id());
}
_ => {
// If there is only an [AddressUnlockCondition], then we can spend the output at any time without
// restrictions
if let [UnlockCondition::Address(_)] = output
.unlock_conditions()
.expect("output needs to have unlock conditions")
.as_ref()
{
// add nft_id for nft outputs
if let Output::Nft(output) = &output {
let nft_id = output.nft_id_non_null(output_id);
account_balance.nfts.push(nft_id);
}
// Add amount
account_balance.base_coin.total += output.amount();
// Add storage deposit
if output.is_basic() {
account_balance.required_storage_deposit.basic += rent;
if output
.native_tokens()
.map(|native_tokens| !native_tokens.is_empty())
.unwrap_or(false)
&& !account.locked_outputs.contains(output_id)
{
total_rent_amount += rent;
}
} else if output.is_nft() {
account_balance.required_storage_deposit.nft += rent;
if !account.locked_outputs.contains(output_id) {
total_rent_amount += rent;
}
}
// Add native tokens
if let Some(native_tokens) = output.native_tokens() {
total_native_tokens.add_native_tokens(native_tokens.clone())?;
}
} else {
// if we have multiple unlock conditions for basic or nft outputs, then we might can't spend the
// balance at the moment or in the future
let output_can_be_unlocked_now =
unlockable_outputs_with_multiple_unlock_conditions.contains(output_id);
// For outputs that are expired or have a timelock unlock condition, but no expiration unlock
// condition and we then can unlock them, then they can never be not available for us anymore
// and should be added to the balance
if output_can_be_unlocked_now {
// check if output can be unlocked always from now on, in that case it should be added to
// the total amount
let output_can_be_unlocked_now_and_in_future = can_output_be_unlocked_forever_from_now_on(
// We use the addresses with unspent outputs, because other addresses of the
// account without unspent outputs can't be related to this output
&account.addresses_with_unspent_outputs,
output,
local_time,
);
if output_can_be_unlocked_now_and_in_future {
// If output has a StorageDepositReturnUnlockCondition, the amount of it should be
// subtracted, because this part needs to be sent back
let amount = output
.unlock_conditions()
.and_then(|u| u.storage_deposit_return())
.map_or_else(
|| output.amount(),
|sdr| {
if account_addresses
.iter()
.any(|a| a.address.inner == *sdr.return_address())
{
// sending to ourself, we get the full amount
output.amount()
} else {
// Sending to someone else
output.amount() - sdr.amount()
}
},
);
// add nft_id for nft outputs
if let Output::Nft(output) = &output {
let nft_id = output.nft_id_non_null(output_id);
account_balance.nfts.push(nft_id);
}
// Add amount
account_balance.base_coin.total += amount;
// Add storage deposit
if output.is_basic() {
account_balance.required_storage_deposit.basic += rent;
// Amount for basic outputs isn't added to total_rent_amount if there aren't native
// tokens, since we can spend it without burning.
if output
.native_tokens()
.map(|native_tokens| !native_tokens.is_empty())
.unwrap_or(false)
&& !account.locked_outputs.contains(output_id)
{
total_rent_amount += rent;
}
} else if output.is_nft() {
account_balance.required_storage_deposit.nft += rent;
if !account.locked_outputs.contains(output_id) {
total_rent_amount += rent;
}
}
// Add native tokens
if let Some(native_tokens) = output.native_tokens() {
total_native_tokens.add_native_tokens(native_tokens.clone())?;
}
} else {
// only add outputs that can't be locked now and at any point in the future
account_balance.potentially_locked_outputs.insert(*output_id, true);
}
} else {
// Don't add expired outputs that can't ever be unlocked by us
if let Some(expiration) = output
.unlock_conditions()
.expect("output needs to have unlock conditions")
.expiration()
{
// Not expired, could get unlockable when it's expired, so we insert it
if local_time < expiration.timestamp() {
account_balance.potentially_locked_outputs.insert(*output_id, false);
}
} else {
account_balance.potentially_locked_outputs.insert(*output_id, false);
}
}
}
}
}
}
// for `available` get locked_outputs, sum outputs amount and subtract from total_amount
log::debug!("[BALANCE] locked outputs: {:#?}", account.locked_outputs);
let mut locked_amount = 0;
let mut locked_native_tokens = NativeTokensBuilder::new();
for locked_output in &account.locked_outputs {
if let Some(output_data) = account.unspent_outputs.get(locked_output) {
// Only check outputs that are in this network
if output_data.network_id == network_id {
locked_amount += output_data.output.amount();
if let Some(native_tokens) = output_data.output.native_tokens() {
locked_native_tokens.add_native_tokens(native_tokens.clone())?;
}
}
}
}
log::debug!(
"[BALANCE] total_amount: {}, locked_amount: {}, total_rent_amount: {}",
account_balance.base_coin.total,
locked_amount,
total_rent_amount,
);
locked_amount += total_rent_amount;
for native_token in total_native_tokens.finish_vec()? {
// Check if some amount is currently locked
let locked_amount = locked_native_tokens.iter().find_map(|(id, amount)| {
if id == native_token.token_id() {
Some(amount)
} else {
None
}
});
let metadata = account
.native_token_foundries
.get(&FoundryId::from(*native_token.token_id()))
.and_then(|foundry| foundry.immutable_features().metadata())
.cloned();
account_balance.native_tokens.push(NativeTokensBalance {
token_id: *native_token.token_id(),
metadata,
total: native_token.amount(),
available: native_token.amount() - *locked_amount.unwrap_or(&U256::from(0u8)),
})
}
#[cfg(not(feature = "participation"))]
{
account_balance.base_coin.available = account_balance.base_coin.total.saturating_sub(locked_amount);
}
#[cfg(feature = "participation")]
{
account_balance.base_coin.available = account_balance
.base_coin
.total
.saturating_sub(locked_amount)
.saturating_sub(account_balance.base_coin.voting_power);
}
Ok(account_balance)
}
}