The network fee for a transaction at three speeds: fee per gas in gwei, the total in ETH and USD, and the expected wait. Fees are gas units times wei per gas, all integers.
Loading GPUI…
use gpui::{div, prelude::*, px, Context, FontWeight, Window};
use crate::icons::{icon, FUEL};
use crate::theme::ActiveTheme;
use crate::ui::badge::{Badge, BadgeVariant};
use crate::ui::card::Card;
use crate::ui::onchain::{format_amount, format_usd_cents, usd_cents};
/// Gwei has 9 decimals: 1 gwei is 10^9 wei.
const GWEI_DECIMALS: u8 = 9;
/// The sample transaction: a token swap's gas limit.
const GAS_LIMIT: u128 = 184_000;
/// Sample ETH price, USD with 8 decimals.
const ETH_PRICE_E8: u128 = 300_000_000_000;
struct Speed {
name: &'static str,
wait: &'static str,
/// Max fee per gas in wei: base fee plus priority tip.
max_fee_wei: u128,
}
/// Sample fees from a quiet network: base fee about 0.4 gwei.
const SPEEDS: [Speed; 3] = [
Speed { name: "Slow", wait: "~1 min", max_fee_wei: 410_000_000 },
Speed { name: "Standard", wait: "~24 sec", max_fee_wei: 520_000_000 },
Speed { name: "Fast", wait: "~12 sec", max_fee_wei: 1_150_000_000 },
];
pub struct GasEstimatePattern {
speed: usize,
}
impl Default for GasEstimatePattern {
fn default() -> Self {
Self { speed: 1 }
}
}
impl Render for GasEstimatePattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme().clone();
// `None` if the product doesn't fit, shown as unavailable rather than wrapped or clamped.
let fee_wei = |speed: &Speed| GAS_LIMIT.checked_mul(speed.max_fee_wei);
let usd = |wei: Option<u128>| {
wei.and_then(|wei| usd_cents(wei, 18, ETH_PRICE_E8)).map_or("Unavailable".into(), format_usd_cents)
};
let options = SPEEDS.iter().enumerate().map(|(ix, speed)| {
let selected = ix == self.speed;
let wei = fee_wei(speed);
div()
.id(("speed", ix))
.flex_1()
.flex()
.flex_col()
.gap_1()
.p_3()
.rounded(theme.radius)
.border_1()
.border_color(if selected { theme.foreground } else { theme.border })
.when(selected, |this| this.bg(theme.accent))
.cursor_pointer()
.hover(|style| style.bg(theme.accent))
.on_click(cx.listener(move |this, _, _, cx| {
this.speed = ix;
cx.notify();
}))
.child(div().text_sm().font_weight(FontWeight::MEDIUM).child(speed.name))
.child(div().text_lg().font_weight(FontWeight::SEMIBOLD).child(usd(wei)))
.child(div().text_xs().text_color(theme.muted_foreground).child(format!(
"{} gwei \u{00b7} {}",
format_amount(speed.max_fee_wei, GWEI_DECIMALS, 2),
speed.wait
)))
});
let speed = &SPEEDS[self.speed];
let wei = fee_wei(speed);
let line = |label: &'static str, value: String| {
div()
.flex()
.justify_between()
.text_sm()
.child(div().text_color(theme.muted_foreground).child(label))
.child(value)
};
div().w(px(440.0)).child(
Card::new()
.title("Network fee")
.description("Paid to the network, not to this app. Sample estimate.")
.child(
div()
.flex()
.flex_col()
.gap_4()
.child(div().flex().gap_2().children(options))
.child(
div()
.flex()
.flex_col()
.gap_2()
.child(line("Gas limit", format_amount(GAS_LIMIT, 0, 0)))
.child(line(
"Max fee per gas",
format!("{} gwei", format_amount(speed.max_fee_wei, GWEI_DECIMALS, 2)),
))
.child(
div()
.flex()
.items_center()
.justify_between()
.pt_2()
.border_t_1()
.border_color(theme.border)
.child(
div()
.flex()
.items_center()
.gap_2()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.child(icon(FUEL).size(px(14.0)).text_color(theme.foreground))
.child("Max network fee"),
)
.child(
div()
.flex()
.items_center()
.gap_2()
.child(
Badge::new(wei.map_or("Unavailable".into(), |wei| {
format!("{} ETH", format_amount(wei, 18, 6))
}))
.variant(BadgeVariant::Secondary),
)
.child(
div()
.text_sm()
.font_weight(FontWeight::MEDIUM)
.child(usd(wei)),
),
),
),
),
),
)
}
}Installation
Add the components
$ cargo hyuskui add badge card onchain 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/gas_estimate.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/gas_estimate.rs use gpui::{div, prelude::*, px, Context, FontWeight, Window}; use crate::icons::{icon, FUEL}; use crate::theme::ActiveTheme; use crate::ui::badge::{Badge, BadgeVariant}; use crate::ui::card::Card; use crate::ui::onchain::{format_amount, format_usd_cents, usd_cents}; /// Gwei has 9 decimals: 1 gwei is 10^9 wei. const GWEI_DECIMALS: u8 = 9; /// The sample transaction: a token swap's gas limit. const GAS_LIMIT: u128 = 184_000; /// Sample ETH price, USD with 8 decimals. const ETH_PRICE_E8: u128 = 300_000_000_000; struct Speed { name: &'static str, wait: &'static str, /// Max fee per gas in wei: base fee plus priority tip. max_fee_wei: u128, } /// Sample fees from a quiet network: base fee about 0.4 gwei. const SPEEDS: [Speed; 3] = [ Speed { name: "Slow", wait: "~1 min", max_fee_wei: 410_000_000 }, Speed { name: "Standard", wait: "~24 sec", max_fee_wei: 520_000_000 }, Speed { name: "Fast", wait: "~12 sec", max_fee_wei: 1_150_000_000 }, ]; pub struct GasEstimatePattern { speed: usize, } impl Default for GasEstimatePattern { fn default() -> Self { Self { speed: 1 } } } impl Render for GasEstimatePattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { let theme = cx.theme().clone(); // `None` if the product doesn't fit, shown as unavailable rather than wrapped or clamped. let fee_wei = |speed: &Speed| GAS_LIMIT.checked_mul(speed.max_fee_wei); let usd = |wei: Option<u128>| { wei.and_then(|wei| usd_cents(wei, 18, ETH_PRICE_E8)).map_or("Unavailable".into(), format_usd_cents) }; let options = SPEEDS.iter().enumerate().map(|(ix, speed)| { let selected = ix == self.speed; let wei = fee_wei(speed); div() .id(("speed", ix)) .flex_1() .flex() .flex_col() .gap_1() .p_3() .rounded(theme.radius) .border_1() .border_color(if selected { theme.foreground } else { theme.border }) .when(selected, |this| this.bg(theme.accent)) .cursor_pointer() .hover(|style| style.bg(theme.accent)) .on_click(cx.listener(move |this, _, _, cx| { this.speed = ix; cx.notify(); })) .child(div().text_sm().font_weight(FontWeight::MEDIUM).child(speed.name)) .child(div().text_lg().font_weight(FontWeight::SEMIBOLD).child(usd(wei))) .child(div().text_xs().text_color(theme.muted_foreground).child(format!( "{} gwei \u{00b7} {}", format_amount(speed.max_fee_wei, GWEI_DECIMALS, 2), speed.wait ))) }); let speed = &SPEEDS[self.speed]; let wei = fee_wei(speed); let line = |label: &'static str, value: String| { div() .flex() .justify_between() .text_sm() .child(div().text_color(theme.muted_foreground).child(label)) .child(value) }; div().w(px(440.0)).child( Card::new() .title("Network fee") .description("Paid to the network, not to this app. Sample estimate.") .child( div() .flex() .flex_col() .gap_4() .child(div().flex().gap_2().children(options)) .child( div() .flex() .flex_col() .gap_2() .child(line("Gas limit", format_amount(GAS_LIMIT, 0, 0))) .child(line( "Max fee per gas", format!("{} gwei", format_amount(speed.max_fee_wei, GWEI_DECIMALS, 2)), )) .child( div() .flex() .items_center() .justify_between() .pt_2() .border_t_1() .border_color(theme.border) .child( div() .flex() .items_center() .gap_2() .text_sm() .font_weight(FontWeight::MEDIUM) .child(icon(FUEL).size(px(14.0)).text_color(theme.foreground)) .child("Max network fee"), ) .child( div() .flex() .items_center() .gap_2() .child( Badge::new(wei.map_or("Unavailable".into(), |wei| { format!("{} ETH", format_amount(wei, 18, 6)) })) .variant(BadgeVariant::Secondary), ) .child( div() .text_sm() .font_weight(FontWeight::MEDIUM) .child(usd(wei)), ), ), ), ), ), ) } }