Skip to content

Commit 2d26575

Browse files
committed
loop: add stubs and CLI commands for the asset deposit subserver
This commit adds a placeholder asset deposit subserver along with the corresponding CLI commands to the Loop daemon and CLI.
1 parent 8ed1697 commit 2d26575

File tree

7 files changed

+291
-1
lines changed

7 files changed

+291
-1
lines changed

assets/deposit/log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package deposit
2+
3+
import (
4+
"github.com/btcsuite/btclog/v2"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
// Subsystem defines the sub system name of this package.
9+
const Subsystem = "ADEP"
10+
11+
// log is a logger that is initialized with no output filters. This
12+
// means the package will not perform any logging by default until the caller
13+
// requests it.
14+
var log btclog.Logger
15+
16+
// The default amount of logging is none.
17+
func init() {
18+
UseLogger(build.NewSubLogger(Subsystem, nil))
19+
}
20+
21+
// UseLogger uses a specified Logger to output package logging info.
22+
// This should be used in preference to SetLogWriter if the caller is also
23+
// using btclog.
24+
func UseLogger(logger btclog.Logger) {
25+
log = logger
26+
}

assets/deposit/server.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package deposit
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"google.golang.org/grpc/codes"
8+
"google.golang.org/grpc/status"
9+
)
10+
11+
// Server is the grpc server that serves the reservation service.
12+
type Server struct {
13+
looprpc.UnimplementedAssetDepositClientServer
14+
}
15+
16+
func NewServer() *Server {
17+
return &Server{}
18+
}
19+
20+
// NewAssetDeposit is the rpc endpoint for loop clients to request a new asset
21+
// deposit.
22+
func (s *Server) NewAssetDeposit(ctx context.Context,
23+
in *looprpc.NewAssetDepositRequest) (*looprpc.NewAssetDepositResponse,
24+
error) {
25+
26+
return nil, status.Error(codes.Unimplemented, "unimplemented")
27+
}
28+
29+
// ListAssetDeposits is the rpc endpoint for loop clients to list their asset
30+
// deposits.
31+
func (s *Server) ListAssetDeposits(ctx context.Context,
32+
in *looprpc.ListAssetDepositsRequest) (
33+
*looprpc.ListAssetDepositsResponse, error) {
34+
35+
return nil, status.Error(codes.Unimplemented, "unimplemented")
36+
}
37+
38+
// RevealAssetDepositKey is the rpc endpoint for loop clients to reveal the
39+
// asset deposit key for a specific asset deposit.
40+
func (s *Server) RevealAssetDepositKey(ctx context.Context,
41+
in *looprpc.RevealAssetDepositKeyRequest) (
42+
*looprpc.RevealAssetDepositKeyResponse, error) {
43+
44+
return nil, status.Error(codes.Unimplemented, "unimplemented")
45+
}
46+
47+
// WithdrawAssetDeposits is the rpc endpoint for loop clients to withdraw their
48+
// asset deposits.
49+
func (s *Server) WithdrawAssetDeposits(ctx context.Context,
50+
in *looprpc.WithdrawAssetDepositsRequest) (
51+
*looprpc.WithdrawAssetDepositsResponse, error) {
52+
53+
return nil, status.Error(codes.Unimplemented, "unimplemented")
54+
}

