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

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  
     8  	"github.com/gorilla/mux"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    13  	"github.com/fibonacci-chain/fbc/x/common"
    14  	"github.com/fibonacci-chain/fbc/x/staking/types"
    15  )
    16  
    17  func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    18  	// query delegator info
    19  	r.HandleFunc(
    20  		"/staking/delegators/{delegatorAddr}",
    21  		delegatorHandlerFn(cliCtx),
    22  	).Methods("GET")
    23  
    24  	// query delegator's unbonding delegation
    25  	r.HandleFunc(
    26  		"/staking/delegators/{delegatorAddr}/unbonding_delegations",
    27  		delegatorUnbondingDelegationsHandlerFn(cliCtx),
    28  	).Methods("GET")
    29  
    30  	// Query all validators that a delegator is bonded to
    31  	r.HandleFunc(
    32  		"/staking/delegators/{delegatorAddr}/validators",
    33  		delegatorValidatorsHandlerFn(cliCtx),
    34  	).Methods("GET")
    35  
    36  	// Query a validator that a delegator is bonded to
    37  	r.HandleFunc(
    38  		"/staking/delegators/{delegatorAddr}/validators/{validatorAddr}",
    39  		delegatorValidatorHandlerFn(cliCtx),
    40  	).Methods("GET")
    41  
    42  	// Get all delegations to a validator
    43  	r.HandleFunc(
    44  		"/staking/validators/{validatorAddr}/delegations",
    45  		validatorDelegationsHandlerFn(cliCtx),
    46  	).Methods("GET")
    47  
    48  	// Queries delegate info for given validator delegator pair
    49  	r.HandleFunc(
    50  		"/staking/validators/{validatorAddr}/delegations/{delegatorAddr}",
    51  		delegationHandlerFn(cliCtx),
    52  	).Methods("GET")
    53  
    54  	// query the proxy relationship on a proxy delegator
    55  	r.HandleFunc(
    56  		"/staking/delegators/{delegatorAddr}/proxy",
    57  		delegatorProxyHandlerFn(cliCtx),
    58  	).Methods("GET")
    59  
    60  	// query the all shares on a validator
    61  	r.HandleFunc(
    62  		"/staking/validators/{validatorAddr}/shares",
    63  		validatorAllSharesHandlerFn(cliCtx),
    64  	).Methods("GET")
    65  
    66  	// get all validators
    67  	r.HandleFunc(
    68  		"/staking/validators",
    69  		validatorsHandlerFn(cliCtx),
    70  	).Methods("GET")
    71  
    72  	// get a single validator info
    73  	r.HandleFunc(
    74  		"/staking/validators/{validatorAddr}",
    75  		validatorHandlerFn(cliCtx),
    76  	).Methods("GET")
    77  
    78  	// Get HistoricalInfo at a given height
    79  	r.HandleFunc(
    80  		"/staking/historical_info/{height}",
    81  		historicalInfoHandlerFn(cliCtx),
    82  	).Methods("GET")
    83  
    84  	// get the current state of the staking pool
    85  	r.HandleFunc(
    86  		"/staking/pool",
    87  		poolHandlerFn(cliCtx),
    88  	).Methods("GET")
    89  
    90  	// get the current staking parameter values
    91  	r.HandleFunc(
    92  		"/staking/parameters",
    93  		paramsHandlerFn(cliCtx),
    94  	).Methods("GET")
    95  
    96  	// get the current staking address values
    97  	r.HandleFunc(
    98  		"/staking/address",
    99  		addressHandlerFn(cliCtx),
   100  	).Methods("GET")
   101  
   102  	// get the current staking address values
   103  	r.HandleFunc(
   104  		"/staking/address/{validatorAddr}/validator_address",
   105  		validatorAddressHandlerFn(cliCtx),
   106  	).Methods("GET")
   107  
   108  	// get the current staking address values
   109  	r.HandleFunc(
   110  		"/staking/address/{validatorAddr}/account_address",
   111  		accountAddressHandlerFn(cliCtx),
   112  	).Methods("GET")
   113  
   114  }
   115  
   116  // HTTP request handler to query all delegator bonded validators
   117  func delegatorValidatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   118  	return queryDelegator(cliCtx, "custom/staking/delegatorValidators")
   119  }
   120  
   121  // HTTP request handler to query a delegation
   122  func delegationHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   123  	return queryBondsInfo(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorDelegator))
   124  }
   125  
   126  // HTTP request handler to query the proxy relationship on a proxy delegator
   127  func delegatorProxyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   128  	return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryProxy))
   129  }
   130  
   131  // HTTP request handler to get information from a currently bonded validator
   132  func delegatorValidatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   133  	return queryBondsInfo(cliCtx, "custom/staking/delegatorValidator")
   134  }
   135  
   136  // HTTP request handler to query all unbonding delegations from a validator
   137  func validatorDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   138  	return queryValidator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorDelegations))
   139  }
   140  
   141  // HTTP request handler to query the info of delegator's unbonding delegation
   142  func delegatorUnbondingDelegationsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   143  	return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryUnbondingDelegation))
   144  }
   145  
   146  func delegatorUnbondingDelegationsHandlerFn2(cliCtx context.CLIContext) http.HandlerFunc {
   147  	return queryBonds(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryUnbondingDelegation2))
   148  }
   149  
   150  // HTTP request handler to query the info of a delegator
   151  func delegatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   152  	return queryDelegator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegator))
   153  }
   154  
   155  // HTTP request handler to query the all shares added to a validator
   156  func validatorAllSharesHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   157  	return queryValidator(cliCtx, fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorAllShares))
   158  }
   159  
   160  // HTTP request handler to query historical info at a given height
   161  func historicalInfoHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   162  	return func(w http.ResponseWriter, r *http.Request) {
   163  		cliCtx := cliCtx
   164  		vars := mux.Vars(r)
   165  		heightStr := vars["height"]
   166  		height, err := strconv.ParseInt(heightStr, 10, 64)
   167  		if err != nil || height < 0 {
   168  			common.HandleErrorMsg(w, cliCtx, common.CodeInternalError, fmt.Sprintf("Must provide non-negative integer for height: %v", err))
   169  			return
   170  		}
   171  
   172  		params := types.NewQueryHistoricalInfoParams(height)
   173  		bz, err := cliCtx.Codec.MarshalJSON(params)
   174  		if err != nil {
   175  			common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error())
   176  			return
   177  		}
   178  
   179  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryHistoricalInfo)
   180  		res, height, err := cliCtx.QueryWithData(route, bz)
   181  		if err != nil {
   182  			sdkErr := common.ParseSDKError(err.Error())
   183  			common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   184  			return
   185  		}
   186  
   187  		cliCtx = cliCtx.WithHeight(height)
   188  		rest.PostProcessResponse(w, cliCtx, res)
   189  	}
   190  }
   191  
   192  // HTTP request handler to query list of validators
   193  func validatorsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   194  	return func(w http.ResponseWriter, r *http.Request) {
   195  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
   196  		if err != nil {
   197  			common.HandleErrorMsg(w, cliCtx, common.CodeArgsWithLimit, err.Error())
   198  			return
   199  		}
   200  
   201  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   202  		if !ok {
   203  			return
   204  		}
   205  
   206  		status := r.FormValue("status")
   207  		if status == "" {
   208  			status = sdk.BondStatusBonded
   209  		}
   210  
   211  		params := types.NewQueryValidatorsParams(page, limit, status)
   212  		bz, err := cliCtx.Codec.MarshalJSON(params)
   213  		if err != nil {
   214  			common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error())
   215  			return
   216  		}
   217  
   218  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidators)
   219  		res, height, err := cliCtx.QueryWithData(route, bz)
   220  		if err != nil {
   221  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   222  			return
   223  		}
   224  
   225  		cliCtx = cliCtx.WithHeight(height)
   226  		rest.PostProcessResponse(w, cliCtx, res)
   227  	}
   228  }
   229  
   230  // HTTP request handler to query the validator information from a given validator address
   231  func validatorHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   232  	return queryValidator(cliCtx, "custom/staking/validator")
   233  }
   234  
   235  // HTTP request handler to query the pool information
   236  func poolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   237  	return func(w http.ResponseWriter, r *http.Request) {
   238  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   239  		if !ok {
   240  			return
   241  		}
   242  
   243  		res, height, err := cliCtx.QueryWithData("custom/staking/pool", nil)
   244  		if err != nil {
   245  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   246  			return
   247  		}
   248  
   249  		cliCtx = cliCtx.WithHeight(height)
   250  		rest.PostProcessResponse(w, cliCtx, res)
   251  	}
   252  }
   253  
   254  // HTTP request handler to query the staking params values
   255  func paramsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   256  	return func(w http.ResponseWriter, r *http.Request) {
   257  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   258  		if !ok {
   259  			return
   260  		}
   261  
   262  		res, height, err := cliCtx.QueryWithData("custom/staking/parameters", nil)
   263  		if err != nil {
   264  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   265  			return
   266  		}
   267  
   268  		cliCtx = cliCtx.WithHeight(height)
   269  		rest.PostProcessResponse(w, cliCtx, res)
   270  	}
   271  }
   272  
   273  func addressHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   274  	return func(w http.ResponseWriter, r *http.Request) {
   275  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   276  		if !ok {
   277  			return
   278  		}
   279  
   280  		res, height, err := cliCtx.QueryWithData("custom/staking/address", nil)
   281  		if err != nil {
   282  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   283  			return
   284  		}
   285  		cliCtx = cliCtx.WithHeight(height)
   286  		rest.PostProcessResponse(w, cliCtx, res)
   287  	}
   288  }
   289  
   290  // HTTP request handler to query one validator address
   291  func validatorAddressHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   292  	return queryValidatorAddr(cliCtx, "custom/staking/validatorAddress")
   293  }
   294  
   295  // HTTP request handler to query one validator's account address
   296  func accountAddressHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   297  	return queryValidatorAddr(cliCtx, "custom/staking/validatorAccAddress")
   298  }
   299  
   300  func queryValidatorAddr(cliCtx context.CLIContext, endpoint string) http.HandlerFunc {
   301  	return func(w http.ResponseWriter, r *http.Request) {
   302  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   303  		if !ok {
   304  			return
   305  		}
   306  
   307  		validatorAddr := mux.Vars(r)["validatorAddr"]
   308  
   309  		res, height, err := cliCtx.QueryWithData(endpoint, []byte(validatorAddr))
   310  		if err != nil {
   311  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   312  			return
   313  		}
   314  		cliCtx = cliCtx.WithHeight(height)
   315  		rest.PostProcessResponse(w, cliCtx, res)
   316  	}
   317  }