Skip to content

fix: add BTC swap reserve enforcement with quote-aware fee handling#30404

Merged
Battambang merged 3 commits into
mainfrom
fix-swap-inconsistent-btc-network-fees2
May 20, 2026
Merged

fix: add BTC swap reserve enforcement with quote-aware fee handling#30404
Battambang merged 3 commits into
mainfrom
fix-swap-inconsistent-btc-network-fees2

Conversation

@Battambang

@Battambang Battambang commented May 19, 2026

Copy link
Copy Markdown
Contributor

Description

This pull request enhances the useInsufficientNativeReserveError hook to support Bitcoin (BTC) native reserve logic, ensuring accurate handling of minimum reserve requirements and quote-aware calculations for BTC swaps. It also improves test coverage for these scenarios and refactors related code for better clarity and extensibility.

BTC Native Reserve Support and Logic Improvements:

  • Added BTC mainnet support to the minimum native reserve calculation, introducing a specific reserve threshold for BTC and handling BTC chain IDs using CAIP format. The hook now accounts for BTC-specific requirements when determining if a swap would violate the minimum reserve.

Testing Enhancements:

  • Added comprehensive test cases for BTC reserve logic, including scenarios for reserve breaches, quote-aware calculations, and edge cases where reserve or fees restrict swaps. Introduced BTC token and quote mocks for robust test coverage.

Refactoring and Type Improvements:

  • Refactored types and utility functions to support both EVM and non-EVM (CAIP) chain IDs, improving maintainability and future extensibility.

Integration Updates:

  • Updated BridgeViewContent and SwapsConfirmButton components to pass the activeQuote prop to the hook, enabling quote-aware reserve checks in the UI flow.

Changelog

CHANGELOG entry: added BTC swap reserve enforcement with quote-aware fee handling

Related issues

Fixes: Bridging BTC is failing at the transaction crafting in certain cases

Manual testing steps

BTC max amount / reserve validation

  1. Use a BTC account with a small balance.
  2. Select BTC as source and ETH as destination.
  3. Enter an amount that would leave less than 0.00003 BTC after the swap.
  4. Confirm the warning banner says Minimum BTC reserve balance is required.
  5. Confirm the message displays a reserve of 0.00003 BTC.
  6. Confirm the CTA shows Insufficient funds.
  7. Click Use max allowed.
  8. Confirm the input is reduced to the max amount shown in the banner.

Screenshots/Recordings

Before

After

BTC-swap

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Medium Risk
Updates swap/bridge validation for BTC to enforce a minimum native reserve using quote-provided network fees/overhead, which can block transactions if miscomputed. Changes are localized and covered by new unit tests, but affect user-facing amount limits and submit eligibility.

Overview
Adds BTC native reserve enforcement to swap/bridge submit validation. useInsufficientNativeReserveError now supports CAIP chain IDs, defines a BTC mainnet minimum reserve (0.00003), and for BTC uses activeQuote to subtract quoted network fees and source-side overhead when computing the max swappable amount.

Integrates quote-aware checks in the UI flow. BridgeView and SwapsConfirmButton now pass activeQuote into the hook so banners/CTAs and the confirm button correctly reflect BTC reserve + fee constraints.

Expands test coverage. Adds BTC-specific unit tests covering reserve boundary conditions, quote fee consumption, decimal truncation, and cases where gas validation should own the failure.

Reviewed by Cursor Bugbot for commit da0093c. Bugbot is set up for automated code reviews on this repo. Configure here.

@Battambang Battambang requested a review from a team as a code owner May 19, 2026 18:52
@github-actions

Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbotv2 metamaskbotv2 Bot added the team-networks Networks team label May 19, 2026
@github-project-automation github-project-automation Bot moved this to Needs dev review in PR review queue May 19, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f157e30. Configure here.

Comment thread app/components/UI/Bridge/hooks/useInsufficientNativeReserveError/index.ts Outdated
Comment thread app/components/UI/Bridge/hooks/useInsufficientNativeReserveError/index.ts Outdated
@Battambang Battambang force-pushed the fix-swap-inconsistent-btc-network-fees2 branch from f157e30 to f102f2f Compare May 19, 2026 21:49
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeSwap, SmokeConfirmations
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: medium
  • AI Confidence: 90%
click to see 🤖 AI reasoning details

E2E Test Selection:
The changes are focused on the Bridge/Swap feature area:

  1. useInsufficientNativeReserveError hook - Extended to support Bitcoin (BTC) mainnet native reserve balance requirements. Previously only handled EVM gas-sponsored networks (Monad). Now handles BTC mainnet with a 0.00003 BTC minimum reserve, factoring in active quote network fees and sent amounts.

  2. BridgeView/index.tsx - Passes activeQuote to the updated hook.

  3. SwapsConfirmButton/index.tsx - Passes activeQuote to the updated hook and reorders the useBridgeQuoteDataContext() call to ensure activeQuote is available before the hook call.

  4. Test file - New unit tests for BTC reserve logic.

Tag Selection Rationale:

  • SmokeSwap: Directly affected — the swap/bridge confirm button (SwapsConfirmButton) and bridge view (BridgeView) are modified. The validation logic that determines whether a user can proceed with a swap/bridge transaction has been changed to support BTC reserve requirements.
  • SmokeConfirmations: Per tag description, when selecting SmokeSwap, also select SmokeConfirmations since swap/bridge flows require transaction confirmations.

The changes are scoped to the Bridge/Swap UI components and a hook used only within that feature area. No core controllers, navigation, or shared infrastructure is affected. The risk is medium because the logic change affects the swap/bridge confirmation flow validation, which could impact whether users can proceed with BTC bridge transactions.

Performance Test Selection:
The changes are limited to validation logic in the Bridge/Swap feature (a hook that checks native reserve balances and a confirm button component). These are not performance-sensitive paths — they involve simple arithmetic calculations on existing data rather than rendering lists, loading assets, or affecting app startup/initialization. No performance test tags are warranted.

View GitHub Actions results

@github-project-automation github-project-automation Bot moved this from Needs dev review to Review finalised - Ready to be merged in PR review queue May 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

@Battambang Battambang added this pull request to the merge queue May 20, 2026
@Battambang Battambang added the QA Passed QA testing has been completed and passed label May 20, 2026
Merged via the queue into main with commit bc03176 May 20, 2026
141 of 144 checks passed
@Battambang Battambang deleted the fix-swap-inconsistent-btc-network-fees2 branch May 20, 2026 13:51
@github-actions github-actions Bot locked and limited conversation to collaborators May 20, 2026
@metamaskbotv2 metamaskbotv2 Bot added the release-7.79.0 Issue or pull request that will be included in release 7.79.0 label May 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

QA Passed QA testing has been completed and passed release-7.79.0 Issue or pull request that will be included in release 7.79.0 size-M team-networks Networks team

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants