From 48daa6ce58648e76187811e81fc7baa036ca37c3 Mon Sep 17 00:00:00 2001 From: TimmyExogenous Date: Tue, 15 Oct 2024 15:59:48 +0800 Subject: [PATCH] remove the redundent code regarding the command line of operator module --- x/operator/client/cli/query.go | 91 +++++++++++++++------------------- 1 file changed, 40 insertions(+), 51 deletions(-) diff --git a/x/operator/client/cli/query.go b/x/operator/client/cli/query.go index aa63d5e7c..4d273d784 100644 --- a/x/operator/client/cli/query.go +++ b/x/operator/client/cli/query.go @@ -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{ @@ -254,34 +278,22 @@ func GetAllOperatorConsAddrs() *cobra.Command { func QueryOperatorUSDValue() *cobra.Command { cmd := &cobra.Command{ Use: "QueryOperatorUSDValue ", - 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) }, } @@ -329,14 +341,7 @@ 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 } @@ -344,19 +349,15 @@ func QueryOperatorSlashInfo() *cobra.Command { 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) }, } @@ -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) }, }