diff --git a/src/constants.rs b/src/constants.rs index f59fda79..9f32cadd 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -11,3 +11,5 @@ pub static MAINNET_DRIP_AMOUNT: LazyLock = /// The amount of calibnet tFIL to be dripped to the user. pub static CALIBNET_DRIP_AMOUNT: LazyLock = LazyLock::new(|| TokenAmount::from_whole(1)); +pub static FIL_MAINNET_UNIT: &str = "FIL"; +pub static FIL_CALIBNET_UNIT: &str = "tFIL"; diff --git a/src/faucet/controller.rs b/src/faucet/controller.rs index 58b9b03d..ef266298 100644 --- a/src/faucet/controller.rs +++ b/src/faucet/controller.rs @@ -61,6 +61,7 @@ impl FaucetController { }, Some(TokenAmount::from_atto(0)), ); + let faucet = FaucetModel { network, send_disabled: create_rw_signal(false), @@ -117,6 +118,14 @@ impl FaucetController { self.faucet.target_address.get() } + pub fn get_fil_unit(&self) -> String { + match self.faucet.network { + Network::Mainnet => crate::constants::FIL_MAINNET_UNIT, + _ => crate::constants::FIL_CALIBNET_UNIT, + } + .to_string() + } + pub fn set_target_address(&self, address: String) { self.faucet.target_address.set(address); } diff --git a/src/faucet/utils.rs b/src/faucet/utils.rs index c92e955e..1cafe09c 100644 --- a/src/faucet/utils.rs +++ b/src/faucet/utils.rs @@ -3,7 +3,7 @@ use crate::key::{sign, Key}; use crate::{lotus_json::LotusJson, message::SignedMessage}; #[cfg(feature = "ssr")] use fvm_shared::address::Network; -use fvm_shared::{address::Address, message::Message}; +use fvm_shared::{address::Address, econ::TokenAmount, message::Message}; use leptos::{server, ServerFnError}; #[server] @@ -113,3 +113,30 @@ pub async fn query_rate_limiter() -> Result { .json::() .await?) } + +/// Formats FIL balance to a human-readable string with two decimal places and a unit. +pub fn format_balance(balance: &TokenAmount, unit: &str) -> String { + format!( + "{:.2} {unit}", + balance.to_string().parse::().unwrap_or_default(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use fvm_shared::econ::TokenAmount; + + #[test] + fn test_format_balance() { + let cases = [ + (TokenAmount::from_whole(1), "1.00 FIL"), + (TokenAmount::from_whole(0), "0.00 FIL"), + (TokenAmount::from_nano(10e6 as i64), "0.01 FIL"), + (TokenAmount::from_nano(999_999_999), "1.00 FIL"), + ]; + for (balance, expected) in cases.iter() { + assert_eq!(format_balance(balance, "FIL"), *expected); + } + } +} diff --git a/src/faucet/views.rs b/src/faucet/views.rs index 6d24cd9b..e9aca5db 100644 --- a/src/faucet/views.rs +++ b/src/faucet/views.rs @@ -6,6 +6,7 @@ use leptos::*; use leptos_use::*; use crate::faucet::controller::FaucetController; +use crate::faucet::utils::format_balance; #[component] pub fn Faucet(target_network: Network) -> impl IntoView { @@ -117,11 +118,11 @@ pub fn Faucet(target_network: Network) -> impl IntoView {

Faucet Balance:

-

{move || faucet.get().get_faucet_balance().to_string()}

+

{ move || format_balance(&faucet.get().get_faucet_balance(), &faucet.get().get_fil_unit()) }

Target Balance:

-

{move || faucet.get().get_target_balance().to_string()}

+

{ move || format_balance(&faucet.get().get_target_balance(), &faucet.get().get_fil_unit()) }


@@ -174,7 +175,7 @@ pub fn Faucet_Calibnet() -> impl IntoView {
- "This faucet distributes 1 tFIL per request. It is rate-limited to 1 request per " {crate::constants::RATE_LIMIT_SECONDS} " seconds. Farming is discouraged and will result in more stringent rate limiting in the future and/or permanent bans." + "This faucet distributes " { format_balance(&crate::constants::CALIBNET_DRIP_AMOUNT, crate::constants::FIL_CALIBNET_UNIT) } " per request. It is rate-limited to 1 request per " {crate::constants::RATE_LIMIT_SECONDS} " seconds. Farming is discouraged and will result in more stringent rate limiting in the future and/or permanent bans."
} } @@ -186,7 +187,7 @@ pub fn Faucet_Mainnet() -> impl IntoView {

Mainnet Faucet

- "This faucet distributes 0.01 FIL per request. It is rate-limited to 1 request per " {crate::constants::RATE_LIMIT_SECONDS} " seconds. Farming is discouraged and will result in more stringent rate limiting in the future and/or permanent bans or service termination. Faucet funds are limited and may run out. They are replenished periodically." + "This faucet distributes " { format_balance(&crate::constants::MAINNET_DRIP_AMOUNT, crate::constants::FIL_MAINNET_UNIT) } " per request. It is rate-limited to 1 request per " {crate::constants::RATE_LIMIT_SECONDS} " seconds. Farming is discouraged and will result in more stringent rate limiting in the future and/or permanent bans or service termination. Faucet funds are limited and may run out. They are replenished periodically."
}