Skip to content

Commit

Permalink
add Query demand orders by status cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangdv2429 committed Mar 7, 2024
1 parent 1784784 commit f80c97d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/eibc/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListDemandOrdersByStatus())

Check warning on line 28 in x/eibc/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/client/cli/query.go#L28

Added line #L28 was not covered by tests
// this line is used by starport scaffolding # 1

return cmd
Expand Down
38 changes: 38 additions & 0 deletions x/eibc/client/cli/query_command_orders.go
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 29 in x/eibc/client/cli/query_command_orders.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/client/cli/query_command_orders.go#L12-L29

Added lines #L12 - L29 were not covered by tests

return clientCtx.PrintProto(res)

Check warning on line 31 in x/eibc/client/cli/query_command_orders.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/client/cli/query_command_orders.go#L31

Added line #L31 was not covered by tests
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd

Check warning on line 37 in x/eibc/client/cli/query_command_orders.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/client/cli/query_command_orders.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}
34 changes: 34 additions & 0 deletions x/eibc/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Check warning on line 61 in x/eibc/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/keeper/grpc_query.go#L55-L61

Added lines #L55 - L61 were not covered by tests

// 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)

Check warning on line 73 in x/eibc/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/keeper/grpc_query.go#L64-L73

Added lines #L64 - L73 were not covered by tests
}
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())
}

Check warning on line 81 in x/eibc/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/keeper/grpc_query.go#L75-L81

Added lines #L75 - L81 were not covered by tests

// Construct the response
return &types.QueryDemandOrdersByStatusResponse{DemandOrders: demandOrders}, nil

Check warning on line 84 in x/eibc/keeper/grpc_query.go

View check run for this annotation

Codecov / codecov/patch

x/eibc/keeper/grpc_query.go#L84

Added line #L84 was not covered by tests
}

0 comments on commit f80c97d

Please sign in to comment.