-
Notifications
You must be signed in to change notification settings - Fork 121
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
hieblmi
wants to merge
8
commits into
lightninglabs:master
Choose a base branch
from
hieblmi:static-arb-amount-swap
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
947d08b
swapserverrpc: arbitrary static swap amount
hieblmi 1cc4bbb
staticaddr: method to fetch deposits by outpoints
hieblmi 0c6ef86
sqlc: selected amount migration for static address swaps
hieblmi da90873
looprpc: fractional amount for static address loop-ins
hieblmi 5f83d05
staticaddr: fractional loop-in amount
hieblmi d91a75f
loopd: fractional static address swap amount
hieblmi 25b79bd
staticaddr: show swap amount in listswaps command
hieblmi 541a118
staticaddr: selected swap amount migration
hieblmi 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
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,104 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lightninglabs/loop/looprpc" | ||
"github.com/lightningnetwork/lnd/input" | ||
"github.com/lightningnetwork/lnd/lnwallet" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testCase struct { | ||
deposits []*looprpc.Deposit | ||
targetValue int64 | ||
expected []string | ||
expectedErr string | ||
} | ||
|
||
// TestSelectDeposits tests the selectDeposits function, which selects | ||
// deposits that can cover a target value, while respecting the dust limit. | ||
func TestSelectDeposits(t *testing.T) { | ||
dustLimit := lnwallet.DustLimitForSize(input.P2TRSize) | ||
d1, d2, d3 := &looprpc.Deposit{ | ||
Value: 1_000_000, | ||
Outpoint: "1", | ||
}, &looprpc.Deposit{ | ||
Value: 2_000_000, | ||
Outpoint: "2", | ||
}, &looprpc.Deposit{ | ||
Value: 3_000_000, | ||
Outpoint: "3", | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
deposits: []*looprpc.Deposit{d1}, | ||
targetValue: 1_000_000, | ||
expected: []string{"1"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1, d2}, | ||
targetValue: 1_000_000, | ||
expected: []string{"2"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1, d2, d3}, | ||
targetValue: 1_000_000, | ||
expected: []string{"3"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1}, | ||
targetValue: 1_000_001, | ||
expected: []string{}, | ||
expectedErr: "not enough deposits to cover", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1}, | ||
targetValue: int64(1_000_000 - dustLimit), | ||
expected: []string{"1"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1}, | ||
targetValue: int64(1_000_000 - dustLimit + 1), | ||
expected: []string{}, | ||
expectedErr: "not enough deposits to cover", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1, d2, d3}, | ||
targetValue: d1.Value + d2.Value + d3.Value, | ||
expected: []string{"1", "2", "3"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1, d2, d3}, | ||
targetValue: d1.Value + d2.Value + d3.Value - | ||
int64(dustLimit), | ||
expected: []string{"1", "2", "3"}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
deposits: []*looprpc.Deposit{d1, d2, d3}, | ||
targetValue: d1.Value + d2.Value + d3.Value - | ||
int64(dustLimit) + 1, | ||
expected: []string{}, | ||
expectedErr: "not enough deposits to cover", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
selectedDeposits, err := selectDeposits( | ||
tc.deposits, tc.targetValue, | ||
) | ||
if tc.expectedErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, tc.expectedErr) | ||
} | ||
require.ElementsMatch(t, tc.expected, selectedDeposits) | ||
} | ||
} |
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
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.
(see my next comment too)
Ideally this should be solved with a knapsack algorithm, but since that's a bit difficult, maybe we can use a variance of this heuristic to first try to find a covering deposit?
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.
The current implementation sorts values descending and then fills the sack until the target is met. This might be a good enough approximation of a knapsack solver, see here: https://en.wikipedia.org/wiki/Knapsack_problem#Greedy_approximation_algorithm