Skip to content

staticaddr: fractional swap amount #887

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
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

hieblmi
Copy link
Collaborator

@hieblmi hieblmi commented Feb 20, 2025

first relevant commit in this PR is 81c4117

This PR adds an amount flag to static loop-in swaps that allows the caller to only swap a fraction of the value of the selected deposits. If only amount is selected, the client selects deposits automatically to cover for the swaps amount.

To ensure backwards UX compatibility, omitting the new flag (--amount=0) selects the total deposit amount for swapping.

This PR also adds a method SelectDeposits to the loop-in manager that is used to coin-select deposits in case the user
only provides a static swap amount.

TODO

  • Sign sweepless sweep and htlc tx with change output if needed
  • integration test

@hieblmi hieblmi self-assigned this Feb 20, 2025
@hieblmi hieblmi marked this pull request as draft February 20, 2025 14:47
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 3 times, most recently from fd6cc75 to f54115f Compare February 21, 2025 15:23
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 6 times, most recently from 67612da to b3522b1 Compare March 19, 2025 11:27
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 6 times, most recently from 8b33f65 to 21fb9ac Compare April 2, 2025 13:48
@hieblmi hieblmi requested a review from Copilot April 2, 2025 13:52
@hieblmi hieblmi marked this pull request as ready for review April 2, 2025 13:52
Copilot

This comment was marked as outdated.

@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 3 times, most recently from 4b1058d to 5625af3 Compare April 3, 2025 15:02
@hieblmi hieblmi requested review from starius, bhandras and sputn1ck April 4, 2025 12:09
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch from 5625af3 to 569359f Compare April 4, 2025 12:09
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch from 569359f to 8be2c65 Compare April 30, 2025 10:51
@hieblmi
Copy link
Collaborator Author

hieblmi commented Apr 30, 2025

Rebased

@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 3 times, most recently from 0127354 to 8adb766 Compare July 16, 2025 10:18
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch 5 times, most recently from d58da27 to 90d2ee6 Compare July 23, 2025 10:16
@lightninglabs-deploy
Copy link

@bhandras: review reminder
@hieblmi, remember to re-request review from reviewers when ready

@hieblmi hieblmi force-pushed the static-arb-amount-swap branch from 90d2ee6 to 24c68d2 Compare July 28, 2025 11:57
@hieblmi hieblmi requested review from Copilot and sputn1ck July 28, 2025 13:25
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds a --amount flag to static loop-in swaps that allows users to swap only a fraction of their deposit value. The remaining value is sent back as change to the static address.

Key changes:

  • Added amount field to swap server RPC for fractional swap amounts
  • Extended sweep batcher to handle change outputs in transaction construction
  • Created deposit selection mechanism for automatic coin selection when only amount is specified

Reviewed Changes

Copilot reviewed 41 out of 42 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
utils/dust_limit.go New utility function for calculating dust limits
swapserverrpc/staticaddr.proto Added optional amount field to loop-in request
sweepbatcher/sweep_batcher.go Extended batcher to support change outputs and presigning
sweepbatcher/sweep_batch.go Enhanced transaction construction with change output handling
staticaddr/loopin/sql_store.go Added selected amount storage and migration support
Comments suppressed due to low confidence (1)

staticaddr/loopin/sql_store.go:23

  • Changing the constant name from outpointSeparator to OutpointSeparator makes it exported, which may not be intended if it's only used internally. Consider if this should remain private.
const OutpointSeparator = ";"

