github.com/Finschia/finschia-sdk@v0.48.1/x/foundation/client/cli/query.go (about) 1 package cli 2 3 import ( 4 "context" 5 "strconv" 6 7 "github.com/spf13/cobra" 8 9 "github.com/Finschia/finschia-sdk/client" 10 "github.com/Finschia/finschia-sdk/client/flags" 11 sdk "github.com/Finschia/finschia-sdk/types" 12 "github.com/Finschia/finschia-sdk/x/foundation" 13 ) 14 15 // NewQueryCmd returns the parent command for all x/foundation CLi query commands. 16 func NewQueryCmd() *cobra.Command { 17 cmd := &cobra.Command{ 18 Use: foundation.ModuleName, 19 Short: "Querying commands for the foundation module", 20 } 21 22 cmd.AddCommand( 23 NewQueryCmdParams(), 24 NewQueryCmdTreasury(), 25 NewQueryCmdFoundationInfo(), 26 NewQueryCmdMember(), 27 NewQueryCmdMembers(), 28 NewQueryCmdProposal(), 29 NewQueryCmdProposals(), 30 NewQueryCmdVote(), 31 NewQueryCmdVotes(), 32 NewQueryCmdTallyResult(), 33 NewQueryCmdCensorships(), 34 NewQueryCmdGrants(), 35 ) 36 37 return cmd 38 } 39 40 // NewQueryCmdParams returns the query foundation parameters command. 41 func NewQueryCmdParams() *cobra.Command { 42 cmd := &cobra.Command{ 43 Use: "params", 44 Short: "Query foundation params", 45 Long: "Gets the current parameters of foundation", 46 Args: cobra.NoArgs, 47 RunE: func(cmd *cobra.Command, args []string) error { 48 clientCtx, err := client.GetClientQueryContext(cmd) 49 if err != nil { 50 return err 51 } 52 queryClient := foundation.NewQueryClient(clientCtx) 53 54 params := foundation.QueryParamsRequest{} 55 res, err := queryClient.Params(context.Background(), ¶ms) 56 if err != nil { 57 return err 58 } 59 60 return clientCtx.PrintProto(res) 61 }, 62 } 63 64 flags.AddQueryFlagsToCmd(cmd) 65 66 return cmd 67 } 68 69 // NewQueryCmdTreasury returns the amount of coins in the foundation treasury 70 func NewQueryCmdTreasury() *cobra.Command { 71 cmd := &cobra.Command{ 72 Use: "treasury", 73 Short: "Query foundation treasury", 74 Long: "Gets the amount of coins in the foundation treasury", 75 Args: cobra.NoArgs, 76 RunE: func(cmd *cobra.Command, args []string) error { 77 clientCtx, err := client.GetClientQueryContext(cmd) 78 if err != nil { 79 return err 80 } 81 queryClient := foundation.NewQueryClient(clientCtx) 82 83 req := foundation.QueryTreasuryRequest{} 84 res, err := queryClient.Treasury(context.Background(), &req) 85 if err != nil { 86 return err 87 } 88 89 return clientCtx.PrintProto(res) 90 }, 91 } 92 93 flags.AddQueryFlagsToCmd(cmd) 94 95 return cmd 96 } 97 98 // NewQueryCmdFoundationInfo returns the information of the foundation. 99 func NewQueryCmdFoundationInfo() *cobra.Command { 100 cmd := &cobra.Command{ 101 Use: "foundation-info", 102 Args: cobra.NoArgs, 103 Short: "Query the foundation information", 104 Long: `Query the foundation information 105 `, 106 RunE: func(cmd *cobra.Command, args []string) error { 107 clientCtx, err := client.GetClientQueryContext(cmd) 108 if err != nil { 109 return err 110 } 111 queryClient := foundation.NewQueryClient(clientCtx) 112 113 req := foundation.QueryFoundationInfoRequest{} 114 res, err := queryClient.FoundationInfo(context.Background(), &req) 115 if err != nil { 116 return err 117 } 118 119 return clientCtx.PrintProto(res) 120 }, 121 } 122 123 flags.AddQueryFlagsToCmd(cmd) 124 return cmd 125 } 126 127 // NewQueryCmdMember returns a member of the foundation. 128 func NewQueryCmdMember() *cobra.Command { 129 cmd := &cobra.Command{ 130 Use: "member [address]", 131 Args: cobra.ExactArgs(1), 132 Short: "Query a foundation member", 133 Long: `Query a foundation member 134 `, 135 RunE: func(cmd *cobra.Command, args []string) error { 136 clientCtx, err := client.GetClientQueryContext(cmd) 137 if err != nil { 138 return err 139 } 140 queryClient := foundation.NewQueryClient(clientCtx) 141 142 address := args[0] 143 if _, err := sdk.AccAddressFromBech32(address); err != nil { 144 return err 145 } 146 147 req := foundation.QueryMemberRequest{Address: address} 148 res, err := queryClient.Member(context.Background(), &req) 149 if err != nil { 150 return err 151 } 152 153 return clientCtx.PrintProto(res) 154 }, 155 } 156 157 flags.AddQueryFlagsToCmd(cmd) 158 return cmd 159 } 160 161 // NewQueryCmdMembers returns the members of the foundation. 162 func NewQueryCmdMembers() *cobra.Command { 163 cmd := &cobra.Command{ 164 Use: "members", 165 Args: cobra.NoArgs, 166 Short: "Query the foundation members", 167 Long: `Query the foundation members 168 `, 169 RunE: func(cmd *cobra.Command, args []string) error { 170 clientCtx, err := client.GetClientQueryContext(cmd) 171 if err != nil { 172 return err 173 } 174 queryClient := foundation.NewQueryClient(clientCtx) 175 176 req := foundation.QueryMembersRequest{} 177 res, err := queryClient.Members(context.Background(), &req) 178 if err != nil { 179 return err 180 } 181 182 return clientCtx.PrintProto(res) 183 }, 184 } 185 186 flags.AddQueryFlagsToCmd(cmd) 187 return cmd 188 } 189 190 // NewQueryCmdProposal returns a proposal baesd on proposal id. 191 func NewQueryCmdProposal() *cobra.Command { 192 cmd := &cobra.Command{ 193 Use: "proposal [proposal-id]", 194 Args: cobra.ExactArgs(1), 195 Short: "Query a proposal", 196 Long: `Query a proposal 197 `, 198 RunE: func(cmd *cobra.Command, args []string) error { 199 clientCtx, err := client.GetClientQueryContext(cmd) 200 if err != nil { 201 return err 202 } 203 queryClient := foundation.NewQueryClient(clientCtx) 204 205 proposalID, err := strconv.ParseUint(args[0], 10, 64) 206 if err != nil { 207 return err 208 } 209 210 req := foundation.QueryProposalRequest{ProposalId: proposalID} 211 res, err := queryClient.Proposal(context.Background(), &req) 212 if err != nil { 213 return err 214 } 215 216 return clientCtx.PrintProto(res) 217 }, 218 } 219 220 flags.AddQueryFlagsToCmd(cmd) 221 return cmd 222 } 223 224 // NewQueryCmdProposals returns all proposals of the foundation. 225 func NewQueryCmdProposals() *cobra.Command { 226 cmd := &cobra.Command{ 227 Use: "proposals", 228 Args: cobra.NoArgs, 229 Short: "Query all proposals", 230 Long: `Query all proposals 231 `, 232 RunE: func(cmd *cobra.Command, args []string) error { 233 clientCtx, err := client.GetClientQueryContext(cmd) 234 if err != nil { 235 return err 236 } 237 queryClient := foundation.NewQueryClient(clientCtx) 238 239 req := foundation.QueryProposalsRequest{} 240 res, err := queryClient.Proposals(context.Background(), &req) 241 if err != nil { 242 return err 243 } 244 245 return clientCtx.PrintProto(res) 246 }, 247 } 248 249 flags.AddQueryFlagsToCmd(cmd) 250 return cmd 251 } 252 253 // NewQueryCmdVote returns the vote of a voter on a proposal. 254 func NewQueryCmdVote() *cobra.Command { 255 cmd := &cobra.Command{ 256 Use: "vote [proposal-id] [voter]", 257 Args: cobra.ExactArgs(2), 258 Short: "Query the vote of a voter on a proposal", 259 Long: `Query the vote of a voter on a proposal 260 `, 261 RunE: func(cmd *cobra.Command, args []string) error { 262 clientCtx, err := client.GetClientQueryContext(cmd) 263 if err != nil { 264 return err 265 } 266 queryClient := foundation.NewQueryClient(clientCtx) 267 268 proposalID, err := strconv.ParseUint(args[0], 10, 64) 269 if err != nil { 270 return err 271 } 272 273 voter := args[1] 274 if _, err := sdk.AccAddressFromBech32(voter); err != nil { 275 return err 276 } 277 278 req := foundation.QueryVoteRequest{ProposalId: proposalID, Voter: voter} 279 res, err := queryClient.Vote(context.Background(), &req) 280 if err != nil { 281 return err 282 } 283 284 return clientCtx.PrintProto(res) 285 }, 286 } 287 288 flags.AddQueryFlagsToCmd(cmd) 289 return cmd 290 } 291 292 // NewQueryCmdVotes returns the votes on a proposal. 293 func NewQueryCmdVotes() *cobra.Command { 294 cmd := &cobra.Command{ 295 Use: "votes [proposal-id]", 296 Args: cobra.ExactArgs(1), 297 Short: "Query the votes on a proposal", 298 Long: `Query the votes on a proposal 299 `, 300 RunE: func(cmd *cobra.Command, args []string) error { 301 clientCtx, err := client.GetClientQueryContext(cmd) 302 if err != nil { 303 return err 304 } 305 queryClient := foundation.NewQueryClient(clientCtx) 306 307 proposalID, err := strconv.ParseUint(args[0], 10, 64) 308 if err != nil { 309 return err 310 } 311 312 req := foundation.QueryVotesRequest{ProposalId: proposalID} 313 res, err := queryClient.Votes(context.Background(), &req) 314 if err != nil { 315 return err 316 } 317 318 return clientCtx.PrintProto(res) 319 }, 320 } 321 322 flags.AddQueryFlagsToCmd(cmd) 323 return cmd 324 } 325 326 // NewQueryCmdTallyResult returns the tally of proposal votes. 327 func NewQueryCmdTallyResult() *cobra.Command { 328 cmd := &cobra.Command{ 329 Use: "tally [proposal-id]", 330 Args: cobra.ExactArgs(1), 331 Short: "Query the tally of proposal votes", 332 Long: `Query the tally of proposal votes 333 `, 334 RunE: func(cmd *cobra.Command, args []string) error { 335 clientCtx, err := client.GetClientQueryContext(cmd) 336 if err != nil { 337 return err 338 } 339 queryClient := foundation.NewQueryClient(clientCtx) 340 341 proposalID, err := strconv.ParseUint(args[0], 10, 64) 342 if err != nil { 343 return err 344 } 345 346 req := foundation.QueryTallyResultRequest{ProposalId: proposalID} 347 res, err := queryClient.TallyResult(context.Background(), &req) 348 if err != nil { 349 return err 350 } 351 352 return clientCtx.PrintProto(res) 353 }, 354 } 355 356 flags.AddQueryFlagsToCmd(cmd) 357 return cmd 358 } 359 360 // NewQueryCmdCensorships returns the query censorships command. 361 func NewQueryCmdCensorships() *cobra.Command { 362 cmd := &cobra.Command{ 363 Use: "censorships", 364 Short: "Query censorships", 365 Long: "Gets the current censorships", 366 Args: cobra.NoArgs, 367 RunE: func(cmd *cobra.Command, args []string) error { 368 clientCtx, err := client.GetClientQueryContext(cmd) 369 if err != nil { 370 return err 371 } 372 queryClient := foundation.NewQueryClient(clientCtx) 373 374 pageReq, err := client.ReadPageRequest(cmd.Flags()) 375 if err != nil { 376 return err 377 } 378 379 censorships := foundation.QueryCensorshipsRequest{ 380 Pagination: pageReq, 381 } 382 res, err := queryClient.Censorships(context.Background(), &censorships) 383 if err != nil { 384 return err 385 } 386 387 return clientCtx.PrintProto(res) 388 }, 389 } 390 391 flags.AddQueryFlagsToCmd(cmd) 392 flags.AddPaginationFlagsToCmd(cmd, "censorships") 393 394 return cmd 395 } 396 397 // NewQueryCmdGrants returns grants on a grantee 398 func NewQueryCmdGrants() *cobra.Command { 399 cmd := &cobra.Command{ 400 Use: "grants [grantee] [msg-type-url]?", 401 Short: "Query grants for a grantee and optionally a msg-type-url", 402 Long: `Query grants for a grantee and optionally a msg-type-url 403 `, 404 Args: cobra.RangeArgs(1, 2), 405 RunE: func(cmd *cobra.Command, args []string) error { 406 clientCtx, err := client.GetClientQueryContext(cmd) 407 if err != nil { 408 return err 409 } 410 queryClient := foundation.NewQueryClient(clientCtx) 411 412 pageReq, err := client.ReadPageRequest(cmd.Flags()) 413 if err != nil { 414 return err 415 } 416 417 grantee, err := sdk.AccAddressFromBech32(args[0]) 418 if err != nil { 419 return err 420 } 421 422 msgTypeURL := "" 423 if len(args) >= 2 { 424 msgTypeURL = args[1] 425 } 426 427 params := foundation.QueryGrantsRequest{ 428 Grantee: grantee.String(), 429 MsgTypeUrl: msgTypeURL, 430 Pagination: pageReq, 431 } 432 res, err := queryClient.Grants(context.Background(), ¶ms) 433 if err != nil { 434 return err 435 } 436 437 return clientCtx.PrintProto(res) 438 }, 439 } 440 441 flags.AddQueryFlagsToCmd(cmd) 442 flags.AddPaginationFlagsToCmd(cmd, "grants") 443 444 return cmd 445 }