Skip to content

Commit

Permalink
remove the redundent code regarding the command line of operator module
Browse files Browse the repository at this point in the history
  • Loading branch information
TimmyExogenous committed Oct 15, 2024
1 parent c5a3779 commit 48daa6c
Showing 1 changed file with 40 additions and 51 deletions.
91 changes: 40 additions & 51 deletions x/operator/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ import (
"github.com/spf13/cobra"
)

type GenericQueryParams struct {
queryClient operatortypes.QueryClient
clientCtx client.Context
}

func ValidOperatorAVSAddr(cmd *cobra.Command, originalOperatorAddr, originalAVSAddr string) (*operatortypes.OperatorAVSAddress, *GenericQueryParams, error) {
_, err := sdk.AccAddressFromBech32(originalOperatorAddr)
if err != nil {
return nil, nil, xerrors.Errorf("invalid operator address,err:%s", err.Error())
}
if !common.IsHexAddress(originalAVSAddr) {
return nil, nil, xerrors.Errorf("invalid avs address,err:%s", types.ErrInvalidAddr)
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return nil, nil, err
}
queryClient := operatortypes.NewQueryClient(clientCtx)
return &operatortypes.OperatorAVSAddress{
OperatorAddr: originalOperatorAddr,
AvsAddress: strings.ToLower(originalAVSAddr),
}, &GenericQueryParams{queryClient: queryClient, clientCtx: clientCtx}, nil
}

// GetQueryCmd returns the parent command for all incentives CLI query commands.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -254,34 +278,22 @@ func GetAllOperatorConsAddrs() *cobra.Command {
func QueryOperatorUSDValue() *cobra.Command {
cmd := &cobra.Command{
Use: "QueryOperatorUSDValue <operatorAddr> <avsAddr>",
Short: "Get the opted-in USD value for the operator",
Short: "Get the opted-in USD value",
Long: "Get the opted-in USD value for the operator",
Example: "exocored query operator QueryOperatorUSDValue exo1c5x7mxphvgavjhu0au9jjqnfqcyspevtyy27mz 0xaa089ba103f765fcea44808bd3d4073523254c57",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return xerrors.Errorf("invalid operator address,err:%s", err.Error())
}
if !common.IsHexAddress(args[1]) {
return xerrors.Errorf("invalid avs address,err:%s", types.ErrInvalidAddr)
}
clientCtx, err := client.GetClientQueryContext(cmd)
validOperatorAVSAddr, genericQueryParams, err := ValidOperatorAVSAddr(cmd, args[0], args[1])
if err != nil {
return err
}
queryClient := operatortypes.NewQueryClient(clientCtx)
req := &operatortypes.QueryOperatorUSDValueRequest{
OperatorAVSAddress: &operatortypes.OperatorAVSAddress{
OperatorAddr: args[0],
AvsAddress: strings.ToLower(args[1]),
},
}
res, err := queryClient.QueryOperatorUSDValue(context.Background(), req)
res, err := genericQueryParams.queryClient.QueryOperatorUSDValue(context.Background(), &operatortypes.QueryOperatorUSDValueRequest{
OperatorAVSAddress: validOperatorAVSAddr,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
return genericQueryParams.clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -329,34 +341,23 @@ func QueryOperatorSlashInfo() *cobra.Command {
Example: "exocored query operator QueryOperatorSlashInfo exo1c5x7mxphvgavjhu0au9jjqnfqcyspevtyy27mz 0xaa089ba103f765fcea44808bd3d4073523254c57",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return xerrors.Errorf("invalid operator address,err:%s", err.Error())
}
if !common.IsHexAddress(args[1]) {
return xerrors.Errorf("invalid avs address,err:%s", types.ErrInvalidAddr)
}
clientCtx, err := client.GetClientQueryContext(cmd)
validOperatorAVSAddr, genericQueryParams, err := ValidOperatorAVSAddr(cmd, args[0], args[1])
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := operatortypes.NewQueryClient(clientCtx)
req := &operatortypes.QueryOperatorSlashInfoRequest{
OperatorAVSAddress: &operatortypes.OperatorAVSAddress{
OperatorAddr: args[0],
AvsAddress: strings.ToLower(args[1]),
},
Pagination: pageReq,
OperatorAVSAddress: validOperatorAVSAddr,
Pagination: pageReq,
}
res, err := queryClient.QueryOperatorSlashInfo(context.Background(), req)
res, err := genericQueryParams.queryClient.QueryOperatorSlashInfo(context.Background(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
return genericQueryParams.clientCtx.PrintProto(res)
},
}

Expand Down Expand Up @@ -438,29 +439,17 @@ func GetOptInfo() *cobra.Command {
Example: "exocored query operator GetOptInfo exo1c5x7mxphvgavjhu0au9jjqnfqcyspevtyy27mz 0xaa089ba103f765fcea44808bd3d4073523254c57",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return xerrors.Errorf("invalid operator address,err:%s", err.Error())
}
if !common.IsHexAddress(args[1]) {
return xerrors.Errorf("invalid avs address,err:%s", types.ErrInvalidAddr)
}
clientCtx, err := client.GetClientQueryContext(cmd)
validOperatorAVSAddr, genericQueryParams, err := ValidOperatorAVSAddr(cmd, args[0], args[1])
if err != nil {
return err
}
queryClient := operatortypes.NewQueryClient(clientCtx)
req := &operatortypes.QueryOptInfoRequest{
OperatorAVSAddress: &operatortypes.OperatorAVSAddress{
OperatorAddr: args[0],
AvsAddress: strings.ToLower(args[1]),
},
}
res, err := queryClient.QueryOptInfo(context.Background(), req)
res, err := genericQueryParams.queryClient.QueryOptInfo(context.Background(), &operatortypes.QueryOptInfoRequest{
OperatorAVSAddress: validOperatorAVSAddr,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
return genericQueryParams.clientCtx.PrintProto(res)
},
}

Expand Down

0 comments on commit 48daa6c

Please sign in to comment.