cmd/loop/asset_deposits.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"github.com/urfave/cli"
8+
)
9+
10+
var (
11+
assetDepositsCommands = cli.Command{
12+
Name: "asset deposits",
13+
ShortName: "ad",
14+
Usage: "TAP asset deposit commands.",
15+
Subcommands: []cli.Command{
16+
newAssetDepositCommand,
17+
listAssetDepositsCommand,
18+
withdrawAssetDepositCommand,
19+
},
20+
}
21+
22+
newAssetDepositCommand = cli.Command{
23+
Name: "new",
24+
ShortName: "n",
25+
Usage: "Create a new TAP asset deposit.",
26+
Description: "Create a new TAP asset deposit.",
27+
Action: newAssetDeposit,
28+
Flags: []cli.Flag{
29+
cli.StringFlag{
30+
Name: "asset_id",
31+
Usage: "The asset id of the asset to deposit.",
32+
},
33+
cli.Uint64Flag{
34+
Name: "amt",
35+
Usage: "the amount to deposit (in asset " +
36+
"units).",
37+
},
38+
cli.UintFlag{
39+
Name: "expiry",
40+
Usage: "the deposit expiry in blocks.",
41+
},
42+
},
43+
}
44+
45+
listAssetDepositsCommand = cli.Command{
46+
Name: "list",
47+
ShortName: "l",
48+
Usage: "List TAP asset deposits.",
49+
Description: "List TAP asset deposits.",
50+
Flags: []cli.Flag{
51+
cli.UintFlag{
52+
Name: "min_confs",
53+
Usage: "The minimum amount of confirmations " +
54+
"an anchor output should have to be " +
55+
"listed.",
56+
},
57+
cli.UintFlag{
58+
Name: "max_confs",
59+
Usage: "The maximum number of confirmations " +
60+
"an anchor output could have to be " +
61+
"listed.",
62+
},
63+
},
64+
Action: listAssetDeposits,
65+
}
66+
67+
withdrawAssetDepositCommand = cli.Command{
68+
Name: "withdraw",
69+
ShortName: "w",
70+
Usage: "Withdraw TAP asset deposits.",
71+
Description: "Withdraw TAP asset deposits.",
72+
Action: withdrawAssetDeposit,
73+
Flags: []cli.Flag{
74+
cli.StringSliceFlag{
75+
Name: "deposit_ids",
76+
Usage: "The deposit ids of the asset " +
77+
"deposits to withdraw.",
78+
},
79+
},
80+
}
81+
)
82+
83+
func init() {
84+
commands = append(commands, assetDepositsCommands)
85+
}
86+
87+
func newAssetDeposit(ctx *cli.Context) error {
88+
ctxb := context.Background()
89+
if ctx.NArg() > 0 {
90+
return cli.ShowCommandHelp(ctx, "newdeposit")
91+
}
92+
93+
client, cleanup, err := getAssetDepositsClient(ctx)
94+
if err != nil {
95+
return err
96+
}
97+
defer cleanup()
98+
99+
assetID := ctx.String("asset_id")
100+
amt := ctx.Uint64("amt")
101+
expiry := int32(ctx.Uint("expiry"))
102+
103+
resp, err := client.NewAssetDeposit(
104+
ctxb, &looprpc.NewAssetDepositRequest{
105+
AssetId: assetID,
106+
Amount: amt,
107+
CsvExpiry: expiry,
108+
},
109+
)
110+
if err != nil {
111+
return err
112+
}
113+
114+
printJSON(resp)
115+
116+
return nil
117+
}
118+
119+
func listAssetDeposits(ctx *cli.Context) error {
120+
ctxb := context.Background()
121+
if ctx.NArg() > 0 {
122+
return cli.ShowCommandHelp(ctx, "list")
123+
}
124+
125+
client, cleanup, err := getAssetDepositsClient(ctx)
126+
if err != nil {
127+
return err
128+
}
129+
defer cleanup()
130+
131+
resp, err := client.ListAssetDeposits(
132+
ctxb, &looprpc.ListAssetDepositsRequest{
133+
MinConfs: uint32(ctx.Int("min_confs")),
134+
MaxConfs: uint32(ctx.Int("max_confs")),
135+
})
136+
if err != nil {
137+
return err
138+
}
139+
140+
printRespJSON(resp)
141+
142+
return nil
143+
}
144+
145+
func withdrawAssetDeposit(ctx *cli.Context) error {
146+
ctxb := context.Background()
147+
if ctx.NArg() > 0 {
148+
return cli.ShowCommandHelp(ctx, "withdraw")
149+
}
150+
151+
client, cleanup, err := getAssetDepositsClient(ctx)
152+
if err != nil {
153+
return err
154+
}
155+
defer cleanup()
156+
157+
depositIDs := ctx.StringSlice("deposit_ids")
158+
159+
resp, err := client.WithdrawAssetDeposits(
160+
ctxb, &looprpc.WithdrawAssetDepositsRequest{
161+
DepositIds: depositIDs,
162+
},
163+
)
164+
if err != nil {
165+
return err
166+
}
167+
168+
printRespJSON(resp)
169+
170+
return nil
171+
}

cmd/loop/main.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,44 @@ func main() {
185185
}
186186
}
187187

