github.com/gravity-devs/liquidity@v1.5.3/x/liquidity/client/cli/query.go (about) 1 package cli 2 3 // DONTCOVER 4 // client is excluded from test coverage in the poc phase milestone 1 and will be included in milestone 2 with completeness 5 6 import ( 7 "context" 8 "fmt" 9 "strconv" 10 "strings" 11 12 "github.com/cosmos/cosmos-sdk/client" 13 "github.com/cosmos/cosmos-sdk/client/flags" 14 "github.com/cosmos/cosmos-sdk/version" 15 "github.com/spf13/cobra" 16 17 "github.com/gravity-devs/liquidity/x/liquidity/types" 18 ) 19 20 // GetQueryCmd returns the cli query commands for this module 21 func GetQueryCmd() *cobra.Command { 22 liquidityQueryCmd := &cobra.Command{ 23 Use: types.ModuleName, 24 Short: "Querying commands for the liquidity module", 25 DisableFlagParsing: true, 26 SuggestionsMinimumDistance: 2, 27 RunE: client.ValidateCmd, 28 } 29 30 liquidityQueryCmd.AddCommand( 31 GetCmdQueryParams(), 32 GetCmdQueryLiquidityPool(), 33 GetCmdQueryLiquidityPools(), 34 GetCmdQueryLiquidityPoolBatch(), 35 GetCmdQueryPoolBatchDepositMsgs(), 36 GetCmdQueryPoolBatchDepositMsg(), 37 GetCmdQueryPoolBatchWithdrawMsgs(), 38 GetCmdQueryPoolBatchWithdrawMsg(), 39 GetCmdQueryPoolBatchSwapMsgs(), 40 GetCmdQueryPoolBatchSwapMsg(), 41 ) 42 43 return liquidityQueryCmd 44 } 45 46 // GetCmdQueryParams implements the params query command. 47 func GetCmdQueryParams() *cobra.Command { 48 cmd := &cobra.Command{ 49 Use: "params", 50 Args: cobra.NoArgs, 51 Short: "Query the values set as liquidity parameters", 52 Long: strings.TrimSpace( 53 fmt.Sprintf(`Query values set as liquidity parameters. 54 55 Example: 56 $ %s query %s params 57 `, 58 version.AppName, types.ModuleName, 59 ), 60 ), 61 RunE: func(cmd *cobra.Command, args []string) error { 62 clientCtx, err := client.GetClientTxContext(cmd) 63 if err != nil { 64 return err 65 } 66 67 queryClient := types.NewQueryClient(clientCtx) 68 69 res, err := queryClient.Params( 70 context.Background(), 71 &types.QueryParamsRequest{}, 72 ) 73 if err != nil { 74 return err 75 } 76 77 return clientCtx.PrintProto(&res.Params) 78 }, 79 } 80 81 flags.AddQueryFlagsToCmd(cmd) 82 83 return cmd 84 } 85 86 func GetCmdQueryLiquidityPool() *cobra.Command { 87 cmd := &cobra.Command{ 88 Use: "pool [pool-id]", 89 Short: "Query details of a liquidity pool", 90 Long: strings.TrimSpace( 91 fmt.Sprintf(`Query details of a liquidity pool 92 Example: 93 $ %[1]s query %[2]s pool 1 94 95 Example (with pool coin denom): 96 $ %[1]s query %[2]s pool --pool-coin-denom=[denom] 97 98 Example (with reserve acc): 99 $ %[1]s query %[2]s pool --reserve-acc=[address] 100 `, 101 version.AppName, types.ModuleName, 102 ), 103 ), 104 RunE: func(cmd *cobra.Command, args []string) error { 105 var res *types.QueryLiquidityPoolResponse 106 clientCtx, err := client.GetClientTxContext(cmd) 107 if err != nil { 108 return err 109 } 110 111 foundArg := false 112 queryClient := types.NewQueryClient(clientCtx) 113 114 poolCoinDenom, _ := cmd.Flags().GetString(FlagPoolCoinDenom) 115 if poolCoinDenom != "" { 116 foundArg = true 117 res, err = queryClient.LiquidityPoolByPoolCoinDenom( 118 context.Background(), 119 &types.QueryLiquidityPoolByPoolCoinDenomRequest{PoolCoinDenom: poolCoinDenom}, 120 ) 121 if err != nil { 122 return err 123 } 124 } 125 126 reserveAcc, _ := cmd.Flags().GetString(FlagReserveAcc) 127 if !foundArg && reserveAcc != "" { 128 foundArg = true 129 res, err = queryClient.LiquidityPoolByReserveAcc( 130 context.Background(), 131 &types.QueryLiquidityPoolByReserveAccRequest{ReserveAcc: reserveAcc}, 132 ) 133 if err != nil { 134 return err 135 } 136 } 137 138 if !foundArg && len(args) > 0 { 139 poolID, err := strconv.ParseUint(args[0], 10, 64) 140 if err != nil { 141 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0]) 142 } 143 144 if poolID != 0 { 145 foundArg = true 146 res, err = queryClient.LiquidityPool( 147 context.Background(), 148 &types.QueryLiquidityPoolRequest{PoolId: poolID}, 149 ) 150 if err != nil { 151 return err 152 } 153 } 154 } 155 156 if !foundArg { 157 return fmt.Errorf("provide the pool-id argument or --%s or --%s flag", FlagPoolCoinDenom, FlagReserveAcc) 158 } 159 160 return clientCtx.PrintProto(res) 161 }, 162 } 163 cmd.Flags().AddFlagSet(flagSetPool()) 164 flags.AddQueryFlagsToCmd(cmd) 165 166 return cmd 167 } 168 169 func GetCmdQueryLiquidityPools() *cobra.Command { 170 cmd := &cobra.Command{ 171 Use: "pools", 172 Args: cobra.NoArgs, 173 Short: "Query for all liquidity pools", 174 Long: strings.TrimSpace( 175 fmt.Sprintf(`Query details about all liquidity pools on a network. 176 Example: 177 $ %s query %s pools 178 `, 179 version.AppName, types.ModuleName, 180 ), 181 ), 182 RunE: func(cmd *cobra.Command, args []string) error { 183 clientCtx, err := client.GetClientTxContext(cmd) 184 if err != nil { 185 return err 186 } 187 188 queryClient := types.NewQueryClient(clientCtx) 189 pageReq, err := client.ReadPageRequest(cmd.Flags()) 190 if err != nil { 191 return err 192 } 193 194 res, err := queryClient.LiquidityPools( 195 context.Background(), 196 &types.QueryLiquidityPoolsRequest{Pagination: pageReq}, 197 ) 198 if err != nil { 199 return err 200 } 201 202 return clientCtx.PrintProto(res) 203 }, 204 } 205 206 flags.AddQueryFlagsToCmd(cmd) 207 208 return cmd 209 } 210 211 func GetCmdQueryLiquidityPoolBatch() *cobra.Command { 212 cmd := &cobra.Command{ 213 Use: "batch [pool-id]", 214 Args: cobra.ExactArgs(1), 215 Short: "Query details of a liquidity pool batch", 216 Long: strings.TrimSpace( 217 fmt.Sprintf(`Query details of a liquidity pool batch 218 Example: 219 $ %s query %s batch 1 220 `, 221 version.AppName, types.ModuleName, 222 ), 223 ), 224 RunE: func(cmd *cobra.Command, args []string) error { 225 clientCtx, err := client.GetClientTxContext(cmd) 226 if err != nil { 227 return err 228 } 229 queryClient := types.NewQueryClient(clientCtx) 230 231 poolID, err := strconv.ParseUint(args[0], 10, 64) 232 if err != nil { 233 return fmt.Errorf("pool-id %s not a valid uint32, input a valid unsigned 32-bit integer pool-id", args[0]) 234 } 235 236 res, err := queryClient.LiquidityPoolBatch( 237 context.Background(), 238 &types.QueryLiquidityPoolBatchRequest{PoolId: poolID}, 239 ) 240 if err != nil { 241 return err 242 } 243 244 return clientCtx.PrintProto(res) 245 }, 246 } 247 248 flags.AddQueryFlagsToCmd(cmd) 249 return cmd 250 } 251 252 func GetCmdQueryPoolBatchDepositMsgs() *cobra.Command { 253 cmd := &cobra.Command{ 254 Use: "deposits [pool-id]", 255 Args: cobra.ExactArgs(1), 256 Short: "Query all deposit messages of the liquidity pool batch", 257 Long: strings.TrimSpace( 258 fmt.Sprintf(`Query all deposit messages of the liquidity pool batch on the specified pool 259 260 If batch messages are normally processed from the endblock, the resulting state is applied and the messages are removed in the beginning of next block. 261 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 262 263 Example: 264 $ %s query %s deposits 1 265 `, 266 version.AppName, types.ModuleName, 267 ), 268 ), 269 RunE: func(cmd *cobra.Command, args []string) error { 270 clientCtx, err := client.GetClientTxContext(cmd) 271 if err != nil { 272 return err 273 } 274 275 queryClient := types.NewQueryClient(clientCtx) 276 pageReq, err := client.ReadPageRequest(cmd.Flags()) 277 if err != nil { 278 return err 279 } 280 281 poolID, err := strconv.ParseUint(args[0], 10, 64) 282 if err != nil { 283 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer pool-id", args[0]) 284 } 285 286 res, err := queryClient.PoolBatchDepositMsgs( 287 context.Background(), 288 &types.QueryPoolBatchDepositMsgsRequest{ 289 PoolId: poolID, 290 Pagination: pageReq, 291 }, 292 ) 293 if err != nil { 294 return err 295 } 296 297 return clientCtx.PrintProto(res) 298 }, 299 } 300 301 flags.AddQueryFlagsToCmd(cmd) 302 303 return cmd 304 } 305 306 func GetCmdQueryPoolBatchDepositMsg() *cobra.Command { 307 cmd := &cobra.Command{ 308 Use: "deposit [pool-id] [msg-index]", 309 Args: cobra.ExactArgs(2), 310 Short: "Query the deposit messages on the liquidity pool batch", 311 Long: strings.TrimSpace( 312 fmt.Sprintf(`Query the deposit messages on the liquidity pool batch for the specified pool-id and msg-index 313 314 If batch messages are normally processed from the endblock, 315 the resulting state is applied and the messages are removed from the beginning of the next block. 316 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 317 318 Example: 319 $ %s query %s deposit 1 20 320 `, 321 version.AppName, types.ModuleName, 322 ), 323 ), 324 RunE: func(cmd *cobra.Command, args []string) error { 325 clientCtx, err := client.GetClientTxContext(cmd) 326 if err != nil { 327 return err 328 } 329 330 queryClient := types.NewQueryClient(clientCtx) 331 332 poolID, err := strconv.ParseUint(args[0], 10, 64) 333 if err != nil { 334 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0]) 335 } 336 337 msgIndex, err := strconv.ParseUint(args[1], 10, 64) 338 if err != nil { 339 return fmt.Errorf("msg-index %s not a valid uint, input a valid unsigned 32-bit integer for msg-index", args[1]) 340 } 341 342 res, err := queryClient.PoolBatchDepositMsg( 343 context.Background(), 344 &types.QueryPoolBatchDepositMsgRequest{ 345 PoolId: poolID, 346 MsgIndex: msgIndex, 347 }, 348 ) 349 if err != nil { 350 return err 351 } 352 353 return clientCtx.PrintProto(res) 354 }, 355 } 356 357 flags.AddQueryFlagsToCmd(cmd) 358 359 return cmd 360 } 361 362 func GetCmdQueryPoolBatchWithdrawMsgs() *cobra.Command { 363 cmd := &cobra.Command{ 364 Use: "withdraws [pool-id]", 365 Args: cobra.ExactArgs(1), 366 Short: "Query for all withdraw messages on the liquidity pool batch", 367 Long: strings.TrimSpace( 368 fmt.Sprintf(`Query all withdraw messages on the liquidity pool batch for the specified pool-id 369 370 If batch messages are normally processed from the endblock, 371 the resulting state is applied and the messages are removed in the beginning of next block. 372 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 373 374 Example: 375 $ %s query %s withdraws 1 376 `, 377 version.AppName, types.ModuleName, 378 ), 379 ), 380 RunE: func(cmd *cobra.Command, args []string) error { 381 clientCtx, err := client.GetClientTxContext(cmd) 382 if err != nil { 383 return err 384 } 385 386 queryClient := types.NewQueryClient(clientCtx) 387 pageReq, err := client.ReadPageRequest(cmd.Flags()) 388 if err != nil { 389 return err 390 } 391 392 poolID, err := strconv.ParseUint(args[0], 10, 64) 393 if err != nil { 394 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer pool-id", args[0]) 395 } 396 397 result, err := queryClient.PoolBatchWithdrawMsgs(context.Background(), &types.QueryPoolBatchWithdrawMsgsRequest{ 398 PoolId: poolID, Pagination: pageReq}) 399 if err != nil { 400 return err 401 } 402 return clientCtx.PrintProto(result) 403 }, 404 } 405 406 flags.AddQueryFlagsToCmd(cmd) 407 408 return cmd 409 } 410 411 func GetCmdQueryPoolBatchWithdrawMsg() *cobra.Command { 412 cmd := &cobra.Command{ 413 Use: "withdraw [pool-id] [msg-index]", 414 Args: cobra.ExactArgs(2), 415 Short: "Query the withdraw messages in the liquidity pool batch", 416 Long: strings.TrimSpace( 417 fmt.Sprintf(`Query the withdraw messages in the liquidity pool batch for the specified pool-id and msg-index 418 419 if the batch message are normally processed from the endblock, 420 the resulting state is applied and the messages are removed in the beginning of next block. 421 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 422 423 Example: 424 $ %s query %s withdraw 1 20 425 `, 426 version.AppName, types.ModuleName, 427 ), 428 ), 429 RunE: func(cmd *cobra.Command, args []string) error { 430 clientCtx, err := client.GetClientTxContext(cmd) 431 if err != nil { 432 return err 433 } 434 435 queryClient := types.NewQueryClient(clientCtx) 436 437 poolID, err := strconv.ParseUint(args[0], 10, 64) 438 if err != nil { 439 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer pool-id", args[0]) 440 } 441 442 msgIndex, err := strconv.ParseUint(args[1], 10, 64) 443 if err != nil { 444 return fmt.Errorf("msg-index %s not a valid uint, input a valid unsigned 32-bit integer msg-index", args[1]) 445 } 446 447 res, err := queryClient.PoolBatchWithdrawMsg( 448 context.Background(), 449 &types.QueryPoolBatchWithdrawMsgRequest{ 450 PoolId: poolID, 451 MsgIndex: msgIndex, 452 }, 453 ) 454 if err != nil { 455 return err 456 } 457 458 return clientCtx.PrintProto(res) 459 }, 460 } 461 462 flags.AddQueryFlagsToCmd(cmd) 463 464 return cmd 465 } 466 467 func GetCmdQueryPoolBatchSwapMsgs() *cobra.Command { 468 cmd := &cobra.Command{ 469 Use: "swaps [pool-id]", 470 Args: cobra.ExactArgs(1), 471 Short: "Query all swap messages in the liquidity pool batch", 472 Long: strings.TrimSpace( 473 fmt.Sprintf(`Query all swap messages in the liquidity pool batch for the specified pool-id 474 475 If batch messages are normally processed from the endblock, 476 the resulting state is applied and the messages are removed in the beginning of next block. 477 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 478 479 Example: 480 $ %s query %s swaps 1 481 `, 482 version.AppName, types.ModuleName, 483 ), 484 ), 485 RunE: func(cmd *cobra.Command, args []string) error { 486 clientCtx, err := client.GetClientTxContext(cmd) 487 if err != nil { 488 return err 489 } 490 491 queryClient := types.NewQueryClient(clientCtx) 492 pageReq, err := client.ReadPageRequest(cmd.Flags()) 493 if err != nil { 494 return err 495 } 496 497 poolID, err := strconv.ParseUint(args[0], 10, 64) 498 if err != nil { 499 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer pool-id", args[0]) 500 } 501 502 res, err := queryClient.PoolBatchSwapMsgs( 503 context.Background(), 504 &types.QueryPoolBatchSwapMsgsRequest{ 505 PoolId: poolID, 506 Pagination: pageReq, 507 }, 508 ) 509 if err != nil { 510 return err 511 } 512 513 return clientCtx.PrintProto(res) 514 }, 515 } 516 517 flags.AddQueryFlagsToCmd(cmd) 518 519 return cmd 520 } 521 522 func GetCmdQueryPoolBatchSwapMsg() *cobra.Command { 523 cmd := &cobra.Command{ 524 Use: "swap [pool-id] [msg-index]", 525 Args: cobra.ExactArgs(2), 526 Short: "Query for the swap message on the batch of the liquidity pool specified pool-id and msg-index", 527 Long: strings.TrimSpace( 528 fmt.Sprintf(`Query for the swap message on the batch of the liquidity pool specified pool-id and msg-index 529 530 If the batch message are normally processed and from the endblock, 531 the resulting state is applied and the messages are removed in the beginning of next block. 532 To query for past blocks, query the block height using the REST/gRPC API of a node that is not pruned. 533 534 Example: 535 $ %s query %s swap 1 20 536 `, 537 version.AppName, types.ModuleName, 538 ), 539 ), 540 RunE: func(cmd *cobra.Command, args []string) error { 541 clientCtx, err := client.GetClientTxContext(cmd) 542 if err != nil { 543 return err 544 } 545 546 queryClient := types.NewQueryClient(clientCtx) 547 548 poolID, err := strconv.ParseUint(args[0], 10, 64) 549 if err != nil { 550 return fmt.Errorf("pool-id %s not a valid uint, input a valid unsigned 32-bit integer for pool-id", args[0]) 551 } 552 553 msgIndex, err := strconv.ParseUint(args[1], 10, 64) 554 if err != nil { 555 return fmt.Errorf("msg-index %s not a valid uint, input a valid unsigned 32-bit integer for msg-index", args[1]) 556 } 557 558 res, err := queryClient.PoolBatchSwapMsg( 559 context.Background(), 560 &types.QueryPoolBatchSwapMsgRequest{ 561 PoolId: poolID, 562 MsgIndex: msgIndex, 563 }, 564 ) 565 if err != nil { 566 return err 567 } 568 569 return clientCtx.PrintProto(res) 570 }, 571 } 572 573 flags.AddQueryFlagsToCmd(cmd) 574 575 return cmd 576 }