An account's recent transactions grouped by day: what each did in plain words, when, and whether it is pending or failed, with a link to the block explorer. Tabs filter by kind.
Loading GPUI…
use gpui::{div, prelude::*, px, App, Context, FontWeight, Window};
use crate::icons::{icon, ARROW_DOWN, ARROW_UP_DOWN, ARROW_UP_RIGHT, CHECK};
use crate::theme::ActiveTheme;
use crate::ui::badge::{Badge, BadgeVariant};
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::onchain::{format_amount, truncate_middle};
use crate::ui::scroll_area::ScrollArea;
use crate::ui::tabs::Tabs;
use crate::ui::toast::{toast, Toast, Toaster};
use crate::ui::tooltip::Tooltip;
#[derive(Clone, Copy, PartialEq)]
enum Kind {
Swap,
Send,
Receive,
Approve,
}
#[derive(Clone, Copy, PartialEq)]
enum Status {
Pending,
Confirmed,
Failed,
}
struct Tx {
day: &'static str,
time: &'static str,
kind: Kind,
/// Amounts in base units with their decimals and symbol, formatted at render.
amount: (u128, u8, &'static str),
other: Option<(u128, u8, &'static str)>,
/// The counterparty, a fake address.
counterparty: &'static str,
/// A fake transaction hash.
hash: &'static str,
status: Status,
}
/// Sample history with fake counterparties and patterned, obviously fake hashes.
const HISTORY: [Tx; 7] = [
Tx { day: "Today", time: "14:32", kind: Kind::Swap, amount: (1_000_000_000_000_000_000, 18, "ETH"), other: Some((2_991_000_000, 6, "USDC")), counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007001", status: Status::Pending },
Tx { day: "Today", time: "11:05", kind: Kind::Send, amount: (250_000_000, 6, "USDC"), other: None, counterparty: "0x0000000000000000000000000000000000000B0b", hash: "0x0000000000000000000000000000000000000000000000000000000000007002", status: Status::Confirmed },
Tx { day: "Today", time: "09:47", kind: Kind::Swap, amount: (2_000_000_000_000_000_000, 18, "ETH"), other: Some((5_900_000_000, 6, "USDC")), counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007003", status: Status::Failed },
Tx { day: "Yesterday", time: "22:18", kind: Kind::Approve, amount: (500_000_000, 6, "USDC"), other: None, counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007004", status: Status::Confirmed },
Tx { day: "Yesterday", time: "18:02", kind: Kind::Receive, amount: (5_000_000, 8, "WBTC"), other: None, counterparty: "0x00000000000000000000000000000000000BA5E0", hash: "0x0000000000000000000000000000000000000000000000000000000000007005", status: Status::Confirmed },
Tx { day: "Nov 28", time: "08:40", kind: Kind::Receive, amount: (1_500_000_000_000_000_000, 18, "ETH"), other: None, counterparty: "0x00000000000000000000000000000000000BA5E0", hash: "0x0000000000000000000000000000000000000000000000000000000000007006", status: Status::Confirmed },
Tx { day: "Nov 28", time: "08:12", kind: Kind::Send, amount: (100_000_000_000_000_000_000, 18, "DAI"), other: None, counterparty: "0x0000000000000000000000000000000000000B0b", hash: "0x0000000000000000000000000000000000000000000000000000000000007007", status: Status::Confirmed },
];
const FILTERS: [&str; 3] = ["All", "Swaps", "Transfers"];
#[derive(Default)]
pub struct TransactionHistoryPattern {
filter: usize,
}
/// The host opens its block explorer at the full hash. The demo has no explorer, so it says which
/// transaction it would open.
fn open_explorer(hash: &'static str, cx: &mut App) {
toast(Toast::new("Open in explorer").description(hash), cx);
}
fn amount((units, decimals, symbol): (u128, u8, &str)) -> String {
format!("{} {symbol}", format_amount(units, decimals, 4))
}
impl Render for TransactionHistoryPattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme().clone();
let shown: Vec<(usize, &Tx)> = HISTORY
.iter()
.enumerate()
.filter(|(_, tx)| match self.filter {
1 => tx.kind == Kind::Swap,
2 => matches!(tx.kind, Kind::Send | Kind::Receive),
_ => true,
})
.collect();
let mut list = div().flex().flex_col().pr_3();
let mut last_day = "";
for (ix, tx) in shown {
if tx.day != last_day {
last_day = tx.day;
list = list.child(
div()
.pt_3()
.pb_1()
.text_xs()
.font_weight(FontWeight::MEDIUM)
.text_color(theme.muted_foreground)
.child(tx.day),
);
}
let (glyph, title, detail) = match tx.kind {
Kind::Swap => (
ARROW_UP_DOWN,
format!("Swap {}", amount(tx.amount)),
tx.other.map_or(String::new(), |other| format!("for {}", amount(other))),
),
Kind::Send => (ARROW_UP_RIGHT, format!("Send {}", amount(tx.amount)), format!("to {}", truncate_middle(tx.counterparty, 6, 4))),
Kind::Receive => (ARROW_DOWN, format!("Receive {}", amount(tx.amount)), format!("from {}", truncate_middle(tx.counterparty, 6, 4))),
Kind::Approve => (CHECK, format!("Approve {}", amount(tx.amount)), format!("for {}", truncate_middle(tx.counterparty, 6, 4))),
};
let status = match tx.status {
Status::Pending => Some(Badge::new("Pending").variant(BadgeVariant::Outline)),
Status::Failed => Some(Badge::new("Failed").variant(BadgeVariant::Destructive)),
Status::Confirmed => None,
};
list = list.child(
div()
.flex()
.items_center()
.gap_3()
.py_2()
.child(
div()
.flex_none()
.size(px(32.0))
.rounded_full()
.bg(theme.muted)
.flex()
.items_center()
.justify_center()
.child(icon(glyph).size(px(14.0)).text_color(theme.foreground)),
)
.child(
div()
.flex_1()
.min_w_0()
.flex()
.flex_col()
.child(
div()
.flex()
.items_center()
.gap_2()
.child(
div()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.truncate()
.when(tx.status == Status::Failed, |this| {
this.text_color(theme.muted_foreground).line_through()
})
.child(title),
)
.children(status),
)
.child(
div()
.text_xs()
.text_color(theme.muted_foreground)
.truncate()
.child(format!("{} \u{00b7} {detail}", tx.time)),
),
)
// The host opens its explorer at the transaction's hash.
.child(
Tooltip::new(("explorer-tip", ix), "View on explorer").trigger(
Button::new(("explorer", ix), "")
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon_only()
.leading(icon(ARROW_UP_RIGHT).size(px(14.0)).text_color(theme.muted_foreground))
.on_click({ let hash = tx.hash; move |_, _, cx| open_explorer(hash, cx) }),
),
),
);
}
div()
.w(px(440.0))
.flex()
.flex_col()
.gap_2()
.p_4()
.rounded(theme.radius * 1.5)
.border_1()
.border_color(theme.border)
.bg(theme.card)
.child(
div()
.flex()
.items_center()
.justify_between()
.child(
div()
.flex()
.flex_col()
.child(div().text_base().font_weight(FontWeight::SEMIBOLD).child("Activity"))
.child(div().text_xs().text_color(theme.muted_foreground).child("Sample transactions")),
)
.child(FILTERS.iter().fold(
Tabs::new("history-filter", self.filter).on_select(cx.listener(|this, ix: &usize, _, cx| {
this.filter = *ix;
cx.notify();
})),
|tabs, label| tabs.tab(*label),
)),
)
.child(ScrollArea::new("history").h(px(380.0)).child(list))
.child(Toaster::new())
}
}Installation
Add the components
$ cargo hyuskui add badge button onchain scroll-area tabs toast tooltip iconsUses Badge, Button, Onchain, Scroll Area, Tabs, Toast and Tooltip.
Add 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/transaction_history.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/transaction_history.rs use gpui::{div, prelude::*, px, App, Context, FontWeight, Window}; use crate::icons::{icon, ARROW_DOWN, ARROW_UP_DOWN, ARROW_UP_RIGHT, CHECK}; use crate::theme::ActiveTheme; use crate::ui::badge::{Badge, BadgeVariant}; use crate::ui::button::{Button, ButtonSize, ButtonVariant}; use crate::ui::onchain::{format_amount, truncate_middle}; use crate::ui::scroll_area::ScrollArea; use crate::ui::tabs::Tabs; use crate::ui::toast::{toast, Toast, Toaster}; use crate::ui::tooltip::Tooltip; #[derive(Clone, Copy, PartialEq)] enum Kind { Swap, Send, Receive, Approve, } #[derive(Clone, Copy, PartialEq)] enum Status { Pending, Confirmed, Failed, } struct Tx { day: &'static str, time: &'static str, kind: Kind, /// Amounts in base units with their decimals and symbol, formatted at render. amount: (u128, u8, &'static str), other: Option<(u128, u8, &'static str)>, /// The counterparty, a fake address. counterparty: &'static str, /// A fake transaction hash. hash: &'static str, status: Status, } /// Sample history with fake counterparties and patterned, obviously fake hashes. const HISTORY: [Tx; 7] = [ Tx { day: "Today", time: "14:32", kind: Kind::Swap, amount: (1_000_000_000_000_000_000, 18, "ETH"), other: Some((2_991_000_000, 6, "USDC")), counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007001", status: Status::Pending }, Tx { day: "Today", time: "11:05", kind: Kind::Send, amount: (250_000_000, 6, "USDC"), other: None, counterparty: "0x0000000000000000000000000000000000000B0b", hash: "0x0000000000000000000000000000000000000000000000000000000000007002", status: Status::Confirmed }, Tx { day: "Today", time: "09:47", kind: Kind::Swap, amount: (2_000_000_000_000_000_000, 18, "ETH"), other: Some((5_900_000_000, 6, "USDC")), counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007003", status: Status::Failed }, Tx { day: "Yesterday", time: "22:18", kind: Kind::Approve, amount: (500_000_000, 6, "USDC"), other: None, counterparty: "0x000000000000000000000000000000000000c0DE", hash: "0x0000000000000000000000000000000000000000000000000000000000007004", status: Status::Confirmed }, Tx { day: "Yesterday", time: "18:02", kind: Kind::Receive, amount: (5_000_000, 8, "WBTC"), other: None, counterparty: "0x00000000000000000000000000000000000BA5E0", hash: "0x0000000000000000000000000000000000000000000000000000000000007005", status: Status::Confirmed }, Tx { day: "Nov 28", time: "08:40", kind: Kind::Receive, amount: (1_500_000_000_000_000_000, 18, "ETH"), other: None, counterparty: "0x00000000000000000000000000000000000BA5E0", hash: "0x0000000000000000000000000000000000000000000000000000000000007006", status: Status::Confirmed }, Tx { day: "Nov 28", time: "08:12", kind: Kind::Send, amount: (100_000_000_000_000_000_000, 18, "DAI"), other: None, counterparty: "0x0000000000000000000000000000000000000B0b", hash: "0x0000000000000000000000000000000000000000000000000000000000007007", status: Status::Confirmed }, ]; const FILTERS: [&str; 3] = ["All", "Swaps", "Transfers"]; #[derive(Default)] pub struct TransactionHistoryPattern { filter: usize, } /// The host opens its block explorer at the full hash. The demo has no explorer, so it says which /// transaction it would open. fn open_explorer(hash: &'static str, cx: &mut App) { toast(Toast::new("Open in explorer").description(hash), cx); } fn amount((units, decimals, symbol): (u128, u8, &str)) -> String { format!("{} {symbol}", format_amount(units, decimals, 4)) } impl Render for TransactionHistoryPattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme().clone(); let shown: Vec<(usize, &Tx)> = HISTORY .iter() .enumerate() .filter(|(_, tx)| match self.filter { 1 => tx.kind == Kind::Swap, 2 => matches!(tx.kind, Kind::Send | Kind::Receive), _ => true, }) .collect(); let mut list = div().flex().flex_col().pr_3(); let mut last_day = ""; for (ix, tx) in shown { if tx.day != last_day { last_day = tx.day; list = list.child( div() .pt_3() .pb_1() .text_xs() .font_weight(FontWeight::MEDIUM) .text_color(theme.muted_foreground) .child(tx.day), ); } let (glyph, title, detail) = match tx.kind { Kind::Swap => ( ARROW_UP_DOWN, format!("Swap {}", amount(tx.amount)), tx.other.map_or(String::new(), |other| format!("for {}", amount(other))), ), Kind::Send => (ARROW_UP_RIGHT, format!("Send {}", amount(tx.amount)), format!("to {}", truncate_middle(tx.counterparty, 6, 4))), Kind::Receive => (ARROW_DOWN, format!("Receive {}", amount(tx.amount)), format!("from {}", truncate_middle(tx.counterparty, 6, 4))), Kind::Approve => (CHECK, format!("Approve {}", amount(tx.amount)), format!("for {}", truncate_middle(tx.counterparty, 6, 4))), }; let status = match tx.status { Status::Pending => Some(Badge::new("Pending").variant(BadgeVariant::Outline)), Status::Failed => Some(Badge::new("Failed").variant(BadgeVariant::Destructive)), Status::Confirmed => None, }; list = list.child( div() .flex() .items_center() .gap_3() .py_2() .child( div() .flex_none() .size(px(32.0)) .rounded_full() .bg(theme.muted) .flex() .items_center() .justify_center() .child(icon(glyph).size(px(14.0)).text_color(theme.foreground)), ) .child( div() .flex_1() .min_w_0() .flex() .flex_col() .child( div() .flex() .items_center() .gap_2() .child( div() .text_sm() .font_weight(FontWeight::MEDIUM) .truncate() .when(tx.status == Status::Failed, |this| { this.text_color(theme.muted_foreground).line_through() }) .child(title), ) .children(status), ) .child( div() .text_xs() .text_color(theme.muted_foreground) .truncate() .child(format!("{} \u{00b7} {detail}", tx.time)), ), ) // The host opens its explorer at the transaction's hash. .child( Tooltip::new(("explorer-tip", ix), "View on explorer").trigger( Button::new(("explorer", ix), "") .variant(ButtonVariant::Ghost) .size(ButtonSize::Sm) .icon_only() .leading(icon(ARROW_UP_RIGHT).size(px(14.0)).text_color(theme.muted_foreground)) .on_click({ let hash = tx.hash; move |_, _, cx| open_explorer(hash, cx) }), ), ), ); } div() .w(px(440.0)) .flex() .flex_col() .gap_2() .p_4() .rounded(theme.radius * 1.5) .border_1() .border_color(theme.border) .bg(theme.card) .child( div() .flex() .items_center() .justify_between() .child( div() .flex() .flex_col() .child(div().text_base().font_weight(FontWeight::SEMIBOLD).child("Activity")) .child(div().text_xs().text_color(theme.muted_foreground).child("Sample transactions")), ) .child(FILTERS.iter().fold( Tabs::new("history-filter", self.filter).on_select(cx.listener(|this, ix: &usize, _, cx| { this.filter = *ix; cx.notify(); })), |tabs, label| tabs.tab(*label), )), ) .child(ScrollArea::new("history").h(px(380.0)).child(list)) .child(Toaster::new()) } }