Skip to content

Commit bee0964

Browse files
authored
refactor: expose balance collecting and printing functionality (#120)
This PR exposes some balance related functionality so that `forc-deploy` can use them to print and collect account information for balance queries.
1 parent 14c29a7 commit bee0964

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

src/account.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ pub(crate) fn verify_address_and_update_cache(
227227
expected_addr: &Bech32Address,
228228
wallet_ciphertext: &[u8],
229229
) -> Result<bool> {
230-
println!("Verifying account {acc_ix}");
231230
let addr = account.address();
232231
if addr == expected_addr {
233232
return Ok(true);

src/balance.rs

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use fuels::{
44
accounts::{wallet::Wallet, ViewOnlyAccount},
55
prelude::*,
66
};
7-
use std::{collections::BTreeMap, path::Path};
7+
use std::{
8+
collections::{BTreeMap, HashMap},
9+
path::Path,
10+
};
811

912
use crate::{
1013
account::{
@@ -25,21 +28,66 @@ pub struct Balance {
2528
accounts: bool,
2629
}
2730

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> {
2950
let wallet = load_wallet(wallet_path)?;
3051
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 {
3453
for (&ix, addr) in addresses.iter_mut() {
3554
let account = derive_account(wallet_path, ix, &password)?;
3655
if verify_address_and_update_cache(ix, &account, addr, &wallet.crypto.ciphertext)? {
3756
*addr = account.address().clone();
3857
}
3958
}
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!("\nAccount {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
4085
};
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?;
4391
println!("Fetching and summing balances of the following accounts:");
4492
for (ix, addr) in &addresses {
4593
println!(" {ix:>3}: {addr}");
@@ -52,17 +100,7 @@ pub async fn cli(wallet_path: &Path, balance: &Balance) -> Result<()> {
52100
futures::future::try_join_all(accounts.iter().map(|acc| acc.get_balances())).await?;
53101

54102
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!("\nAccount {ix}:");
64-
print_balance(&balance);
65-
}
103+
print_account_balances(&addresses, &account_balances);
66104
}
67105

68106
let mut total_balance = BTreeMap::default();

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod account;
2+
pub mod balance;
23
pub mod import;
34
pub mod new;
45
pub mod sign;

0 commit comments

Comments
 (0)