@@ -4,7 +4,10 @@ use fuels::{
4
4
accounts:: { wallet:: Wallet , ViewOnlyAccount } ,
5
5
prelude:: * ,
6
6
} ;
7
- use std:: { collections:: BTreeMap , path:: Path } ;
7
+ use std:: {
8
+ collections:: { BTreeMap , HashMap } ,
9
+ path:: Path ,
10
+ } ;
8
11
9
12
use crate :: {
10
13
account:: {
@@ -25,21 +28,66 @@ pub struct Balance {
25
28
accounts : bool ,
26
29
}
27
30
28
- pub async fn cli ( wallet_path : & Path , balance : & Balance ) -> Result < ( ) > {
31
+ /// Whether to verify cached accounts or not.
32
+ ///
33
+ /// To verify cached accounts we require wallet vault password.
34
+ pub enum AccountVerification {
35
+ No ,
36
+ Yes ( String ) ,
37
+ }
38
+
39
+ /// List of accounts and amount of tokens they hold with different ASSET_IDs.
40
+ pub type AccountBalances = Vec < HashMap < String , u64 > > ;
41
+ /// A mapping between account index and the bech32 address for that account.
42
+ pub type AccountsMap = BTreeMap < usize , Bech32Address > ;
43
+
44
+ /// Return a map of accounts after desired verification applied in a map where each key is account
45
+ /// index and each value is the `Bech32Address` of that account.
46
+ pub fn collect_accounts_with_verification (
47
+ wallet_path : & Path ,
48
+ verification : AccountVerification ,
49
+ ) -> Result < AccountsMap > {
29
50
let wallet = load_wallet ( wallet_path) ?;
30
51
let mut addresses = read_cached_addresses ( & wallet. crypto . ciphertext ) ?;
31
- if !balance. account . unverified . unverified {
32
- let prompt = "Please enter your wallet password to verify accounts: " ;
33
- let password = rpassword:: prompt_password ( prompt) ?;
52
+ if let AccountVerification :: Yes ( password) = verification {
34
53
for ( & ix, addr) in addresses. iter_mut ( ) {
35
54
let account = derive_account ( wallet_path, ix, & password) ?;
36
55
if verify_address_and_update_cache ( ix, & account, addr, & wallet. crypto . ciphertext ) ? {
37
56
* addr = account. address ( ) . clone ( ) ;
38
57
}
39
58
}
59
+ }
60
+
61
+ Ok ( addresses)
62
+ }
63
+
64
+ /// Print collected account balances for each asset type.
65
+ pub fn print_account_balances ( accounts_map : & AccountsMap , account_balances : & AccountBalances ) {
66
+ for ( ix, balance) in accounts_map. keys ( ) . zip ( account_balances) {
67
+ let balance: BTreeMap < _ , _ > = balance
68
+ . iter ( )
69
+ . map ( |( id, & val) | ( id. clone ( ) , u128:: from ( val) ) )
70
+ . collect ( ) ;
71
+ if balance. is_empty ( ) {
72
+ continue ;
73
+ }
74
+ println ! ( "\n Account {ix} -- {}:" , accounts_map[ ix] ) ;
75
+ print_balance ( & balance) ;
76
+ }
77
+ }
78
+ pub async fn cli ( wallet_path : & Path , balance : & Balance ) -> Result < ( ) > {
79
+ let verification = if !balance. account . unverified . unverified {
80
+ let prompt = "Please enter your wallet password to verify accounts: " ;
81
+ let password = rpassword:: prompt_password ( prompt) ?;
82
+ AccountVerification :: Yes ( password)
83
+ } else {
84
+ AccountVerification :: No
40
85
} ;
41
- println ! ( "Connecting to {}" , balance. account. node_url) ;
42
- let provider = Provider :: connect ( & balance. account . node_url ) . await ?;
86
+ let addresses = collect_accounts_with_verification ( wallet_path, verification) ?;
87
+
88
+ let node_url = & balance. account . node_url ;
89
+ println ! ( "Connecting to {node_url}" ) ;
90
+ let provider = Provider :: connect ( node_url) . await ?;
43
91
println ! ( "Fetching and summing balances of the following accounts:" ) ;
44
92
for ( ix, addr) in & addresses {
45
93
println ! ( " {ix:>3}: {addr}" ) ;
@@ -52,17 +100,7 @@ pub async fn cli(wallet_path: &Path, balance: &Balance) -> Result<()> {
52
100
futures:: future:: try_join_all ( accounts. iter ( ) . map ( |acc| acc. get_balances ( ) ) ) . await ?;
53
101
54
102
if balance. accounts {
55
- for ( ix, balance) in addresses. keys ( ) . zip ( & account_balances) {
56
- let balance: BTreeMap < _ , _ > = balance
57
- . iter ( )
58
- . map ( |( id, & val) | ( id. clone ( ) , u128:: from ( val) ) )
59
- . collect ( ) ;
60
- if balance. is_empty ( ) {
61
- continue ;
62
- }
63
- println ! ( "\n Account {ix}:" ) ;
64
- print_balance ( & balance) ;
65
- }
103
+ print_account_balances ( & addresses, & account_balances) ;
66
104
}
67
105
68
106
let mut total_balance = BTreeMap :: default ( ) ;
0 commit comments