-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
272 lines (228 loc) · 6.68 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package cli
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/incubus-network/fury/x/cdp/types"
)
// Query CDP flags
const (
flagCollateralType = "collateral-type"
flagOwner = "owner"
flagID = "id"
flagRatio = "ratio" // returns CDPs under the given collateralization ratio threshold
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// Group nameservice queries under a subcommand
cdpQueryCmd := &cobra.Command{
Use: "cdp",
Short: "Querying commands for the cdp module",
}
cmds := []*cobra.Command{
QueryCdpCmd(),
QueryGetCdpsCmd(),
QueryCdpDepositsCmd(),
QueryParamsCmd(),
QueryGetAccounts(),
}
for _, cmd := range cmds {
flags.AddQueryFlagsToCmd(cmd)
}
cdpQueryCmd.AddCommand(cmds...)
return cdpQueryCmd
}
// QueryCdpCmd returns the command handler for querying a particular cdp
func QueryCdpCmd() *cobra.Command {
return &cobra.Command{
Use: "cdp [owner-addr] [collateral-type]",
Short: "get info about a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get a CDP by the owner address and the collateral name.
Example:
$ %s query %s cdp fury15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
_, err = sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
res, err := queryClient.Cdp(context.Background(), &types.QueryCdpRequest{
Owner: args[0],
CollateralType: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
}
// QueryGetCdpsCmd queries the cdps in the store
func QueryGetCdpsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cdps",
Short: "query cdps with optional filters",
Long: strings.TrimSpace(`Query for all paginated cdps that match optional filters:
Example:
$ kvcli q cdp cdps --collateral-type=bnb
$ kvcli q cdp cdps --owner=fury1hatdq32u5x4wnxrtv5wzjzmq49sxgjgsj0mffm
$ kvcli q cdp cdps --id=21
$ kvcli q cdp cdps --ratio=2.75
$ kvcli q cdp cdps --page=2 --limit=100
`,
),
RunE: func(cmd *cobra.Command, args []string) error {
strCollateralType, err := cmd.Flags().GetString(flagCollateralType)
if err != nil {
return err
}
strOwner, err := cmd.Flags().GetString(flagOwner)
if err != nil {
return err
}
strID, err := cmd.Flags().GetString(flagID)
if err != nil {
return err
}
strRatio, err := cmd.Flags().GetString(flagRatio)
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
req := types.QueryCdpsRequest{
Pagination: pageReq,
}
if len(strCollateralType) != 0 {
req.CollateralType = strings.ToLower(strings.TrimSpace(strCollateralType))
}
if len(strOwner) != 0 {
cdpOwner, err := sdk.AccAddressFromBech32(strings.ToLower(strings.TrimSpace(strOwner)))
if err != nil {
return fmt.Errorf("cannot parse address from cdp owner %s", strOwner)
}
req.Owner = cdpOwner.String()
}
if len(strID) != 0 {
cdpID, err := strconv.ParseUint(strID, 10, 64)
if err != nil {
return fmt.Errorf("cannot parse cdp ID %s", strID)
}
req.ID = cdpID
}
if len(strRatio) != 0 {
cdpRatio, err := sdk.NewDecFromStr(strRatio)
if err != nil {
return fmt.Errorf("cannot parse cdp ratio %s", strRatio)
}
// ratio is also validated on server
req.Ratio = cdpRatio.String()
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Cdps(context.Background(), &req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
cmd.Flags().String(flagCollateralType, "", "(optional) filter by CDP collateral type")
cmd.Flags().String(flagOwner, "", "(optional) filter by CDP owner")
cmd.Flags().String(flagID, "", "(optional) filter by CDP ID")
cmd.Flags().String(flagRatio, "", "(optional) filter by CDP collateralization ratio threshold")
flags.AddPaginationFlagsToCmd(cmd, "cdps")
return cmd
}
// QueryCdpDepositsCmd returns the command handler for querying the deposits of a particular cdp
func QueryCdpDepositsCmd() *cobra.Command {
return &cobra.Command{
Use: "deposits [owner-addr] [collateral-type]",
Short: "get deposits for a cdp",
Long: strings.TrimSpace(
fmt.Sprintf(`Get the deposits of a CDP.
Example:
$ %s query %s deposits fury15qdefkmwswysgg4qxgqpqr35k3m49pkx2jdfnw atom-a
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
_, err = sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}
res, err := queryClient.Deposits(context.Background(), &types.QueryDepositsRequest{
Owner: args[0],
CollateralType: args[1],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
}
// QueryParamsCmd returns the command handler for cdp parameter querying
func QueryParamsCmd() *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "get the cdp module parameters",
Long: "get the current global cdp module parameters.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(&res.Params)
},
}
}
// QueryGetAccounts queries CDP module accounts
func QueryGetAccounts() *cobra.Command {
return &cobra.Command{
Use: "accounts",
Short: "get module accounts",
Long: "get cdp module account addresses",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Accounts(context.Background(), &types.QueryAccountsRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
}