From f80c97dca0c0818e179aef5f4fca4a4b3d726245 Mon Sep 17 00:00:00 2001 From: hoangdv2429 Date: Thu, 7 Mar 2024 17:58:00 +0700 Subject: [PATCH] add Query demand orders by status cmd --- x/eibc/client/cli/query.go | 1 + x/eibc/client/cli/query_command_orders.go | 38 +++++++++++++++++++++++ x/eibc/keeper/grpc_query.go | 34 ++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 x/eibc/client/cli/query_command_orders.go diff --git a/x/eibc/client/cli/query.go b/x/eibc/client/cli/query.go index 8d508667c..fd9b3c2a2 100644 --- a/x/eibc/client/cli/query.go +++ b/x/eibc/client/cli/query.go @@ -25,6 +25,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command { } cmd.AddCommand(CmdQueryParams()) + cmd.AddCommand(CmdListDemandOrdersByStatus()) // this line is used by starport scaffolding # 1 return cmd diff --git a/x/eibc/client/cli/query_command_orders.go b/x/eibc/client/cli/query_command_orders.go new file mode 100644 index 000000000..f1e35441a --- /dev/null +++ b/x/eibc/client/cli/query_command_orders.go @@ -0,0 +1,38 @@ +package cli + +import ( + "context" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/dymensionxyz/dymension/v3/x/eibc/types" + "github.com/spf13/cobra" +) + +func CmdListDemandOrdersByStatus() *cobra.Command { + cmd := &cobra.Command{ + Use: "list-demand-orders [status]", + Short: "List all demand orders with a specific status", + Long: `Query demand orders filtered by status. Examples of status include "pending", "finalized", and "reverted".`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := client.GetClientContextFromCmd(cmd) + status := args[0] + + queryClient := types.NewQueryClient(clientCtx) + res, err := queryClient.DemandOrdersByStatus(context.Background(), &types.QueryDemandOrdersByStatusRequest{ + Status: status, + }) + + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/eibc/keeper/grpc_query.go b/x/eibc/keeper/grpc_query.go index 549285b83..673e95d78 100644 --- a/x/eibc/keeper/grpc_query.go +++ b/x/eibc/keeper/grpc_query.go @@ -2,6 +2,8 @@ package keeper import ( "context" + "fmt" + "strings" sdk "github.com/cosmos/cosmos-sdk/types" commontypes "github.com/dymensionxyz/dymension/v3/x/common/types" @@ -49,3 +51,35 @@ func (q Querier) DemandOrderById(goCtx context.Context, req *types.QueryGetDeman } return nil, status.Error(codes.Internal, err.Error()) } + +func (q Querier) DemandOrdersByStatus(goCtx context.Context, req *types.QueryDemandOrdersByStatusRequest) (*types.QueryDemandOrdersByStatusResponse, error) { + if req == nil { + return nil, status.Error(codes.InvalidArgument, "empty request") + } + if req.Status == "" { + return nil, status.Error(codes.InvalidArgument, "status must be provided") + } + + // Convert string status to commontypes.Status + var statusValue commontypes.Status + switch strings.ToUpper(req.Status) { + case "PENDING": + statusValue = commontypes.Status_PENDING + case "FINALIZED": + statusValue = commontypes.Status_FINALIZED + case "REVERTED": + statusValue = commontypes.Status_REVERTED + default: + return nil, fmt.Errorf("invalid demand order status: %s", req.Status) + } + ctx := sdk.UnwrapSDKContext(goCtx) + + // Get the demand orders by status + demandOrders, err := q.ListDemandOrdersByStatus(ctx, statusValue) + if err != nil { + return nil, status.Error(codes.Internal, err.Error()) + } + + // Construct the response + return &types.QueryDemandOrdersByStatusResponse{DemandOrders: demandOrders}, nil +}