github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/slashing/client/rest/query.go (about)

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/gorilla/mux"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/slashing/internal/types"
    13  )
    14  
    15  func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    16  	r.HandleFunc(
    17  		"/slashing/validators/{validatorPubKey}/signing_info",
    18  		signingInfoHandlerFn(cliCtx),
    19  	).Methods("GET")
    20  
    21  	r.HandleFunc(
    22  		"/slashing/signing_infos",
    23  		signingInfoHandlerListFn(cliCtx),
    24  	).Methods("GET")
    25  
    26  	r.HandleFunc(
    27  		"/slashing/parameters",
    28  		queryParamsHandlerFn(cliCtx),
    29  	).Methods("GET")
    30  }
    31  
    32  // http request handler to query signing info
    33  func signingInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    34  	return func(w http.ResponseWriter, r *http.Request) {
    35  		vars := mux.Vars(r)
    36  		pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, vars["validatorPubKey"])
    37  		if err != nil {
    38  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    39  			return
    40  		}
    41  
    42  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    43  		if !ok {
    44  			return
    45  		}
    46  
    47  		params := types.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address()))
    48  
    49  		bz, err := cliCtx.Codec.MarshalJSON(params)
    50  		if err != nil {
    51  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    52  			return
    53  		}
    54  
    55  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfo)
    56  		res, height, err := cliCtx.QueryWithData(route, bz)
    57  		if err != nil {
    58  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    59  			return
    60  		}
    61  
    62  		cliCtx = cliCtx.WithHeight(height)
    63  		rest.PostProcessResponse(w, cliCtx, res)
    64  	}
    65  }
    66  
    67  // http request handler to query signing info
    68  func signingInfoHandlerListFn(cliCtx context.CLIContext) http.HandlerFunc {
    69  	return func(w http.ResponseWriter, r *http.Request) {
    70  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
    71  		if err != nil {
    72  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    73  			return
    74  		}
    75  
    76  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    77  		if !ok {
    78  			return
    79  		}
    80  
    81  		params := types.NewQuerySigningInfosParams(page, limit)
    82  		bz, err := cliCtx.Codec.MarshalJSON(params)
    83  		if err != nil {
    84  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    85  			return
    86  		}
    87  
    88  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySigningInfos)
    89  		res, height, err := cliCtx.QueryWithData(route, bz)
    90  		if err != nil {
    91  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    92  			return
    93  		}
    94  
    95  		cliCtx = cliCtx.WithHeight(height)
    96  		rest.PostProcessResponse(w, cliCtx, res)
    97  	}
    98  }
    99  
   100  func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   101  	return func(w http.ResponseWriter, r *http.Request) {
   102  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   103  		if !ok {
   104  			return
   105  		}
   106  
   107  		route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute)
   108  
   109  		res, height, err := cliCtx.QueryWithData(route, nil)
   110  		if err != nil {
   111  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   112  			return
   113  		}
   114  
   115  		cliCtx = cliCtx.WithHeight(height)
   116  		rest.PostProcessResponse(w, cliCtx, res)
   117  	}
   118  }