-
Notifications
You must be signed in to change notification settings - Fork 1
Use sendAll when change would be dust instead of creating dust change outputs #451
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
Open
ben-kaufman
wants to merge
2
commits into
feat/multiple-addresses-types
Choose a base branch
from
fix/dust-change-send-all
base: feat/multiple-addresses-types
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.
+158
−40
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import Foundation | ||
|
|
||
| /// Helper for determining when to use sendAll to avoid creating dust change outputs. | ||
| enum DustChangeHelper { | ||
| /// Returns true if the expected change would be dust (below dust limit), so sendAll should be used. | ||
| /// - Parameters: | ||
| /// - totalInput: Total sats from selected UTXOs (or spendable balance) | ||
| /// - amountSats: Amount to send to recipient | ||
| /// - normalFee: Fee for a normal send (recipient + change outputs) | ||
| /// - dustLimit: Minimum non-dust amount (default: Env.dustLimit) | ||
| /// - Returns: true when change would be dust and sendAll should be used | ||
| static func shouldUseSendAllToAvoidDust( | ||
| totalInput: UInt64, | ||
| amountSats: UInt64, | ||
| normalFee: UInt64, | ||
| dustLimit: UInt64 = UInt64(Env.dustLimit) | ||
| ) -> Bool { | ||
| let expectedChange = Int64(totalInput) - Int64(amountSats) - Int64(normalFee) | ||
| return expectedChange >= 0 && expectedChange < Int64(dustLimit) | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| @testable import Bitkit | ||
| import XCTest | ||
|
|
||
| final class DustChangeHelperTests: XCTestCase { | ||
| private let dustLimit: UInt64 = 547 | ||
|
|
||
| // MARK: - Change would be dust -> use sendAll | ||
|
|
||
| func testChangeBelowDustLimit_ShouldUseSendAll() { | ||
| // totalInput: 100_000, amount: 99_500, fee: 500 -> change = 0 | ||
| XCTAssertTrue(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 99500, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
|
|
||
| // totalInput: 100_000, amount: 99_000, fee: 500 -> change = 500 (below 547) | ||
| XCTAssertTrue(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 99000, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
|
|
||
| // totalInput: 100_000, amount: 98_954, fee: 500 -> change = 546 (just below dust) | ||
| XCTAssertTrue(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 98954, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
| } | ||
|
|
||
| func testChangeAtDustLimit_ShouldNotUseSendAll() { | ||
| // change = 547 is at the limit; < 547 means dust. So 547 is NOT dust. | ||
| // totalInput: 100_000, amount: 98_953, fee: 500 -> change = 547 (at limit, NOT dust) | ||
| XCTAssertFalse(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 98953, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
| } | ||
|
|
||
| func testChangeAboveDustLimit_ShouldNotUseSendAll() { | ||
| // totalInput: 100_000, amount: 98_000, fee: 500 -> change = 1_500 | ||
| XCTAssertFalse(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 98000, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
|
|
||
| // totalInput: 100_000, amount: 98_954, fee: 495 -> change = 551 | ||
| XCTAssertFalse(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 98954, | ||
| normalFee: 495, | ||
| dustLimit: dustLimit | ||
| )) | ||
| } | ||
|
|
||
| func testMaxSend_ChangeZero_ShouldUseSendAll() { | ||
| // totalInput: 100_000, amount: 99_500, fee: 500 -> change = 0 (max case) | ||
| XCTAssertTrue(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 99500, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
| } | ||
|
|
||
| func testInsufficientFunds_NegativeChange_ShouldNotUseSendAll() { | ||
| // totalInput: 100_000, amount: 100_000, fee: 500 -> change = -500 | ||
| XCTAssertFalse(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 100_000, | ||
| normalFee: 500, | ||
| dustLimit: dustLimit | ||
| )) | ||
| } | ||
|
|
||
| func testUsesEnvDustLimit_WhenNotSpecified() { | ||
| // Verify default uses Env.dustLimit (547): change 546 is dust | ||
| XCTAssertTrue(DustChangeHelper.shouldUseSendAllToAvoidDust( | ||
| totalInput: 100_000, | ||
| amountSats: 99454, | ||
| normalFee: 0 | ||
| // dustLimit omitted -> uses Env.dustLimit | ||
| )) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.