@@ -2798,6 +2798,8 @@ func testSweepBatcherComposite(t *testing.T, store testStore,
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

lnd.SetMinRelayFee(200)
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

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

The hardcoded minimum relay fee value (200) is used inconsistently throughout the test file. Consider defining this as a constant or using a more descriptive variable to improve maintainability and make the test intentions clearer.

Suggested change
lnd.SetMinRelayFee(200)
lnd.SetMinRelayFee(minRelayFee)

Copilot uses AI. Check for mistakes.

@@ -113,7 +128,7 @@ func (h *mockPresignedHelper) DestPkScript(ctx context.Context,
}

// SignTx tries to sign the transaction. If all the inputs are online, it signs
// the exact transaction passed and adds it to presignedBatches. Otherwise it
// the exact transaction passed and adds it to presignedBatches. Otherwise, it
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should maintain consistent punctuation. The sentence should end without a comma: 'Otherwise it' rather than 'Otherwise, it'.

Suggested change
// the exact transaction passed and adds it to presignedBatches. Otherwise, it
// the exact transaction passed and adds it to presignedBatches. Otherwise it

Copilot uses AI. Check for mistakes.

@@ -1382,15 +1484,21 @@ func (b *batch) publishMixedBatch(ctx context.Context) (btcutil.Amount, error,
// known in advance to be non-cooperative (nonCoopHint) and not failed
// to sign cooperatively in previous rounds (coopFailed). If any of them
// fails, the sweep is excluded from all following rounds and another
// round is attempted. Otherwise the cycle completes and we sign the
// round is attempted. Otherwise, the cycle completes and we sign the
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should maintain consistent punctuation. The sentence should end without a comma: 'Otherwise the cycle completes' rather than 'Otherwise, the cycle completes'.

Suggested change
// round is attempted. Otherwise, the cycle completes and we sign the
// round is attempted. Otherwise the cycle completes and we sign the

Copilot uses AI. Check for mistakes.

@@ -1450,7 +1559,7 @@ func (b *batch) publishMixedBatch(ctx context.Context) (btcutil.Amount, error,
// If there was any failure of cooperative signing, we need to
// update weight estimates (since non-cooperative signing has
// larger witness) and hence update the whole transaction and
// all the signatures. Otherwise we complete cooperative part.
// all the signatures. Otherwise, we complete cooperative part.
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should maintain consistent punctuation. The sentence should end without a comma: 'Otherwise we complete cooperative part' rather than 'Otherwise, we complete cooperative part'.

Suggested change
// all the signatures. Otherwise, we complete cooperative part.
// all the signatures. Otherwise we complete the cooperative part.

Copilot uses AI. Check for mistakes.

}
}

log.Infof("Updating selected swap amounts for %d loop out swaps",
Copy link
Preview

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

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

The log message refers to 'loop out swaps' but this is migrating loop-in swaps. The message should be corrected to 'loop in swaps'.

Suggested change
log.Infof("Updating selected swap amounts for %d loop out swaps",
log.Infof("Updating selected swap amounts for %d loop in swaps",

Copilot uses AI. Check for mistakes.

@hieblmi hieblmi force-pushed the static-arb-amount-swap branch from 24c68d2 to dd855df Compare July 30, 2025 07:43
hieblmi and others added 15 commits August 1, 2025 10:34
since deposits have been normalized in the db in the
context of a static address swap this commit now
retrieves the deposit information as part of
GetLoopInByHash and GetStaticAddressLoopInSwapsByStates.
The QuoteRequest adds a field select_deposits to signal
that the specified quote amount should be coin-selected from the client's deposits in order to determine the
number of deposits to quote for.
The StaticAddressLoopInRequest gets a new amount field that either indicates that the swap amount should be allocated from the passed deposits or coin-selected from all available deposits.
In this commit we add a new function SelectDeposits
to the loop-in manager. It coin-selects deposits that
meet an arbitrary swap amount provided by the client.
We have to ensure that the server creates the correct
change outputs for the htlc- and sweepless sweep
transactions.
If a quote request contains an amount and flag
SelectDeposits set to true the quoting coin-
selects the required deposits to meet the
swap amount in order to quote for the number
of deposits.
The selected_amount column of all previous
swaps is filled with the total value of
deposits that partook in these swaps.
@hieblmi hieblmi force-pushed the static-arb-amount-swap branch from dd855df to 6af4a98 Compare August 1, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants