Skip to content

Commit 69f2af9

Browse files
committed
loop+cmd: extract types into swap module
1 parent 7a1680d commit 69f2af9

File tree

13 files changed

+104
-89
lines changed

13 files changed

+104
-89
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
144144
}
145145

146146
swaps = append(swaps, &SwapInfo{
147-
SwapType: TypeOut,
147+
SwapType: swap.TypeOut,
148148
SwapContract: swp.Contract.SwapContract,
149149
SwapStateData: swp.State(),
150150
SwapHash: swp.Hash,
@@ -164,7 +164,7 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
164164
}
165165

166166
swaps = append(swaps, &SwapInfo{
167-
SwapType: TypeIn,
167+
SwapType: swap.TypeIn,
168168
SwapContract: swp.Contract.SwapContract,
169169
SwapStateData: swp.State(),
170170
SwapHash: swp.Hash,

cmd/loop/loopin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66

77
"github.com/btcsuite/btcutil"
8-
"github.com/lightninglabs/loop"
98
"github.com/lightninglabs/loop/looprpc"
9+
"github.com/lightninglabs/loop/swap"
1010
"github.com/urfave/cli"
1111
)
1212

@@ -68,7 +68,7 @@ func loopIn(ctx *cli.Context) error {
6868
}
6969

7070
limits := getInLimits(amt, quote)
71-
err = displayLimits(loop.TypeIn, amt, limits, external)
71+
err = displayLimits(swap.TypeIn, amt, limits, external)
7272
if err != nil {
7373
return err
7474
}

cmd/loop/loopout.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/lightninglabs/loop"
88
"github.com/lightninglabs/loop/looprpc"
9+
"github.com/lightninglabs/loop/swap"
910
"github.com/urfave/cli"
1011
)
1112

@@ -93,7 +94,8 @@ func loopOut(ctx *cli.Context) error {
9394

9495
limits := getLimits(amt, quote)
9596

96-
if err := displayLimits(loop.TypeOut, amt, limits, false); err != nil {
97+
err = displayLimits(swap.TypeOut, amt, limits, false)
98+
if err != nil {
9799
return err
98100
}
99101

cmd/loop/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func getLimits(amt btcutil.Amount, quote *looprpc.QuoteResponse) *limits {
116116
}
117117
}
118118

119-
func displayLimits(swapType loop.Type, amt btcutil.Amount, l *limits,
119+
func displayLimits(swapType swap.Type, amt btcutil.Amount, l *limits,
120120
externalHtlc bool) error {
121121

122122
totalSuccessMax := l.maxMinerFee + l.maxSwapFee
@@ -127,7 +127,7 @@ func displayLimits(swapType loop.Type, amt btcutil.Amount, l *limits,
127127
totalSuccessMax += *l.maxPrepayRoutingFee
128128
}
129129

130-
if swapType == loop.TypeIn && externalHtlc {
130+
if swapType == swap.TypeIn && externalHtlc {
131131
fmt.Printf("On-chain fee for external loop in is not " +
132132
"included.\nSufficient fees will need to be paid " +
133133
"when constructing the transaction in the external " +
@@ -148,7 +148,7 @@ func displayLimits(swapType loop.Type, amt btcutil.Amount, l *limits,
148148
return nil
149149
case "x":
150150
fmt.Println()
151-
if swapType != loop.TypeIn || !externalHtlc {
151+
if swapType != swap.TypeIn || !externalHtlc {
152152
fmt.Printf("Max on-chain fee: %d\n",
153153
l.maxMinerFee)
154154
}

cmd/loopd/swapclient_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightninglabs/loop"
1212
"github.com/lightninglabs/loop/lndclient"
1313
"github.com/lightninglabs/loop/loopdb"
14+
"github.com/lightninglabs/loop/swap"
1415

1516
"github.com/btcsuite/btcutil"
1617
"github.com/lightninglabs/loop/looprpc"
@@ -113,9 +114,9 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
113114

114115
var swapType looprpc.SwapType
115116
switch loopSwap.SwapType {
116-
case loop.TypeIn:
117+
case swap.TypeIn:
117118
swapType = looprpc.SwapType_LOOP_IN
118-
case loop.TypeOut:
119+
case swap.TypeOut:
119120
swapType = looprpc.SwapType_LOOP_OUT
120121
default:
121122
return nil, errors.New("unknown swap type")

interface.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/btcsuite/btcutil"
77
"github.com/lightninglabs/loop/loopdb"
8+
"github.com/lightninglabs/loop/swap"
89
"github.com/lightningnetwork/lnd/lntypes"
910
)
1011

@@ -235,28 +236,6 @@ type SwapInfoKit struct {
235236
LastUpdateTime time.Time
236237
}
237238

238-
// Type indicates the type of swap.
239-
type Type uint8
240-
241-
const (
242-
// TypeIn is a loop in swap.
243-
TypeIn Type = iota
244-
245-
// TypeOut is a loop out swap.
246-
TypeOut
247-
)
248-
249-
func (t Type) String() string {
250-
switch t {
251-
case TypeIn:
252-
return "In"
253-
case TypeOut:
254-
return "Out"
255-
default:
256-
return "Unknown"
257-
}
258-
}
259-
260239
// SwapInfo exposes common info fields for loop in and loop out swaps.
261240
type SwapInfo struct {
262241
loopdb.SwapStateData
@@ -265,7 +244,7 @@ type SwapInfo struct {
265244

266245
SwapHash lntypes.Hash
267246

268-
SwapType Type
247+
SwapType swap.Type
269248

270249
loopdb.SwapContract
271250

log.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package loop
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/btcsuite/btclog"
8-
"github.com/lightningnetwork/lnd/lntypes"
97
)
108

119
// log is a logger that is initialized with no output filters. This
@@ -24,46 +22,3 @@ func (logWriter) Write(p []byte) (n int, err error) {
2422
os.Stdout.Write(p)
2523
return len(p), nil
2624
}
27-
28-
// SwapLog logs with a short swap hash prefix.
29-
type SwapLog struct {
30-
// Logger is the underlying based logger.
31-
Logger btclog.Logger
32-
33-
// Hash is the hash the identifies the target swap.
34-
Hash lntypes.Hash
35-
}
36-
37-
// Infof formats message according to format specifier and writes to
38-
// log with LevelInfo.
39-
func (s *SwapLog) Infof(format string, params ...interface{}) {
40-
s.Logger.Infof(
41-
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
42-
params...,
43-
)
44-
}
45-
46-
// Warnf formats message according to format specifier and writes to
47-
// to log with LevelError.
48-
func (s *SwapLog) Warnf(format string, params ...interface{}) {
49-
s.Logger.Warnf(
50-
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
51-
params...,
52-
)
53-
}
54-
55-
// Errorf formats message according to format specifier and writes to
56-
// to log with LevelError.
57-
func (s *SwapLog) Errorf(format string, params ...interface{}) {
58-
s.Logger.Errorf(
59-
fmt.Sprintf("%v %s", ShortHash(&s.Hash), format),
60-
params...,
61-
)
62-
63-
}
64-
65-
// ShortHash returns a shortened version of the hash suitable for use in
66-
// logging.
67-
func ShortHash(hash *lntypes.Hash) string {
68-
return hash.String()[:6]
69-
}

loopin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"fmt"
88
"time"
99

10-
"github.com/lightninglabs/loop/swap"
11-
1210
"github.com/btcsuite/btcutil"
1311

1412
"github.com/lightningnetwork/lnd/chainntnfs"
@@ -19,6 +17,7 @@ import (
1917

2018
"github.com/lightninglabs/loop/lndclient"
2119
"github.com/lightninglabs/loop/loopdb"
20+
"github.com/lightninglabs/loop/swap"
2221
"github.com/lightningnetwork/lnd/lntypes"
2322
)
2423

@@ -149,7 +148,8 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
149148
}
150149

151150
swapKit, err := newSwapKit(
152-
swapHash, TypeIn, cfg, &contract.SwapContract, swap.HtlcNP2WSH,
151+
swapHash, swap.TypeIn, cfg, &contract.SwapContract,
152+
swap.HtlcNP2WSH,
153153
)
154154
if err != nil {
155155
return nil, err
@@ -182,7 +182,8 @@ func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig,
182182
logger.Infof("Resuming loop in swap %v", hash)
183183

184184
swapKit, err := newSwapKit(
185-
hash, TypeIn, cfg, &pend.Contract.SwapContract, swap.HtlcNP2WSH,
185+
hash, swap.TypeIn, cfg, &pend.Contract.SwapContract,
186+
swap.HtlcNP2WSH,
186187
)
187188
if err != nil {
188189
return nil, err

loopout.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
116116
}
117117

118118
swapKit, err := newSwapKit(
119-
swapHash, TypeOut, cfg, &contract.SwapContract, swap.HtlcP2WSH,
119+
swapHash, swap.TypeOut, cfg, &contract.SwapContract,
120+
swap.HtlcP2WSH,
120121
)
121122
if err != nil {
122123
return nil, err
@@ -149,7 +150,8 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
149150
logger.Infof("Resuming loop out swap %v", hash)
150151

151152
swapKit, err := newSwapKit(
152-
hash, TypeOut, cfg, &pend.Contract.SwapContract, swap.HtlcP2WSH,
153+
hash, swap.TypeOut, cfg, &pend.Contract.SwapContract,
154+
swap.HtlcP2WSH,
153155
)
154156
if err != nil {
155157
return nil, err

swap.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type swapKit struct {
1616

1717
height int32
1818

19-
log *SwapLog
19+
log *swap.PrefixLog
2020

2121
lastUpdateTime time.Time
2222
cost loopdb.SwapCost
@@ -25,10 +25,10 @@ type swapKit struct {
2525
swapConfig
2626

2727
contract *loopdb.SwapContract
28-
swapType Type
28+
swapType swap.Type
2929
}
3030

31-
func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig,
31+
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
3232
contract *loopdb.SwapContract, outputType swap.HtlcOutputType) (
3333
*swapKit, error) {
3434

@@ -42,7 +42,7 @@ func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig,
4242
return nil, err
4343
}
4444

45-
log := &SwapLog{
45+
log := &swap.PrefixLog{
4646
Hash: hash,
4747
Logger: logger,
4848
}

0 commit comments

Comments
 (0)