Skip to content

Commit e3f049a

Browse files
committed
liquidity: add asset swap info to swap builder
1 parent 832a052 commit e3f049a

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

liquidity/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ type swapBuilder interface {
6262
// is just for a dry run.
6363
buildSwap(ctx context.Context, peer route.Vertex,
6464
channels []lnwire.ShortChannelID, amount btcutil.Amount,
65-
params Parameters) (swapSuggestion, error)
65+
params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
66+
error)
6667
}
6768

6869
// swapSuggestion is an interface implemented by suggested swaps for our

liquidity/liquidity.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {
637637

638638
suggestion, err := builder.buildSwap(
639639
ctx, channel.PubKeyBytes, outgoing, swapAmt, easyParams,
640+
nil,
640641
)
641642
if err != nil {
642643
return err

liquidity/loopin_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func (b *loopInBuilder) inUse(traffic *swapTraffic, peer route.Vertex,
8484
// For loop in, we do not add the autoloop label for dry runs.
8585
func (b *loopInBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
8686
_ []lnwire.ShortChannelID, amount btcutil.Amount,
87-
params Parameters) (swapSuggestion, error) {
87+
params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
88+
error) {
8889

8990
quote, err := b.cfg.LoopInQuote(ctx, &loop.LoopInQuoteRequest{
9091
Amount: amount,

liquidity/loopin_builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestLoopinBuildSwap(t *testing.T) {
184184
swap, err := builder.buildSwap(
185185
context.Background(), peer1, []lnwire.ShortChannelID{
186186
chan1,
187-
}, swapAmt, params,
187+
}, swapAmt, params, nil,
188188
)
189189
assert.Equal(t, testCase.expectedSwap, swap)
190190
assert.Equal(t, testCase.expectedErr, err)

liquidity/loopout_builder.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package liquidity
22

33
import (
44
"context"
5+
"encoding/hex"
56

67
"github.com/btcsuite/btcd/btcutil"
78
"github.com/lightninglabs/loop"
@@ -84,6 +85,33 @@ func (b *loopOutBuilder) inUse(traffic *swapTraffic, peer route.Vertex,
8485
return nil
8586
}
8687

88+
type assetSwapInfo struct {
89+
assetID string
90+
peerPubkey []byte
91+
}
92+
93+
// buildSwapOpts contains the options for building a swap.
94+
type buildSwapOpts struct {
95+
assetSwap *assetSwapInfo
96+
}
97+
98+
// defaultBuildSwapOpts returns the default options for building a swap.
99+
func defaultBuildSwapOpts() *buildSwapOpts {
100+
return &buildSwapOpts{}
101+
}
102+
103+
// buildSwapOption is a functional option that can be passed to the buildSwap
104+
// method.
105+
type buildSwapOption func(*buildSwapOpts)
106+
107+
// withAssetSwapInfo is an option to provide asset swap information to the
108+
// builder.
109+
func withAssetSwapInfo(assetSwapInfo *assetSwapInfo) buildSwapOption {
110+
return func(o *buildSwapOpts) {
111+
o.assetSwap = assetSwapInfo
112+
}
113+
}
114+
87115
// buildSwap creates a swap for the target peer/channels provided. The autoloop
88116
// boolean indicates whether this swap will actually be executed, because there
89117
// 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,
94122
// dry-run, and we do not add the autoloop label to the recommended swap.
95123
func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
96124
channels []lnwire.ShortChannelID, amount btcutil.Amount,
97-
params Parameters) (swapSuggestion, error) {
125+
params Parameters, swapOpts ...buildSwapOption) (swapSuggestion,
126+
error) {
127+
128+
var (
129+
assetRfqRequest *loop.AssetRFQRequest
130+
assetIDBytes []byte
131+
err error
132+
)
133+
134+
opts := defaultBuildSwapOpts()
135+
for _, opt := range swapOpts {
136+
opt(opts)
137+
}
138+
139+
initiator := getInitiator(params)
140+
141+
if opts.assetSwap != nil {
142+
assetSwap := opts.assetSwap
143+
assetIDBytes, err = hex.DecodeString(assetSwap.assetID)
144+
if err != nil {
145+
return nil, err
146+
}
147+
assetRfqRequest = &loop.AssetRFQRequest{
148+
AssetId: assetIDBytes,
149+
AssetEdgeNode: assetSwap.peerPubkey,
150+
}
151+
initiator += "-" + assetSwap.assetID
152+
}
98153

99154
quote, err := b.cfg.LoopOutQuote(
100155
ctx, &loop.LoopOutQuoteRequest{
101156
Amount: amount,
102157
SweepConfTarget: params.SweepConfTarget,
103158
SwapPublicationDeadline: b.cfg.Clock.Now(),
104-
Initiator: getInitiator(params),
159+
Initiator: initiator,
160+
AssetRFQRequest: assetRfqRequest,
105161
},
106162
)
107163
if err != nil {
@@ -146,7 +202,13 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex,
146202
MaxSwapFee: quote.SwapFee,
147203
MaxPrepayAmount: quote.PrepayAmount,
148204
SweepConfTarget: params.SweepConfTarget,
149-
Initiator: getInitiator(params),
205+
Initiator: initiator,
206+
}
207+
208+
if opts.assetSwap != nil {
209+
request.AssetId = assetIDBytes
210+
request.AssetPrepayRfqId = quote.LoopOutRfq.PrepayRfqId
211+
request.AssetSwapRfqId = quote.LoopOutRfq.SwapRfqId
150212
}
151213

152214
if params.Autoloop {

0 commit comments

Comments
 (0)