Accounts balances (#815)

Co-authored-by: wiesche <wiesche89@googlemail.com>
This commit is contained in:
ardocrat
2026-09-28 11:20:01 +03:00
committed by GitHub
co-authored by wiesche
parent 452a3ec51b
commit 2d87b8c894
23 changed files with 525 additions and 153 deletions
+51 -25
View File
@@ -31,6 +31,7 @@ use crate::util::secp::key::SecretKey;
use crate::util::{Mutex, ZeroingString};
use crate::{controller, display};
use grin_wallet_libwallet::wallet_lock;
use qr_code::QrCode;
use serde_json as json;
use std::fs::File;
@@ -52,7 +53,7 @@ fn show_recovery_phrase(phrase: ZeroingString) {
/// Arguments common to all wallet commands
#[derive(Clone)]
pub struct GlobalArgs {
pub account: String,
pub account: Option<String>,
pub api_secret: Option<String>,
pub node_api_secret: Option<String>,
pub show_spent: bool,
@@ -263,6 +264,8 @@ where
/// Arguments for account command
pub struct AccountArgs {
pub create: Option<String>,
pub minimum_confirmations: u64,
pub active: Option<String>,
}
pub fn account<L, C, K>(
@@ -275,20 +278,7 @@ where
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
if args.create.is_none() {
let res = {
let acct_mappings = owner_api.accounts(keychain_mask)?;
// give logging thread a moment to catch up
thread::sleep(Duration::from_millis(200));
display::accounts(acct_mappings);
Ok(())
};
if let Err(e) = res {
error!("Error listing accounts: {}", e);
return Err(Error::LibWallet(e));
}
} else {
let label = args.create.unwrap();
if let Some(label) = args.create {
let res = {
owner_api.create_account_path(keychain_mask, &label)?;
thread::sleep(Duration::from_millis(200));
@@ -300,6 +290,31 @@ where
error!("Error creating account '{}': {}", label, e);
return Err(Error::LibWallet(e));
}
} else if let Some(label) = args.active {
let res = {
owner_api.set_active_account(keychain_mask, &label)?;
thread::sleep(Duration::from_millis(200));
info!("Account: '{}' set as active!", label);
Ok(())
};
if let Err(e) = res {
thread::sleep(Duration::from_millis(200));
error!("Error setting account '{}' as active: {}", label, e);
return Err(Error::LibWallet(e));
}
} else {
let res = {
let acct_mappings =
owner_api.accounts_info(keychain_mask, args.minimum_confirmations)?;
// give logging thread a moment to catch up
thread::sleep(Duration::from_millis(200));
display::accounts(acct_mappings);
Ok(())
};
if let Err(e) = res {
error!("Error listing accounts: {}", e);
return Err(Error::LibWallet(e));
}
}
Ok(())
}
@@ -702,7 +717,6 @@ pub struct ReceiveArgs {
pub fn receive<L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<&SecretKey>,
g_args: &GlobalArgs,
args: ReceiveArgs,
mut tor_config: TorConfig,
test_mode: bool,
@@ -752,7 +766,7 @@ where
owner_api.config_path(),
km,
|api| {
slate = api.receive_tx(&slate, Some(&g_args.account), None)?;
slate = api.receive_tx(&slate, None, None)?;
Ok(())
},
)?;
@@ -1174,7 +1188,6 @@ pub struct InfoArgs {
pub fn info<L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<&SecretKey>,
g_args: &GlobalArgs,
args: InfoArgs,
dark_scheme: bool,
) -> Result<(), Error>
@@ -1186,8 +1199,9 @@ where
let updater_running = owner_api.updater_running.load(Ordering::Relaxed);
let (validated, wallet_info) =
owner_api.retrieve_summary_info(keychain_mask, true, args.minimum_confirmations)?;
let account = account_label(owner_api)?;
display::info(
&g_args.account,
&account,
&wallet_info,
validated || updater_running,
dark_scheme,
@@ -1210,8 +1224,9 @@ where
let res = owner_api.node_height(keychain_mask)?;
let (validated, outputs) =
owner_api.retrieve_outputs(keychain_mask, g_args.show_spent, true, None)?;
let account = account_label(owner_api)?;
display::outputs(
&g_args.account,
&account,
res.height,
validated || updater_running,
outputs,
@@ -1230,7 +1245,6 @@ pub struct TxsArgs {
pub fn txs<L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<&SecretKey>,
g_args: &GlobalArgs,
args: TxsArgs,
dark_scheme: bool,
) -> Result<(), Error>
@@ -1249,8 +1263,9 @@ where
let first_tx = args
.count
.map_or(0, |c| txs.len().saturating_sub(c as usize));
let account = account_label(owner_api)?;
display::txs(
&g_args.account,
&account,
res.height,
validated || updater_running,
&txs[first_tx..],
@@ -1276,7 +1291,7 @@ where
if id.is_some() {
let (_, outputs) = owner_api.retrieve_outputs(keychain_mask, true, false, id)?;
display::outputs(
&g_args.account,
&account,
res.height,
validated || updater_running,
outputs,
@@ -1459,7 +1474,6 @@ where
/// Payment Proof Address
pub fn address<L, C, K>(
owner_api: &mut Owner<L, C, K>,
g_args: &GlobalArgs,
keychain_mask: Option<&SecretKey>,
) -> Result<(), Error>
where
@@ -1469,14 +1483,26 @@ where
{
// Just address at derivation index 0 for now
let address = owner_api.get_slatepack_address(keychain_mask, 0)?;
let account = account_label(owner_api)?;
println!();
println!("Address for account - {}", g_args.account);
println!("Address for account - {}", account);
println!("-------------------------------------");
println!("{}", address);
println!();
Ok(())
}
/// Get current account label.
fn account_label<L, C, K>(owner_api: &mut Owner<L, C, K>) -> Result<String, Error>
where
L: WalletLCProvider<'static, C, K> + 'static,
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
wallet_lock!(owner_api.wallet_inst, w);
Ok(w.active_account().label)
}
/// Proof Export Args
pub struct ProofExportArgs {
pub output_file: String,
+8
View File
@@ -540,13 +540,20 @@ pub fn accounts(acct_mappings: Vec<AcctPathMapping>) {
mMG->"Name",
bMG->"Parent Output Key",
bMG->"Slatepack Address (Index 0)",
bMG->"Spendable balance",
]);
for m in acct_mappings {
let slatepack_path = address::address_derivation_path(&m.path, 0).to_bip_32_string();
let spendable = if let Some(info) = m.info {
amount_to_hr_string(info.amount_currently_spendable, true)
} else {
"-".to_string()
};
table.add_row(row![
bFC->m.label,
bGC->m.path.to_bip_32_string(),
bGC->slatepack_path,
bGC->spendable,
]);
}
table.set_format(
@@ -562,6 +569,7 @@ pub fn accounts(acct_mappings: Vec<AcctPathMapping>) {
let width = table.to_string().find('\n').unwrap_or(0);
println!("{:^1$}", "BIP-32 Derivation Path", width);
table.printstd();
println!("Balances use local data and may be out of date.");
println!();
}
+74 -10
View File
@@ -81,7 +81,7 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// test default accounts exist
{
let accounts = api1.accounts(mask1)?;
let accounts = api1.accounts_info(mask1, 2)?;
assert_eq!(accounts[0].label, "default");
assert_eq!(accounts[0].path, ExtKeychain::derive_key_id(2, 0, 0, 0, 0));
}
@@ -97,6 +97,53 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// trying to add same label again should fail
let res = api1.create_account_path(mask1, "account1");
assert!(res.is_err());
// Check missing flags and temporary account changes
for saved in [None, Some(true)] {
{
wallet_inst!(wallet1, w);
let mut account = w.get_acct_path("default".to_owned())?.unwrap();
account.current = saved;
let mut batch = w.batch(mask1)?;
batch.save_acct_path(account)?;
batch.commit()?;
}
for active in ["default", "account1"] {
{
wallet_inst!(wallet1, w);
w.set_account_by_name(active)?;
}
for accounts in [api1.accounts(mask1)?, api1.accounts_info(mask1, 2)?] {
assert_eq!(accounts.len(), 4);
assert_eq!(accounts[0].label, active);
for account in accounts {
assert_eq!(account.current, Some(account.label == active));
}
}
// Keep the saved selection unchanged
wallet_inst!(wallet1, w);
for account in w.acct_path_iter()? {
let expected = if account.label == "default" {
saved
} else {
None
};
assert_eq!(account.current, expected);
}
}
}
// Restore the saved account on reopen
api1.set_active_account(mask1, "account1")?;
{
wallet_inst!(wallet1, w);
w.set_account_by_name("default")?;
assert_eq!(w.active_account().label, "default");
}
api1.close_wallet(None)?;
api1.open_wallet(None, "".into(), false)?;
{
wallet_inst!(wallet1, w);
assert_eq!(w.active_account().label, "account1");
}
}
// add account to wallet 2
@@ -108,20 +155,20 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Default wallet 2 to listen on that account
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("listener_account")?;
w.set_account_by_name("listener_account")?;
}
// Mine into two different accounts in the same wallet
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
assert_eq!(w.parent_key_id(), ExtKeychain::derive_key_id(2, 1, 0, 0, 0));
}
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 7, false);
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("account2")?;
w.set_account_by_name("account2")?;
assert_eq!(w.parent_key_id(), ExtKeychain::derive_key_id(2, 2, 0, 0, 0));
}
let _ = test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 5, false);
@@ -141,12 +188,28 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// now check second account
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
{
let labels: Vec<_> = api1.accounts(mask1)?.into_iter().map(|a| a.label).collect();
let labels: Vec<_> = api1
.accounts_info(mask1, 2)?
.into_iter()
.map(|a| a.label)
.collect();
assert_eq!(labels, ["account1", "default", "account2", "account3"]);
assert!(api1.accounts(mask1)?.iter().all(|a| a.info.is_none()));
for confirmations in [0, 1, 2, 10] {
let accounts = api1.accounts_info(mask1, confirmations)?;
assert_eq!(accounts[0].info.as_ref().unwrap().last_confirmed_height, 0);
assert_eq!(accounts[2].info.as_ref().unwrap().last_confirmed_height, 12);
for account in accounts {
api1.set_active_account(mask1, &account.label)?;
let (_, summary) = api1.retrieve_summary_info(mask1, false, confirmations)?;
assert_eq!(account.info.unwrap(), summary);
}
api1.set_active_account(mask1, "account1")?;
}
// check last confirmed height on this account is different from above (should be 0)
let (_, wallet1_info) = api1.retrieve_summary_info(mask1, false, 1)?;
assert_eq!(wallet1_info.last_confirmed_height, 0);
@@ -159,10 +222,11 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
let (_, txs) = api1.retrieve_txs(mask1, true, None, None, None)?;
assert_eq!(txs.len(), 7);
}
// should be nothing in default account
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("default")?;
w.set_account_by_name("default")?;
}
{
let (_, wallet1_info) = api1.retrieve_summary_info(mask1, false, 1)?;
@@ -180,7 +244,7 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Send a tx to another wallet
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
{
let args = InitTxArgs {
@@ -218,7 +282,7 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// other account should be untouched
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("account2")?;
w.set_account_by_name("account2")?;
}
{
let (_, wallet1_info) = api1.retrieve_summary_info(mask1, false, 1)?;
@@ -241,7 +305,7 @@ fn accounts_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Default account on wallet 2 should be untouched
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("default")?;
w.set_account_by_name("default")?;
}
{
let (_, wallet2_info) = api2.retrieve_summary_info(mask2, false, 1)?;
+2 -2
View File
@@ -88,7 +88,7 @@ fn file_exchange_test_impl(test_dir: &'static str, use_bin: bool) -> Result<(),
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut bh = 10u64;
let _ =
@@ -130,7 +130,7 @@ fn file_exchange_test_impl(test_dir: &'static str, use_bin: bool) -> Result<(),
// Get some mining done
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
let mut slate = PathToSlate((&send_file).into()).get_tx()?.0;
+1 -1
View File
@@ -80,7 +80,7 @@ fn invoice_tx_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut _bh = 10u64;
let _ =
+2 -2
View File
@@ -77,11 +77,11 @@ fn late_lock_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
test_framework::award_blocks_to_wallet(&chain, wallet1.clone(), mask1, 10, false)?;
+2 -2
View File
@@ -72,7 +72,7 @@ fn mwixnet_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut bh = 10u64;
let _ =
@@ -121,7 +121,7 @@ fn mwixnet_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Check total in 'listener' account
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("listener")?;
w.set_account_by_name("listener")?;
}
let (wallet1_refreshed, wallet1_info) = api1.retrieve_summary_info(mask1, true, 1)?;
assert!(wallet1_refreshed);
+6 -6
View File
@@ -86,7 +86,7 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut bh = 10u64;
let _ =
@@ -120,7 +120,7 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
// wallet 1 receives file to different account, completes
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("listener")?;
w.set_account_by_name("listener")?;
}
wallet::controller::foreign_single_use(
@@ -138,7 +138,7 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
// wallet 1 receives file to different account, completes
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
// wallet 1 finalize
@@ -164,7 +164,7 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("listener")?;
w.set_account_by_name("listener")?;
}
let (wallet1_refreshed, wallet1_info) = api1.retrieve_summary_info(mask1, true, 1)?;
@@ -175,11 +175,11 @@ fn file_repost_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error>
// as above, but synchronously
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
let amount = 60_000_000_000;
+2 -2
View File
@@ -70,7 +70,7 @@ fn self_send_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut bh = 10u64;
let _ =
@@ -119,7 +119,7 @@ fn self_send_test_impl(test_dir: &'static str) -> Result<(), libwallet::Error> {
// Check total in 'listener' account
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("listener")?;
w.set_account_by_name("listener")?;
}
let (wallet1_refreshed, wallet1_info) = api1.retrieve_summary_info(mask1, true, 1)?;
assert!(wallet1_refreshed);
+2 -2
View File
@@ -137,7 +137,7 @@ fn slatepack_exchange_test_impl(
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let mut bh = 10u64;
let _ =
@@ -213,7 +213,7 @@ fn slatepack_exchange_test_impl(
// Get some mining done
{
wallet_inst!(wallet2, w);
w.set_parent_key_id_by_name("account1")?;
w.set_account_by_name("account1")?;
}
let (mut slatepack, mut slate) =
+1 -1
View File
@@ -80,7 +80,7 @@ fn updater_thread_test_impl(test_dir: &'static str) -> Result<(), libwallet::Err
// Get some mining done
{
wallet_inst!(wallet1, w);
w.set_parent_key_id_by_name("mining")?;
w.set_account_by_name("mining")?;
}
let bh = 10u64;
let _ =