-
Notifications
You must be signed in to change notification settings - Fork 3
Handle low faucet balance in claim token API #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,6 +219,15 @@ pub async fn claim_token( | |
.map_err(ServerFnError::new)?; | ||
|
||
SendWrapper::new(async move { | ||
let faucet_balance = rpc | ||
.wallet_balance(from, &faucet_info.token_type()) | ||
.await | ||
.map_err(ServerFnError::new)?; | ||
if faucet_balance < *faucet_info.drip_amount() { | ||
return Err(ServerFnError::ServerError( | ||
"Faucet is empty, Request top-up".to_string(), | ||
)); | ||
} | ||
Comment on lines
+222
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainMissing gas balance check for ERC-20 faucets and TOCTOU consideration. Two concerns with the balance pre-check:
Recommended fix for the major issue: For ERC-20 faucets, add an additional balance check for native tokens needed for gas: let faucet_balance = rpc
.wallet_balance(from, &faucet_info.token_type())
.await
.map_err(ServerFnError::new)?;
if faucet_balance < *faucet_info.drip_amount() {
return Err(ServerFnError::ServerError(
"Faucet is empty, Request top-up".to_string(),
));
}
+
+// For ERC-20 faucets, also check native token balance for gas
+if matches!(faucet_info.token_type(), TokenType::Erc20(_)) {
+ let native_balance = rpc
+ .wallet_balance(from, &TokenType::Native)
+ .await
+ .map_err(ServerFnError::new)?;
+ // Rough estimate: gas_limit * max_gas_price
+ let estimated_gas_cost = TokenAmount::from_atto(
+ faucet_info.max_gas_limit() as u128 * 2_000_000_000u128 // 2 Gwei estimate
+ );
+ if native_balance < estimated_gas_cost {
+ return Err(ServerFnError::ServerError(
+ "Faucet has insufficient native tokens for gas fees, Request top-up".to_string(),
+ ));
+ }
+}
+
match faucet_info { For the TOCTOU concern: The fail-fast approach is valuable for UX despite the race window. If you want to minimize on-chain failures and gas waste, consider implementing advisory locking (e.g., using Redis with SETNX or a database row lock) around the balance check and transfer operation. However, given the limited impact and the complexity of distributed locking, the current approach may be acceptable. Please verify whether on-chain transaction failures due to insufficient balance are an acceptable trade-off. 🏁 Script executed: #!/bin/bash
# Find the definition of wallet_balance
rg -n 'fn wallet_balance' -C3 --type rust
# Find the TokenType enum definition
rg -n 'enum TokenType' -C5 --type rust
# Show lines 200–260 in server_api.rs around the balance check
sed -n '200,260p' src/faucet/server_api.rs Length of output: 4119 🏁 Script executed: #!/bin/bash
# Find the ERC-20 claim handler
rg -n 'handle_erc20_claim' -C5 --type rust
# Find the native claim handler
rg -n 'handle_native_claim' -C5 --type rust
# Find the FaucetInfo enum definition
rg -n 'enum FaucetInfo' -C5 --type rust Length of output: 3174 🏁 Script executed: #!/bin/bash
# Dump handle_erc20_claim function body
sed -n '312,360p' src/faucet/server_api.rs
# Dump FaucetInfo impl block to find methods
rg -n 'impl FaucetInfo' -C5 --type rust
# Search for drip_amount and token_type methods
rg -n 'fn drip_amount' -C3 --type rust
rg -n 'fn token_type' -C3 --type rust Length of output: 3440 Check native FIL balance before ERC-20 transfers and mitigate TOCTOU race
🤖 Prompt for AI Agents
|
||
match faucet_info { | ||
FaucetInfo::MainnetFIL => { | ||
set_response_status(StatusCode::IM_A_TEAPOT); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve grammar and link text descriptiveness.
Two minor issues in this documentation section:
Apply this diff:
Based on learnings
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
157-157: Link text should be descriptive
(MD059, descriptive-link-text)
🤖 Prompt for AI Agents