Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub static MAINNET_DRIP_AMOUNT: LazyLock<TokenAmount> =
/// The amount of calibnet tFIL to be dripped to the user.
pub static CALIBNET_DRIP_AMOUNT: LazyLock<TokenAmount> =
LazyLock::new(|| TokenAmount::from_whole(1));
pub static FIL_MAINNET_UNIT: &str = "FIL";
pub static FIL_CALIBNET_UNIT: &str = "tFIL";
9 changes: 9 additions & 0 deletions src/faucet/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl FaucetController {
},
Some(TokenAmount::from_atto(0)),
);

let faucet = FaucetModel {
network,
send_disabled: create_rw_signal(false),
Expand Down Expand Up @@ -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);
}
Expand Down
29 changes: 28 additions & 1 deletion src/faucet/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -113,3 +113,30 @@ pub async fn query_rate_limiter() -> Result<bool, ServerFnError> {
.json::<bool>()
.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::<f32>().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);
}
}
}
9 changes: 5 additions & 4 deletions src/faucet/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -117,11 +118,11 @@ pub fn Faucet(target_network: Network) -> impl IntoView {
<div class="flex justify-between my-4">
<div>
<h3 class="text-lg font-semibold">Faucet Balance:</h3>
<p class="text-xl">{move || faucet.get().get_faucet_balance().to_string()}</p>
<p class="text-xl">{ move || format_balance(&faucet.get().get_faucet_balance(), &faucet.get().get_fil_unit()) }</p>
</div>
<div>
<h3 class="text-lg font-semibold">Target Balance:</h3>
<p class="text-xl">{move || faucet.get().get_target_balance().to_string()}</p>
<p class="text-xl">{ move || format_balance(&faucet.get().get_target_balance(), &faucet.get().get_fil_unit()) }</p>
</div>
</div>
<hr class="my-4 border-t border-gray-300" />
Expand Down Expand Up @@ -174,7 +175,7 @@ pub fn Faucet_Calibnet() -> impl IntoView {
<Faucet target_network=Network::Testnet />
</div>
<div class="text-center mt-4">
"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."
</div>
}
}
Expand All @@ -186,7 +187,7 @@ pub fn Faucet_Mainnet() -> impl IntoView {
<h1 class="text-4xl font-bold mb-6 text-center">Mainnet Faucet</h1>
<Faucet target_network=Network::Mainnet />
<div class="text-center mt-4">
"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."
</div>
</div>
}
Expand Down
Loading