Skip to content

Commit

Permalink
cmd/tapcli: add events send/receive subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Mar 19, 2024
1 parent 818f6ab commit 958916e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
114 changes: 114 additions & 0 deletions cmd/tapcli/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"encoding/hex"
"fmt"

"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/urfave/cli"
)

var eventCommands = []cli.Command{
{
Name: "events",
ShortName: "e",
Usage: "Subscribe to live events from the Taproot Asset " +
"daemon.",
Category: "Events",
Subcommands: []cli.Command{
receiveEventsCommand,
sendEventsCommand,
},
},
}

var receiveEventsCommand = cli.Command{
Name: "receive",
ShortName: "r",
Usage: "Subscribe to events around receiving inbound assets",
Description: "Get live updates on the status of inbound asset " +
"transfers to the local node. This command will block " +
"until aborted manually by hitting Ctrl+C.",
Flags: []cli.Flag{
cli.StringFlag{
Name: addrName,
Usage: "(optional) the address to receive events " +
"for; if not set, events for all addresses " +
"will be shown",
},
},
Action: receiveEvents,
}

func receiveEvents(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()

addr, err := client.SubscribeReceiveEvents(
ctxc, &taprpc.SubscribeReceiveEventsRequest{
FilterAddr: ctx.String(addrName),
},
)
if err != nil {
return fmt.Errorf("unable to subscribe to receive events: %w",
err)
}

for {
event, err := addr.Recv()
if err != nil {
return fmt.Errorf("unable to receive event: %w", err)
}

printRespJSON(event)
}
}

var sendEventsCommand = cli.Command{
Name: "send",
ShortName: "s",
Usage: "Subscribe to events around sending outbound assets",
Description: "Get live updates on the status of outbound asset " +
"transfers to the local node. This command will block " +
"until aborted manually by hitting Ctrl+C.",
Flags: []cli.Flag{
cli.StringFlag{
Name: scriptKeyName,
Usage: "(optional) the script key to receive events " +
"for; if not set, events for all transfers " +
"will be shown",
},
},
Action: sendEvents,
}

func sendEvents(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()

scriptKeyBytes, err := hex.DecodeString(ctx.String(scriptKeyName))
if err != nil {
return fmt.Errorf("unable to hex decode script key: %w", err)
}

send, err := client.SubscribeSendEvents(
ctxc, &taprpc.SubscribeSendEventsRequest{
FilterScriptKey: scriptKeyBytes,
},
)
if err != nil {
return fmt.Errorf("unable to subscribe to send events: %w",
err)
}

for {
event, err := send.Recv()
if err != nil {
return fmt.Errorf("unable to receive event: %w", err)
}

printRespJSON(event)
}
}
1 change: 1 addition & 0 deletions cmd/tapcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func main() {
}
app.Commands = append(app.Commands, assetsCommands...)
app.Commands = append(app.Commands, addrCommands...)
app.Commands = append(app.Commands, eventCommands...)
app.Commands = append(app.Commands, proofCommands...)
app.Commands = append(app.Commands, universeCommands...)
app.Commands = append(app.Commands, devCommands...)
Expand Down

0 comments on commit 958916e

Please sign in to comment.