Skip to content

Commit 037966b

Browse files
authored
Merge pull request #586 from hieblmi/getinfo-rpc
`getInfo` rpc for `loopd`
2 parents cb8975d + 84bf303 commit 037966b

10 files changed

+756
-106
lines changed

cmd/loop/info.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"github.com/urfave/cli"
8+
)
9+
10+
var getInfoCommand = cli.Command{
11+
Name: "getinfo",
12+
Usage: "show general information about the loop daemon",
13+
Description: "Displays general information about the daemon like " +
14+
"current version, connection parameters and basic swap " +
15+
"information.",
16+
Action: getInfo,
17+
}
18+
19+
func getInfo(ctx *cli.Context) error {
20+
client, cleanup, err := getClient(ctx)
21+
if err != nil {
22+
return err
23+
}
24+
defer cleanup()
25+
26+
cfg, err := client.GetInfo(
27+
context.Background(), &looprpc.GetInfoRequest{},
28+
)
29+
if err != nil {
30+
return err
31+
}
32+
33+
printRespJSON(cfg)
34+
35+
return nil
36+
}

cmd/loop/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func main() {
153153
monitorCommand, quoteCommand, listAuthCommand,
154154
listSwapsCommand, swapInfoCommand, getLiquidityParamsCommand,
155155
setLiquidityRuleCommand, suggestSwapCommand, setParamsCommand,
156+
getInfoCommand,
156157
}
157158

158159
err := app.Run(os.Args)

loopd/daemon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
438438

439439
// Now finally fully initialize the swap client RPC server instance.
440440
d.swapClientServer = swapClientServer{
441+
config: d.cfg,
441442
network: lndclient.Network(d.cfg.Network),
442443
impl: swapclient,
443444
liquidityMgr: getLiquidityManager(swapclient),

loopd/perms/perms.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ var RequiredPermissions = map[string][]bakery.Op{
6767
Entity: "suggestions",
6868
Action: "read",
6969
}},
70+
"/looprpc.SwapClient/GetInfo": {{
71+
Entity: "suggestions",
72+
Action: "read",
73+
}},
7074
"/looprpc.SwapClient/GetLiquidityParams": {{
7175
Entity: "suggestions",
7276
Action: "read",

loopd/swapclient_server.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type swapClientServer struct {
6868
clientrpc.UnimplementedSwapClientServer
6969
clientrpc.UnimplementedDebugServer
7070

71+
config *Config
7172
network lndclient.Network
7273
impl *loop.Client
7374
liquidityMgr *liquidity.Manager
@@ -738,6 +739,77 @@ func (s *swapClientServer) GetLsatTokens(ctx context.Context,
738739
return &clientrpc.TokensResponse{Tokens: rpcTokens}, nil
739740
}
740741

742+
// GetInfo returns basic information about the loop daemon and details to swaps
743+
// from the swap store.
744+
func (s *swapClientServer) GetInfo(_ context.Context,
745+
_ *clientrpc.GetInfoRequest) (*clientrpc.GetInfoResponse, error) {
746+
747+
// Fetch loop-outs from the loop db.
748+
outSwaps, err := s.impl.Store.FetchLoopOutSwaps()
749+
if err != nil {
750+
return nil, err
751+
}
752+
753+
// Collect loop-out stats.
754+
loopOutStats := &clientrpc.LoopStats{}
755+
for _, out := range outSwaps {
756+
switch out.State().State.Type() {
757+
case loopdb.StateTypeSuccess:
758+
loopOutStats.SuccessCount++
759+
loopOutStats.SumSucceededAmt += int64(
760+
out.Contract.AmountRequested,
761+
)
762+
763+
case loopdb.StateTypePending:
764+
loopOutStats.PendingCount++
765+
loopOutStats.SumPendingAmt += int64(
766+
out.Contract.AmountRequested,
767+
)
768+
769+
case loopdb.StateTypeFail:
770+
loopOutStats.FailCount++
771+
}
772+
}
773+
774+
// Fetch loop-ins from the loop db.
775+
inSwaps, err := s.impl.Store.FetchLoopInSwaps()
776+
if err != nil {
777+
return nil, err
778+
}
779+
780+
// Collect loop-in stats.
781+
loopInStats := &clientrpc.LoopStats{}
782+
for _, in := range inSwaps {
783+
switch in.State().State.Type() {
784+
case loopdb.StateTypeSuccess:
785+
loopInStats.SuccessCount++
786+
loopInStats.SumSucceededAmt += int64(
787+
in.Contract.AmountRequested,
788+
)
789+
790+
case loopdb.StateTypePending:
791+
loopInStats.PendingCount++
792+
loopInStats.SumPendingAmt += int64(
793+
in.Contract.AmountRequested,
794+
)
795+
796+
case loopdb.StateTypeFail:
797+
loopInStats.FailCount++
798+
}
799+
}
800+
801+
return &clientrpc.GetInfoResponse{
802+
Version: loop.Version(),
803+
Network: s.config.Network,
804+
RpcListen: s.config.RPCListen,
805+
RestListen: s.config.RESTListen,
806+
MacaroonPath: s.config.MacaroonPath,
807+
TlsCertPath: s.config.TLSCertPath,
808+
LoopOutStats: loopOutStats,
809+
LoopInStats: loopInStats,
810+
}, nil
811+
}
812+
741813
// GetLiquidityParams gets our current liquidity manager's parameters.
742814
func (s *swapClientServer) GetLiquidityParams(_ context.Context,
743815
_ *clientrpc.GetLiquidityParamsRequest) (*clientrpc.LiquidityParameters,

0 commit comments

Comments
 (0)