Skip to content

Commit

Permalink
feat: truncate balance value if it's too large
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Jan 12, 2025
1 parent f67b8ed commit b0f6fb8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ use super::{
},
};

use alloy::primitives::U256;
use gloo_timers::future::TimeoutFuture;
use leptos::{logging, prelude::*, task::spawn_local};
use rand::Rng;

// Duration of each alert message shows in the UI
const ALERT_MSG_DURATION_MILLIS: u32 = 9_000;

// Format a U256 value truncating it to only 4 decimals if it's too large in attos.
pub fn truncated_balance_str(v: U256) -> String {
if v > U256::from(1_000_000u128) {
format!(
"{:.4}",
f64::from(v / U256::from(1_000_000_000_000u128)) / 1_000_000.0
)
} else {
format!("{v} attos")
}
}

// Shows an alert message in the UI (currently as an error).
// TODO: allow to provide the type of alert, i.e. info, warning, etc.
pub fn show_alert_msg(msg: String) {
Expand Down
10 changes: 6 additions & 4 deletions src/nodes_list_view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
app::{BatchInProgress, ClientGlobalState},
chart_view::{node_metrics_update, ChartSeriesData, NodeChartView},
helpers::{node_logs_stream, show_alert_msg},
helpers::{node_logs_stream, show_alert_msg, truncated_balance_str},
icons::{
IconCancel, IconRecycle, IconRemove, IconShowChart, IconShowLogs, IconStartNode,
IconStopNode, IconUpgradeNode,
Expand Down Expand Up @@ -331,13 +331,15 @@ fn NodeInstanceView(
</p>
<p>
<div class="flex flex-row">
<div class="basis-1/2">
<div class="basis-2/3">
<span class="node-info-item">"Balance: "</span>
{move || {
info.read().balance.map_or(" -".to_string(), |v| v.to_string())
info.read()
.balance
.map_or(" -".to_string(), |v| truncated_balance_str(v))
}}
</div>
<div class="basis-1/2">
<div class="basis-1/3">
<span class="node-info-item">"Rewards: "</span>
{move || {
info.read().rewards.map_or(" -".to_string(), |v| v.to_string())
Expand Down
16 changes: 11 additions & 5 deletions src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::app::ClientGlobalState;
use super::{app::ClientGlobalState, helpers::truncated_balance_str};

use alloy::primitives::U256;
use alloy::primitives::{utils::format_units, U256};
use leptos::prelude::*;
use std::collections::HashMap;

Expand All @@ -27,7 +27,7 @@ pub fn AggregatedStatsView() -> impl IntoView {
.filter(|n| n.read().status.is_inactive())
.count()
};
let balance = move || {
let total_balance = move || {
let mut total = U256::ZERO;
let seen = context
.nodes
Expand All @@ -48,8 +48,9 @@ pub fn AggregatedStatsView() -> impl IntoView {
total += balance;
}

total.to_string()
total
};

let connected_peers = move || {
context
.nodes
Expand Down Expand Up @@ -144,7 +145,12 @@ pub fn AggregatedStatsView() -> impl IntoView {
<div class="stats flex">
<div class="stat place-items-center">
<div class="stat-title">Current total balance</div>
<div class="stat-value text-primary">{balance}</div>
<div class="stat-value text-primary">
{move || truncated_balance_str(total_balance())}
</div>
<div class="stat-desc text-secondary">
{move || format_units(total_balance(), "ether").unwrap_or_default()}
</div>
</div>

<div class="stat place-items-center">
Expand Down

0 comments on commit b0f6fb8

Please sign in to comment.