Skip to content

feat(wallet): size the Max-send fee reserve from real UTXO count - #861

Merged
romchornyi merged 2 commits into
swift-sdk-integrationfrom
fix/max-send-size-based-fee-reserve
Jul 23, 2026
Merged

romchornyi merged 2 commits into
swift-sdk-integrationfrom
fix/max-send-size-based-fee-reserve

Conversation

@romchornyi

Copy link
Copy Markdown
Contributor

Issue being fixed or feature implemented

"Max" / all-funds core sends did not empty the wallet — they left ~0.001 DASH behind as change.

Root cause: the Max amount is WalletBalance.maxSendable = spendable − sendFeeReserveDuffs, and sendFeeReserveDuffs is a flat 100,000-duff reserve. The actual fee is size-based (~1–2k duffs at 1 duff/byte), so the send returned reserve − actual_fee (~98k duffs) as change. Confirmed on-chain earlier: an 11-input Max send charged 1,740 duffs but left a 98,260-duff change output (= 100,000 − 1,740, exact).

Funds were never lost (change went to the wallet's own address), but Max was misleading — the resulting balance was not zero.

What was done?

  • Added SwiftDashSDKTransactionSender.maxSendFeeReserveDuffs() — sizes the reserve from the BIP44 account's real spendable-UTXO count using the same 1 duff/byte model as estimatedSignalFee, plus a +50% safety margin so it can never under-reserve (an under-estimate would make the Max send fail to build).
  • Fail-safe: when the account can't be enumerated (no wallet/manager/UTXOs) it falls back to the flat sendFeeReserveDuffs, i.e. the previous behaviour — never worse, only tighter when it can be.
  • Exposed SwiftDashSDKWalletState.feeAwareMaxSendable() and routed every core-send Max/all-funds call site through it (Send, InternalTransfer, SwapConvert, OrderPreview, ContactProfile, BuyCredits, DashSpendPay).
  • CrowdNode deliberately kept its own deposit-cap fee logic (TX_FEE_PER_INPUT) — out of scope.

How Has This Been Tested?

Verified on device (testnet, dashpay scheme): a Max send now fills the full spendable balance (reserve dropped from the flat 100,000 to a few hundred duffs) and the confirm builds successfully — the +50% margin covered the real fee without under-reserving. Recommend a follow-up check that the post-send transparent balance settles to ~0. Note: maxSendFeeReserveDuffs() performs a UTXO enumeration on each read; it's a synchronous DB read but worth an eye on the per-keystroke affordability checks.

Breaking Changes

None.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Max / all-funds core sends reserved a flat 100,000-duff fee headroom
(WalletBalance.maxSendable). The real fee is ~1-2k duffs, so Max left the
difference (~0.001 DASH) behind as change and never emptied the wallet.

Add SwiftDashSDKTransactionSender.maxSendFeeReserveDuffs(), which sizes the
reserve from the BIP44 account's actual spendable-UTXO count using the same
1 duff/byte model as estimatedSignalFee, plus a 50% safety margin so it can
never under-reserve (which would make the Max send fail to build). It falls
back to the flat reserve when the account can't be enumerated, so behaviour
is never worse than before. Expose SwiftDashSDKWalletState.feeAwareMaxSendable()
and route every core-send Max/all-funds call site through it (CrowdNode keeps
its own deposit-cap fee logic).

Verified on device (testnet): Max now fills the full spendable balance and
the confirm builds successfully.
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c9bc9fdc-cd32-4642-8c25-f1bcbb0bfa54

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/max-send-size-based-fee-reserve

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.

@romchornyi
romchornyi requested a review from llbartekll July 23, 2026 09:02

@llbartekll llbartekll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The account lookup needs to include the BIP44 standard tag. is the whole Standard family and can contain both BIP44 () and BIP32 () account 0. Picking the first match without checking can therefore count UTXOs from the wrong account and compute an incorrect Max reserve. Please mirror here and require (and preferably use instead of the literal 0).

@llbartekll llbartekll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clarification with the exact symbols: typeTag == 0 covers the whole Standard family. Account 0 may exist as both BIP44 (standardTag == 0) and BIP32 (standardTag == 1). Please make the lookup require $0.standardTag == Self.bip44StandardTag and use Self.bip44AccountIndex instead of the literal 0, matching addressUtxos(script:).

typeTag == 0 is the whole Standard family; account 0 can exist as both
BIP44 (standardTag 0) and BIP32 (standardTag 1). Matching on typeTag+index
alone could pick the BIP32 account and size the reserve from the wrong
UTXO set. Require standardTag == bip44StandardTag and use bip44AccountIndex,
mirroring addressUtxos(script:).
@romchornyi
romchornyi requested a review from llbartekll July 23, 2026 09:06

@llbartekll llbartekll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good

@romchornyi
romchornyi merged commit 1a555af into swift-sdk-integration Jul 23, 2026
1 check passed
@romchornyi
romchornyi deleted the fix/max-send-size-based-fee-reserve branch July 23, 2026 09:24
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.

3 participants