liquidity: add asset swap info to swap builder

This commit is contained in:
sputn1ck 2025-02-17 10:34:04 +01:00
parent 832a0526d6
commit e3f049ae6f
No known key found for this signature in database
GPG key ID: 671103D881A5F0E4
5 changed files with 71 additions and 6 deletions

View file

@ -62,7 +62,8 @@ type swapBuilder interface {
// is just for a dry run. // is just for a dry run.
buildSwap(ctx context.Context, peer route.Vertex, buildSwap(ctx context.Context, peer route.Vertex,
channels []lnwire.ShortChannelID, amount btcutil.Amount, channels []lnwire.ShortChannelID, amount btcutil.Amount,
params Parameters) (swapSuggestion, error) params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
error)
} }
// swapSuggestion is an interface implemented by suggested swaps for our // swapSuggestion is an interface implemented by suggested swaps for our

View file

@ -637,6 +637,7 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {
suggestion, err := builder.buildSwap( suggestion, err := builder.buildSwap(
ctx, channel.PubKeyBytes, outgoing, swapAmt, easyParams, ctx, channel.PubKeyBytes, outgoing, swapAmt, easyParams,
nil,
) )
if err != nil { if err != nil {
return err return err

View file

@ -84,7 +84,8 @@ func (b *loopInBuilder) inUse(traffic *swapTraffic, peer route.Vertex,
// For loop in, we do not add the autoloop label for dry runs. // For loop in, we do not add the autoloop label for dry runs.
func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex, func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
_ []lnwire.ShortChannelID, amount btcutil.Amount, _ []lnwire.ShortChannelID, amount btcutil.Amount,
params Parameters) (swapSuggestion, error) { params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
error) {
quote, err := b.cfg.LoopInQuote(ctx, &loop.LoopInQuoteRequest{ quote, err := b.cfg.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
Amount: amount, Amount: amount,

View file

@ -184,7 +184,7 @@ func TestLoopinBuildSwap(t *testing.T) {
swap, err := builder.buildSwap( swap, err := builder.buildSwap(
context.Background(), peer1, []lnwire.ShortChannelID{ context.Background(), peer1, []lnwire.ShortChannelID{
chan1, chan1,
}, swapAmt, params, }, swapAmt, params, nil,
) )
assert.Equal(t, testCase.expectedSwap, swap) assert.Equal(t, testCase.expectedSwap, swap)
assert.Equal(t, testCase.expectedErr, err) assert.Equal(t, testCase.expectedErr, err)

View file

@ -2,6 +2,7 @@ package liquidity
import ( import (
"context" "context"
"encoding/hex"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
@ -84,6 +85,33 @@ func (b *loopOutBuilder) inUse(traffic *swapTraffic, peer route.Vertex,
return nil return nil
} }
type assetSwapInfo struct {
assetID string
peerPubkey []byte
}
// buildSwapOpts contains the options for building a swap.
type buildSwapOpts struct {
assetSwap *assetSwapInfo
}
// defaultBuildSwapOpts returns the default options for building a swap.
func defaultBuildSwapOpts() *buildSwapOpts {
return &buildSwapOpts{}
}
// buildSwapOption is a functional option that can be passed to the buildSwap
// method.
type buildSwapOption func(*buildSwapOpts)
// withAssetSwapInfo is an option to provide asset swap information to the
// builder.
func withAssetSwapInfo(assetSwapInfo *assetSwapInfo) buildSwapOption {
return func(o *buildSwapOpts) {
o.assetSwap = assetSwapInfo
}
}
// buildSwap creates a swap for the target peer/channels provided. The autoloop // buildSwap creates a swap for the target peer/channels provided. The autoloop
// boolean indicates whether this swap will actually be executed, because there // boolean indicates whether this swap will actually be executed, because there
// are some calls we can leave out if this swap is just for a dry run (ie, when // are some calls we can leave out if this swap is just for a dry run (ie, when
@ -94,14 +122,42 @@ func (b *loopOutBuilder) inUse(traffic *swapTraffic, peer route.Vertex,
// dry-run, and we do not add the autoloop label to the recommended swap. // dry-run, and we do not add the autoloop label to the recommended swap.
func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex, func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
channels []lnwire.ShortChannelID, amount btcutil.Amount, channels []lnwire.ShortChannelID, amount btcutil.Amount,
params Parameters) (swapSuggestion, error) { params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
error) {
var (
assetRfqRequest *loop.AssetRFQRequest
assetIDBytes []byte
err error
)
opts := defaultBuildSwapOpts()
for _, opt := range swapOpts {
opt(opts)
}
initiator := getInitiator(params)
if opts.assetSwap != nil {
assetSwap := opts.assetSwap
assetIDBytes, err = hex.DecodeString(assetSwap.assetID)
if err != nil {
return nil, err
}
assetRfqRequest = &loop.AssetRFQRequest{
AssetId: assetIDBytes,
AssetEdgeNode: assetSwap.peerPubkey,
}
initiator += "-" + assetSwap.assetID
}
quote, err := b.cfg.LoopOutQuote( quote, err := b.cfg.LoopOutQuote(
ctx, &loop.LoopOutQuoteRequest{ ctx, &loop.LoopOutQuoteRequest{
Amount: amount, Amount: amount,
SweepConfTarget: params.SweepConfTarget, SweepConfTarget: params.SweepConfTarget,
SwapPublicationDeadline: b.cfg.Clock.Now(), SwapPublicationDeadline: b.cfg.Clock.Now(),
Initiator: getInitiator(params), Initiator: initiator,
AssetRFQRequest: assetRfqRequest,
}, },
) )
if err != nil { if err != nil {
@ -146,7 +202,13 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
MaxSwapFee: quote.SwapFee, MaxSwapFee: quote.SwapFee,
MaxPrepayAmount: quote.PrepayAmount, MaxPrepayAmount: quote.PrepayAmount,
SweepConfTarget: params.SweepConfTarget, SweepConfTarget: params.SweepConfTarget,
Initiator: getInitiator(params), Initiator: initiator,
}
if opts.assetSwap != nil {
request.AssetId = assetIDBytes
request.AssetPrepayRfqId = quote.LoopOutRfq.PrepayRfqId
request.AssetSwapRfqId = quote.LoopOutRfq.SwapRfqId
} }
if params.Autoloop { if params.Autoloop {