188-
func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) {
188+
func getConn(ctx *cli.Context) (*grpc.ClientConn, func(), error) {
189189
rpcServer := ctx.GlobalString("rpcserver")
190190
tlsCertPath, macaroonPath, err := extractPathArgs(ctx)
191191
if err != nil {
192192
return nil, nil, err
193193
}
194+
194195
conn, err := getClientConn(rpcServer, tlsCertPath, macaroonPath)
195196
if err != nil {
196197
return nil, nil, err
197198
}
198199
cleanup := func() { conn.Close() }
199200

201+
return conn, cleanup, nil
202+
}
203+
204+
func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) {
205+
conn, cleanup, err := getConn(ctx)
206+
if err != nil {
207+
return nil, nil, err
208+
}
209+
200210
loopClient := looprpc.NewSwapClientClient(conn)
201211
return loopClient, cleanup, nil
202212
}
203213

214+
func getAssetDepositsClient(ctx *cli.Context) (
215+
looprpc.AssetDepositClientClient, func(), error) {
216+
217+
conn, cleanup, err := getConn(ctx)
218+
if err != nil {
219+
return nil, nil, err
220+
}
221+
222+
assetDepositsClient := looprpc.NewAssetDepositClientClient(conn)
223+
return assetDepositsClient, cleanup, nil
224+
}
225+
204226
func getMaxRoutingFee(amt btcutil.Amount) btcutil.Amount {
205227
return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate)
206228
}

loopd/daemon.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightninglabs/lndclient"
1717
"github.com/lightninglabs/loop"
1818
"github.com/lightninglabs/loop/assets"
19+
asset_deposit "github.com/lightninglabs/loop/assets/deposit"
1920
"github.com/lightninglabs/loop/instantout"
2021
"github.com/lightninglabs/loop/instantout/reservation"
2122
"github.com/lightninglabs/loop/loopdb"
@@ -248,6 +249,11 @@ func (d *Daemon) startWebServers() error {
248249
)
249250
loop_looprpc.RegisterSwapClientServer(d.grpcServer, d)
250251

252+
// Register the asset deposit sub-server within the grpc server.
253+
loop_looprpc.RegisterAssetDepositClientServer(
254+
d.grpcServer, d.swapClientServer.assetDepositServer,
255+
)
256+
251257
// Register our debug server if it is compiled in.
252258
d.registerDebugServer()
253259

@@ -700,6 +706,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
700706
)
701707
}
702708

709+
// If the deposit manager is nil, the server will reutrn Unimplemented
710+
// error for all RPCs.
711+
assetDepositServer := asset_deposit.NewServer()
712+
703713
// Now finally fully initialize the swap client RPC server instance.
704714
d.swapClientServer = swapClientServer{
705715
config: d.cfg,
@@ -717,6 +727,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
717727
depositManager: depositManager,
718728
withdrawalManager: withdrawalManager,
719729
staticLoopInManager: staticLoopInManager,
730+
assetDepositServer: assetDepositServer,
720731
assetClient: d.assetClient,
721732
}
722733

loopd/log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/lightninglabs/aperture/l402"
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
10+
"github.com/lightninglabs/loop/assets/deposit"
1011
"github.com/lightninglabs/loop/fsm"
1112
"github.com/lightninglabs/loop/instantout"
1213
"github.com/lightninglabs/loop/instantout/reservation"
@@ -92,6 +93,9 @@ func SetupLoggers(root *build.SubLoggerManager, intercept signal.Interceptor) {
9293
lnd.AddSubLogger(
9394
root, sweep.Subsystem, intercept, sweep.UseLogger,
9495
)
96+
lnd.AddSubLogger(
97+
root, deposit.Subsystem, intercept, deposit.UseLogger,
98+
)
9599
}
96100

97101
// genSubLogger creates a logger for a subsystem. We provide an instance of

loopd/swapclient_server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/lightninglabs/lndclient"
2323
"github.com/lightninglabs/loop"
2424
"github.com/lightninglabs/loop/assets"
25+
asset_deposit "github.com/lightninglabs/loop/assets/deposit"
2526
"github.com/lightninglabs/loop/fsm"
2627
"github.com/lightninglabs/loop/instantout"
2728
"github.com/lightninglabs/loop/instantout/reservation"
@@ -97,6 +98,7 @@ type swapClientServer struct {
9798
depositManager *deposit.Manager
9899
withdrawalManager *withdraw.Manager
99100
staticLoopInManager *loopin.Manager
101+
assetDepositServer *asset_deposit.Server
100102
assetClient *assets.TapdClient
101103
swaps map[lntypes.Hash]loop.SwapInfo
102104
subscribers map[int]chan<- interface{}

0 commit comments

Comments
 (0)