Skip to content

Commit

Permalink
feat: display unconfirmed balance in clients
Browse files Browse the repository at this point in the history
unconfirmed balance displays in dashboard overview and there is also a
neptune-cli command
  • Loading branch information
dan-da committed Nov 1, 2024
1 parent 069637c commit 203fb3a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/bin/dashboard_src/overview_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use super::screen::Screen;
#[derive(Debug, Clone)]
pub struct OverviewData {
available_balance: Option<NeptuneCoins>,
available_unconfirmed_balance: Option<NeptuneCoins>,
timelocked_balance: Option<NeptuneCoins>,
confirmations: Option<BlockHeight>,
synchronization_percentage: Option<f64>,
Expand Down Expand Up @@ -73,6 +74,7 @@ impl OverviewData {
pub fn new(network: Network, listen_address: Option<SocketAddr>) -> Self {
Self {
available_balance: Default::default(),
available_unconfirmed_balance: Default::default(),
timelocked_balance: Default::default(),
confirmations: Default::default(),
synchronization_percentage: Default::default(),
Expand Down Expand Up @@ -102,6 +104,7 @@ impl OverviewData {
pub async fn test() -> Self {
OverviewData {
available_balance: Some(NeptuneCoins::zero()),
available_unconfirmed_balance: Some(NeptuneCoins::zero()),
timelocked_balance: Some(NeptuneCoins::zero()),
confirmations: Some(17.into()),
synchronization_percentage: Some(99.5),
Expand Down Expand Up @@ -218,6 +221,7 @@ impl OverviewScreen {
own_overview_data.authenticated_peer_count=Some(0);
own_overview_data.syncing=resp.syncing;
own_overview_data.available_balance = Some(resp.available_balance);
own_overview_data.available_unconfirmed_balance = Some(resp.available_unconfirmed_balance);
own_overview_data.timelocked_balance = Some(resp.timelocked_balance);
own_overview_data.is_mining = resp.is_mining;
own_overview_data.confirmations = resp.confirmations;
Expand Down Expand Up @@ -382,6 +386,10 @@ impl Widget for OverviewScreen {
None => " ".to_string(),
},
));
lines.push(format!(
"unconfirmed balance: {}",
dashifnotset!(data.available_unconfirmed_balance),
));
lines.push(format!(
"time-locked balance: {}",
dashifnotset!(data.timelocked_balance),
Expand Down
5 changes: 5 additions & 0 deletions src/bin/neptune-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ enum Command {
block_selector: BlockSelector,
},
SyncedBalance,
SyncedBalanceUnconfirmed,
WalletStatus,
OwnReceivingAddress,
ListCoins,
Expand Down Expand Up @@ -415,6 +416,10 @@ async fn main() -> Result<()> {
let val = client.synced_balance(ctx).await?;
println!("{val}");
}
Command::SyncedBalanceUnconfirmed => {
let val = client.synced_balance_unconfirmed(ctx).await?;
println!("{val}");
}
Command::WalletStatus => {
let wallet_status: WalletStatus = client.wallet_status(ctx).await?;
println!("{}", serde_json::to_string_pretty(&wallet_status)?);
Expand Down
7 changes: 4 additions & 3 deletions src/models/state/wallet/wallet_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl WalletState {
self.mempool_unspent_utxos
.values()
.flatten()
.map(|a| &a.utxo)
.map(|au| &au.utxo)
}

pub async fn confirmed_balance(
Expand All @@ -354,13 +354,13 @@ impl WalletState {
.map(|u| u.get_native_currency_amount())
.sum(),
)
.unwrap()
.expect("balance must never be negative")
.safe_add(
self.mempool_unspent_utxos_iter()
.map(|u| u.get_native_currency_amount())
.sum(),
)
.unwrap()
.expect("balance must never overflow")
}

// note: does not verify we do not have any dups.
Expand Down Expand Up @@ -1055,6 +1055,7 @@ impl WalletState {
}
}
}

WalletStatus {
synced_unspent,
unsynced_unspent,
Expand Down
18 changes: 18 additions & 0 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub struct DashBoardOverviewDataFromClient {
pub syncing: bool,
pub available_balance: NeptuneCoins,
pub timelocked_balance: NeptuneCoins,
pub available_unconfirmed_balance: NeptuneCoins,
pub mempool_size: usize,
pub mempool_tx_count: usize,

Expand Down Expand Up @@ -118,6 +119,9 @@ pub trait RPC {
/// Get sum of unspent UTXOs.
async fn synced_balance() -> NeptuneCoins;

/// Get sum of unspent UTXOs including mempool transactions.
async fn synced_balance_unconfirmed() -> NeptuneCoins;

/// Get the client's wallet transaction history
async fn history() -> Vec<(Digest, BlockHeight, Timestamp, NeptuneCoins)>;

Expand Down Expand Up @@ -571,6 +575,15 @@ impl RPC for NeptuneRPCServer {
wallet_status.synced_unspent_available_amount(now)
}

// documented in trait. do not add doc-comment.
async fn synced_balance_unconfirmed(self, _context: tarpc::context::Context) -> NeptuneCoins {
let gs = self.state.lock_guard().await;

gs.wallet_state
.unconfirmed_balance(gs.chain.light_state().hash(), Timestamp::now())
.await
}

// documented in trait. do not add doc-comment.
async fn wallet_status(self, _context: tarpc::context::Context) -> WalletStatus {
self.state
Expand Down Expand Up @@ -660,6 +673,10 @@ impl RPC for NeptuneRPCServer {
let mempool_size = state.mempool.get_size();
let mempool_tx_count = state.mempool.len();
let cpu_temp = Self::cpu_temp_inner();
let unconfirmed_balance = state
.wallet_state
.unconfirmed_balance(tip_digest, now)
.await;

let peer_count = Some(state.net.peer_map.len());

Expand All @@ -674,6 +691,7 @@ impl RPC for NeptuneRPCServer {
syncing,
available_balance: wallet_status.synced_unspent_available_amount(now),
timelocked_balance: wallet_status.synced_unspent_timelocked_amount(now),
available_unconfirmed_balance: unconfirmed_balance,
mempool_size,
mempool_tx_count,
peer_count,
Expand Down

0 comments on commit 203fb3a

Please sign in to comment.