2018-03-05 22:33:44 +03:00
|
|
|
|
// Copyright 2018 The Grin Developers
|
2017-09-24 07:40:31 +03:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
use checker;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
use core::core::amount_to_hr_string;
|
2018-05-24 18:27:26 +03:00
|
|
|
|
use keychain::Keychain;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
use prettytable;
|
2018-05-24 18:27:26 +03:00
|
|
|
|
use types::{OutputStatus, WalletConfig, WalletData, WalletInfo};
|
2017-09-24 07:40:31 +03:00
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
|
pub fn show_info(config: &WalletConfig, keychain: &Keychain) {
|
2018-02-06 14:42:26 +03:00
|
|
|
|
let wallet_info = retrieve_info(config, keychain);
|
2018-03-04 03:19:54 +03:00
|
|
|
|
println!(
|
|
|
|
|
"\n____ Wallet Summary Info at {} ({}) ____\n",
|
|
|
|
|
wallet_info.current_height, wallet_info.data_confirmed_from
|
|
|
|
|
);
|
2018-02-06 14:42:26 +03:00
|
|
|
|
let mut table = table!(
|
|
|
|
|
[bFG->"Total", FG->amount_to_hr_string(wallet_info.total)],
|
|
|
|
|
[bFY->"Awaiting Confirmation", FY->amount_to_hr_string(wallet_info.amount_awaiting_confirmation)],
|
|
|
|
|
[bFY->"Confirmed but Still Locked", FY->amount_to_hr_string(wallet_info.amount_confirmed_but_locked)],
|
|
|
|
|
[bFG->"Currently Spendable", FG->amount_to_hr_string(wallet_info.amount_currently_spendable)],
|
|
|
|
|
[Fw->"---------", Fw->"---------"],
|
|
|
|
|
[Fr->"(Locked by previous transaction)", Fr->amount_to_hr_string(wallet_info.amount_locked)]
|
|
|
|
|
);
|
|
|
|
|
table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
|
|
|
|
|
table.printstd();
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
|
|
if !wallet_info.data_confirmed {
|
|
|
|
|
println!(
|
2018-03-04 03:19:54 +03:00
|
|
|
|
"\nWARNING: Failed to verify wallet contents with grin server. \
|
|
|
|
|
Above info is maybe not fully updated or invalid! \
|
|
|
|
|
Check that your `grin server` is OK, or see `wallet help restore`"
|
2018-02-06 14:42:26 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 20:56:09 +03:00
|
|
|
|
pub fn retrieve_info(config: &WalletConfig, keychain: &Keychain) -> WalletInfo {
|
2017-11-08 03:44:20 +03:00
|
|
|
|
let result = checker::refresh_outputs(&config, &keychain);
|
2017-09-24 07:40:31 +03:00
|
|
|
|
|
2018-02-06 14:42:26 +03:00
|
|
|
|
let ret_val = WalletData::read_wallet(&config.data_file_dir, |wallet_data| {
|
2018-01-13 21:27:40 +03:00
|
|
|
|
let (current_height, from) = match checker::get_tip_from_node(config) {
|
|
|
|
|
Ok(tip) => (tip.height, "from server node"),
|
2017-11-01 02:32:33 +03:00
|
|
|
|
Err(_) => match wallet_data.outputs.values().map(|out| out.height).max() {
|
2018-01-13 21:27:40 +03:00
|
|
|
|
Some(height) => (height, "from wallet"),
|
|
|
|
|
None => (0, "node/wallet unavailable"),
|
2017-11-01 02:32:33 +03:00
|
|
|
|
},
|
2017-10-18 23:47:37 +03:00
|
|
|
|
};
|
2018-01-17 06:03:40 +03:00
|
|
|
|
let mut unspent_total = 0;
|
|
|
|
|
let mut unspent_but_locked_total = 0;
|
|
|
|
|
let mut unconfirmed_total = 0;
|
|
|
|
|
let mut locked_total = 0;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
for out in wallet_data
|
2017-10-12 19:56:44 +03:00
|
|
|
|
.outputs
|
2017-10-06 23:10:30 +03:00
|
|
|
|
.values()
|
2017-11-10 17:03:31 +03:00
|
|
|
|
.filter(|out| out.root_key_id == keychain.root_key_id())
|
|
|
|
|
{
|
|
|
|
|
if out.status == OutputStatus::Unspent {
|
2018-01-17 06:03:40 +03:00
|
|
|
|
unspent_total += out.value;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
if out.lock_height > current_height {
|
2018-03-04 03:19:54 +03:00
|
|
|
|
unspent_but_locked_total += out.value;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if out.status == OutputStatus::Unconfirmed && !out.is_coinbase {
|
2018-01-17 06:03:40 +03:00
|
|
|
|
unconfirmed_total += out.value;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
}
|
|
|
|
|
if out.status == OutputStatus::Locked {
|
2018-01-17 06:03:40 +03:00
|
|
|
|
locked_total += out.value;
|
2017-11-10 17:03:31 +03:00
|
|
|
|
}
|
2018-03-04 03:19:54 +03:00
|
|
|
|
}
|
2017-11-10 17:03:31 +03:00
|
|
|
|
|
2018-02-06 14:42:26 +03:00
|
|
|
|
let mut data_confirmed = true;
|
|
|
|
|
if let Err(_) = result {
|
|
|
|
|
data_confirmed = false;
|
|
|
|
|
}
|
2018-02-28 20:56:09 +03:00
|
|
|
|
Ok(WalletInfo {
|
2018-03-04 03:19:54 +03:00
|
|
|
|
current_height: current_height,
|
|
|
|
|
total: unspent_total + unconfirmed_total,
|
2018-02-06 14:42:26 +03:00
|
|
|
|
amount_awaiting_confirmation: unconfirmed_total,
|
|
|
|
|
amount_confirmed_but_locked: unspent_but_locked_total,
|
2018-03-04 03:19:54 +03:00
|
|
|
|
amount_currently_spendable: unspent_total - unspent_but_locked_total,
|
2018-02-06 14:42:26 +03:00
|
|
|
|
amount_locked: locked_total,
|
|
|
|
|
data_confirmed: data_confirmed,
|
|
|
|
|
data_confirmed_from: String::from(from),
|
2018-02-28 20:56:09 +03:00
|
|
|
|
})
|
2017-09-24 07:40:31 +03:00
|
|
|
|
});
|
2018-02-06 14:42:26 +03:00
|
|
|
|
ret_val.unwrap()
|
2017-09-24 07:40:31 +03:00
|
|
|
|
}
|