Token balances with their USD value and 24h change, largest first, under the account's total. Values are integer cents computed from base units and prices; small balances can be hidden.
Loading GPUI…
use gpui::{div, prelude::*, px, Context, FontWeight, SharedString, Window};
use crate::icons::{icon, TRENDING_DOWN, TRENDING_UP};
use crate::theme::ActiveTheme;
use crate::ui::avatar::{Avatar, AvatarSize};
use crate::ui::card::Card;
use crate::ui::onchain::{format_amount, format_bps, format_usd_cents, usd_cents};
use crate::ui::switch::Switch;
struct Holding {
symbol: &'static str,
name: &'static str,
decimals: u8,
balance: u128,
/// Sample USD price, 8 decimals.
price_e8: u128,
/// 24h change in bps; negative is down.
change_bps: i64,
}
/// Sample balances and prices.
const HOLDINGS: [Holding; 6] = [
Holding { symbol: "ETH", name: "Ether", decimals: 18, balance: 1_234_567_890_123_456_789, price_e8: 300_000_000_000, change_bps: 214 },
Holding { symbol: "USDC", name: "USD Coin", decimals: 6, balance: 2_500_120_000, price_e8: 100_000_000, change_bps: 1 },
Holding { symbol: "WBTC", name: "Wrapped Bitcoin", decimals: 8, balance: 4_210_337, price_e8: 6_000_000_000_000, change_bps: -87 },
Holding { symbol: "DAI", name: "Dai Stablecoin", decimals: 18, balance: 820_500_000_000_000_000_000, price_e8: 99_980_000, change_bps: -2 },
Holding { symbol: "SMPL", name: "Sample Governance", decimals: 18, balance: 120_000_000_000_000_000, price_e8: 4_200_000_000, change_bps: 512 },
Holding { symbol: "SMPD", name: "Sample Dust", decimals: 18, balance: 3_000_000_000_000_000, price_e8: 12_000_000, change_bps: -1_240 },
];
/// Balances worth less than this many cents hide when the switch is on.
const SMALL_CENTS: u128 = 100;
#[derive(Default)]
pub struct BalanceListPattern {
hide_small: bool,
}
impl Render for BalanceListPattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme().clone();
let mut rows: Vec<(&Holding, u128)> = HOLDINGS
.iter()
.map(|h| (h, usd_cents(h.balance, h.decimals, h.price_e8).unwrap_or(0)))
.collect();
rows.sort_by(|a, b| b.1.cmp(&a.1));
let total: u128 = rows.iter().map(|(_, cents)| cents).sum();
let hidden = rows.iter().filter(|(_, cents)| *cents < SMALL_CENTS).count();
if self.hide_small {
rows.retain(|(_, cents)| *cents >= SMALL_CENTS);
}
let list = rows.into_iter().map(|(h, cents)| {
let up = h.change_bps >= 0;
let change = format!("{}{}", if up { "+" } else { "\u{2212}" }, format_bps(h.change_bps.unsigned_abs() as u128));
div()
.flex()
.items_center()
.gap_3()
.py_2p5()
.child(
Avatar::new(SharedString::from(h.symbol[..1].to_string())).size(AvatarSize::Custom(px(32.0))),
)
.child(
div()
.flex_1()
.min_w_0()
.flex()
.flex_col()
.child(div().text_sm().font_weight(FontWeight::MEDIUM).child(h.name))
.child(
div()
.text_xs()
.text_color(theme.muted_foreground)
.child(format!("{} {}", format_amount(h.balance, h.decimals, 4), h.symbol)),
),
)
.child(
div()
.flex()
.flex_col()
.items_end()
// A non-zero balance worth under a cent shouldn't read as nothing.
.child(div().text_sm().font_weight(FontWeight::MEDIUM).child(if cents == 0 && h.balance > 0 {
"<$0.01".to_string()
} else {
format_usd_cents(cents)
}))
.child(
div()
.flex()
.items_center()
.gap_1()
.text_xs()
// No success token in the theme: gains stay foreground, losses go destructive.
.text_color(if up { theme.muted_foreground } else { theme.destructive })
.child(
icon(if up { TRENDING_UP } else { TRENDING_DOWN })
.size(px(12.0))
.text_color(if up { theme.muted_foreground } else { theme.destructive }),
)
.child(change),
),
)
});
div().w(px(400.0)).child(
Card::new()
.title("Balances")
.description("Sample account on Mainnet")
.child(
div()
.flex()
.flex_col()
.child(
div()
.text_3xl()
.font_weight(FontWeight::SEMIBOLD)
.pb_2()
.child(format_usd_cents(total)),
)
.child(div().flex().flex_col().children(list))
.child(div().h_px().my_2().bg(theme.border))
.child(
Switch::new("hide-small", self.hide_small)
.label(format!("Hide balances under $1 ({hidden})"))
.on_toggle(cx.listener(|this, on: &bool, _, cx| {
this.hide_small = *on;
cx.notify();
})),
),
),
)
}
}Installation
Add the components
$ cargo hyuskui add avatar card onchain switch iconsAdd the crates
$ cargo add sha3@0.10onchain.rshashes addresses with Keccak-256 for their EIP-55 checksum.cargo hyuskui addruns this for you; you need it only when copying the files by hand.Copy the pattern
Paste the code into
src/crypto/balance_list.rsand declare the module. The demo runs on sample data and fake addresses; feed it your wallet connection, balances and quotes, and send transactions from the handlers.src/crypto/balance_list.rs use gpui::{div, prelude::*, px, Context, FontWeight, SharedString, Window}; use crate::icons::{icon, TRENDING_DOWN, TRENDING_UP}; use crate::theme::ActiveTheme; use crate::ui::avatar::{Avatar, AvatarSize}; use crate::ui::card::Card; use crate::ui::onchain::{format_amount, format_bps, format_usd_cents, usd_cents}; use crate::ui::switch::Switch; struct Holding { symbol: &'static str, name: &'static str, decimals: u8, balance: u128, /// Sample USD price, 8 decimals. price_e8: u128, /// 24h change in bps; negative is down. change_bps: i64, } /// Sample balances and prices. const HOLDINGS: [Holding; 6] = [ Holding { symbol: "ETH", name: "Ether", decimals: 18, balance: 1_234_567_890_123_456_789, price_e8: 300_000_000_000, change_bps: 214 }, Holding { symbol: "USDC", name: "USD Coin", decimals: 6, balance: 2_500_120_000, price_e8: 100_000_000, change_bps: 1 }, Holding { symbol: "WBTC", name: "Wrapped Bitcoin", decimals: 8, balance: 4_210_337, price_e8: 6_000_000_000_000, change_bps: -87 }, Holding { symbol: "DAI", name: "Dai Stablecoin", decimals: 18, balance: 820_500_000_000_000_000_000, price_e8: 99_980_000, change_bps: -2 }, Holding { symbol: "SMPL", name: "Sample Governance", decimals: 18, balance: 120_000_000_000_000_000, price_e8: 4_200_000_000, change_bps: 512 }, Holding { symbol: "SMPD", name: "Sample Dust", decimals: 18, balance: 3_000_000_000_000_000, price_e8: 12_000_000, change_bps: -1_240 }, ]; /// Balances worth less than this many cents hide when the switch is on. const SMALL_CENTS: u128 = 100; #[derive(Default)] pub struct BalanceListPattern { hide_small: bool, } impl Render for BalanceListPattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme().clone(); let mut rows: Vec<(&Holding, u128)> = HOLDINGS .iter() .map(|h| (h, usd_cents(h.balance, h.decimals, h.price_e8).unwrap_or(0))) .collect(); rows.sort_by(|a, b| b.1.cmp(&a.1)); let total: u128 = rows.iter().map(|(_, cents)| cents).sum(); let hidden = rows.iter().filter(|(_, cents)| *cents < SMALL_CENTS).count(); if self.hide_small { rows.retain(|(_, cents)| *cents >= SMALL_CENTS); } let list = rows.into_iter().map(|(h, cents)| { let up = h.change_bps >= 0; let change = format!("{}{}", if up { "+" } else { "\u{2212}" }, format_bps(h.change_bps.unsigned_abs() as u128)); div() .flex() .items_center() .gap_3() .py_2p5() .child( Avatar::new(SharedString::from(h.symbol[..1].to_string())).size(AvatarSize::Custom(px(32.0))), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child(div().text_sm().font_weight(FontWeight::MEDIUM).child(h.name)) .child( div() .text_xs() .text_color(theme.muted_foreground) .child(format!("{} {}", format_amount(h.balance, h.decimals, 4), h.symbol)), ), ) .child( div() .flex() .flex_col() .items_end() // A non-zero balance worth under a cent shouldn't read as nothing. .child(div().text_sm().font_weight(FontWeight::MEDIUM).child(if cents == 0 && h.balance > 0 { "<$0.01".to_string() } else { format_usd_cents(cents) })) .child( div() .flex() .items_center() .gap_1() .text_xs() // No success token in the theme: gains stay foreground, losses go destructive. .text_color(if up { theme.muted_foreground } else { theme.destructive }) .child( icon(if up { TRENDING_UP } else { TRENDING_DOWN }) .size(px(12.0)) .text_color(if up { theme.muted_foreground } else { theme.destructive }), ) .child(change), ), ) }); div().w(px(400.0)).child( Card::new() .title("Balances") .description("Sample account on Mainnet") .child( div() .flex() .flex_col() .child( div() .text_3xl() .font_weight(FontWeight::SEMIBOLD) .pb_2() .child(format_usd_cents(total)), ) .child(div().flex().flex_col().children(list)) .child(div().h_px().my_2().bg(theme.border)) .child( Switch::new("hide-small", self.hide_small) .label(format!("Hide balances under $1 ({hidden})")) .on_toggle(cx.listener(|this, on: &bool, _, cx| { this.hide_small = *on; cx.notify(); })), ), ), ) } }