Skip to content

fix(ui): replace f64 round-trip with integer arithmetic in max amount calculations#619

Closed
thepastaclaw wants to merge 1 commit intodashpay:v1.0-devfrom
thepastaclaw:fix/f64-precision-max-amount
Closed

fix(ui): replace f64 round-trip with integer arithmetic in max amount calculations#619
thepastaclaw wants to merge 1 commit intodashpay:v1.0-devfrom
thepastaclaw:fix/f64-precision-max-amount

Conversation

@thepastaclaw
Copy link
Collaborator

@thepastaclaw thepastaclaw commented Feb 22, 2026

Issue

The "Max" button in transfer and withdraw screens uses a floating-point round-trip (u64→f64→u64) to subtract fee estimates from the available balance. This can cause precision loss with large credit amounts, potentially resulting in incorrect max values.

Changes

Replace the floating-point arithmetic with integer saturating_sub:

// Before (precision loss possible)
let max_amount_minus_fee = (self.max_amount as f64 / 100_000_000_000.0 - 0.0002).max(0.0);
let max_amount_credits = (max_amount_minus_fee * 100_000_000_000.0) as u64;

// After (exact integer arithmetic)
let max_amount_credits = self.max_amount.saturating_sub(20_000_000);
  • transfer_screen.rs: 0.0002 DASH = 20,000,000 credits
  • withdraw_screen.rs: 0.005 DASH = 500,000,000 credits

saturating_sub also handles the edge case where balance < fee (returns 0 instead of underflowing).

Validation

  • cargo check
  • cargo fmt

Summary by CodeRabbit

Release Notes

  • Refactor
    • Optimized internal calculation logic for transfer and withdrawal amount limits to improve performance and reliability.

… calculations

Replace floating-point round-trip (u64→f64→u64) with integer
saturating_sub for fee reservation in Max button calculations.
This eliminates precision loss that could cause incorrect max
amounts in transfer and withdraw screens.

- transfer_screen: 0.0002 DASH = 20_000_000 credits
- withdraw_screen: 0.005 DASH = 500_000_000 credits
@thepastaclaw thepastaclaw force-pushed the fix/f64-precision-max-amount branch from 7a28435 to 4569b4a Compare February 22, 2026 17:15
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 22, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Two UI screen components replace floating-point arithmetic calculations with saturating integer subtraction to determine maximum transferable and withdrawable amounts after fee deductions. This eliminates float-based operations while preserving external behavior.

Changes

Cohort / File(s) Summary
Transfer/Withdraw Max Amount Calculation
src/ui/identities/transfer_screen.rs, src/ui/identities/withdraw_screen.rs
Replaced floating-point fee calculation with direct saturating subtraction. Transfer screen subtracts 20,000,000 credits (0.0002 DASH); withdraw screen subtracts 500,000,000 credits (0.005 DASH). Eliminates float operations and simplifies fee handling logic.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~4 minutes

Poem

🐰 No floats to fret, just subtraction clean,
Saturating sums, the sharpest I've seen!
Twenty millions, five hundred more,
Integer math is what we adore! ✨

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@thepastaclaw
Copy link
Collaborator Author

Duplicate of #622 — sub-agent created the same fix independently. Closing in favor of #622.

@thepastaclaw
Copy link
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 22, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant