-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
125 lines (106 loc) · 2.91 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cmd
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/bandprotocol/falcon/relayer"
)
// queryCmd represents the command for querying data from source and destination chains.
func queryCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Query commands on source and destination chains",
}
cmd.AddCommand(
queryTunnelCmd(app),
queryPacketCmd(app),
queryBalanceCmd(app),
)
return cmd
}
// queryTunnelCmd returns a command that query tunnel information.
func queryTunnelCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "tunnel [tunnel_id]",
Aliases: []string{"t"},
Short: "Query commands on tunnel data",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s query tunnel 1`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
tunnelID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
tunnel, err := app.QueryTunnelInfo(cmd.Context(), tunnelID)
if err != nil {
return err
}
out, err := json.MarshalIndent(tunnel, "", " ")
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
},
}
return cmd
}
// queryPacketCmd returns a command that query packet information.
func queryPacketCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "packet [tunnel_id] [sequence]",
Aliases: []string{"p"},
Short: "Query commands on packet data",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s query packet 1 1`, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
tunnelID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
sequence, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}
packet, err := app.QueryTunnelPacketInfo(cmd.Context(), tunnelID, sequence)
if err != nil {
return err
}
out, err := json.MarshalIndent(packet, "", " ")
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(out))
return nil
},
}
return cmd
}
// queryBalanceCmd returns a command that query balance of the given account.
func queryBalanceCmd(app *relayer.App) *cobra.Command {
cmd := &cobra.Command{
Use: "balance [chain_name] [key_name]",
Aliases: []string{"b"},
Short: "Query commands on account balance",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s query balance eth test-key
$ %s q b eth test-key`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chainName := args[0]
keyName := args[1]
bal, err := app.QueryBalance(cmd.Context(), chainName, keyName)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), bal.String())
return nil
},
}
return cmd
}