-
Notifications
You must be signed in to change notification settings - Fork 2
fix: handle dust change in max transfer to spending #748
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
Draft
jvsena42
wants to merge
6
commits into
master
Choose a base branch
from
fix/dust-on-max-transfer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−13
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d35ffc3
fix: used provided amount instead of all amount to calculate the fee
jvsena42 f781d8b
fix: use isMaxAmount when the remaining value is dust
jvsena42 0751c39
Merge branch 'master' into fix/dust-on-max-transfer
jvsena42 d14bf62
Merge branch 'master' into fix/dust-on-max-transfer
jvsena42 b9c94c8
Merge branch 'master' into fix/dust-on-max-transfer
jvsena42 2d76f28
Merge branch 'master' into fix/dust-on-max-transfer
jvsena42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Fee Estimation Uses Wrong Amount
The
calculateTotalFeecall on line 201-205 estimates the on-chain mining fee for the wrong transaction shape, which can cause incorrect dust change detection.The Problem:
The code passes
amountSats = spendableBalance(the full wallet balance) to estimate the fee. When the amount equals the full balance, the fee estimator constructs a send-all transaction with no change output (1 output). However, the change formula on line 207 models a transaction sendingorder.feeSat(less than the full balance), which will have a change output (2 outputs).A transaction with a change output has a higher fee because the extra output adds weight (a P2WPKH output is ~31 vbytes, P2TR ~43 vbytes). At typical fee rates, this is a few hundred sats difference.
Why This Matters:
Because
txFeeis underestimated,expectedChangeis overestimated by the cost of one change output. This creates a window where the actual change would be dust (< 546 sats), but the code computesexpectedChange >= 546, failing to setshouldUseSendAll = true. The transaction then attempts a regular send with a dust change output, which can fail.Example:
Suppose:
spendableBalance= 100,000 satsorder.feeSat= 98,200 satsCode computes:
expectedChange = 100,000 - 98,200 - 1,100 = 700(above 546, soshouldUseSendAll = false)Correct:
expectedChange = 100,000 - 98,200 - 1,410 = 390(below 546, should use send-all!)The transaction would attempt a regular send with a 390-sat change output, which is dust and would fail.
The Fix:
This ensures the fee is estimated for the same transaction shape (sending
order.feeSatwith a change output) that the change formula models.Reference:
bitkit-android/app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt
Lines 200 to 206 in 2d76f28