github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/distribution/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/x/distribution/client/common"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/types"
    11  
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    13  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    15  )
    16  
    17  func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string) {
    18  	// Get the total rewards balance from all delegations
    19  	r.HandleFunc(
    20  		"/distribution/delegators/{delegatorAddr}/rewards",
    21  		delegatorRewardsHandlerFn(cliCtx, queryRoute),
    22  	).Methods("GET")
    23  
    24  	// Query a delegation reward
    25  	r.HandleFunc(
    26  		"/distribution/delegators/{delegatorAddr}/rewards/{validatorAddr}",
    27  		delegationRewardsHandlerFn(cliCtx, queryRoute),
    28  	).Methods("GET")
    29  
    30  	// Get the rewards withdrawal address
    31  	r.HandleFunc(
    32  		"/distribution/delegators/{delegatorAddr}/withdraw_address",
    33  		delegatorWithdrawalAddrHandlerFn(cliCtx, queryRoute),
    34  	).Methods("GET")
    35  
    36  	// Validator distribution information
    37  	r.HandleFunc(
    38  		"/distribution/validators/{validatorAddr}",
    39  		validatorInfoHandlerFn(cliCtx, queryRoute),
    40  	).Methods("GET")
    41  
    42  	// Commission and self-delegation rewards of a single a validator
    43  	r.HandleFunc(
    44  		"/distribution/validators/{validatorAddr}/rewards",
    45  		validatorRewardsHandlerFn(cliCtx, queryRoute),
    46  	).Methods("GET")
    47  
    48  	// Outstanding rewards of a single validator
    49  	r.HandleFunc(
    50  		"/distribution/validators/{validatorAddr}/outstanding_rewards",
    51  		outstandingRewardsHandlerFn(cliCtx, queryRoute),
    52  	).Methods("GET")
    53  
    54  	// Get the current distribution parameter values
    55  	r.HandleFunc(
    56  		"/distribution/parameters",
    57  		paramsHandlerFn(cliCtx, queryRoute),
    58  	).Methods("GET")
    59  
    60  	// Get the amount held in the community pool
    61  	r.HandleFunc(
    62  		"/distribution/community_pool",
    63  		communityPoolHandler(cliCtx, queryRoute),
    64  	).Methods("GET")
    65  
    66  }
    67  
    68  // HTTP request handler to query the total rewards balance from all delegations
    69  func delegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
    70  	return func(w http.ResponseWriter, r *http.Request) {
    71  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    72  		if !ok {
    73  			return
    74  		}
    75  
    76  		delegatorAddr, ok := checkDelegatorAddressVar(w, r)
    77  		if !ok {
    78  			return
    79  		}
    80  
    81  		params := types.NewQueryDelegatorParams(delegatorAddr)
    82  		bz, err := cliCtx.Codec.MarshalJSON(params)
    83  		if err != nil {
    84  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal params: %s", err))
    85  			return
    86  		}
    87  
    88  		route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards)
    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  // HTTP request handler to query a delegation rewards
   101  func delegationRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   102  	return func(w http.ResponseWriter, r *http.Request) {
   103  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   104  		if !ok {
   105  			return
   106  		}
   107  
   108  		delAddr := mux.Vars(r)["delegatorAddr"]
   109  		valAddr := mux.Vars(r)["validatorAddr"]
   110  
   111  		// query for rewards from a particular delegation
   112  		res, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
   113  		if !ok {
   114  			return
   115  		}
   116  
   117  		cliCtx = cliCtx.WithHeight(height)
   118  		rest.PostProcessResponse(w, cliCtx, res)
   119  	}
   120  }
   121  
   122  // HTTP request handler to query a delegation rewards
   123  func delegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   124  	return func(w http.ResponseWriter, r *http.Request) {
   125  		delegatorAddr, ok := checkDelegatorAddressVar(w, r)
   126  		if !ok {
   127  			return
   128  		}
   129  
   130  		cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   131  		if !ok {
   132  			return
   133  		}
   134  
   135  		bz := cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr))
   136  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", queryRoute), bz)
   137  		if err != nil {
   138  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   139  			return
   140  		}
   141  
   142  		cliCtx = cliCtx.WithHeight(height)
   143  		rest.PostProcessResponse(w, cliCtx, res)
   144  	}
   145  }
   146  
   147  // ValidatorDistInfo defines the properties of
   148  // validator distribution information response.
   149  type ValidatorDistInfo struct {
   150  	OperatorAddress     sdk.AccAddress                       `json:"operator_address" yaml:"operator_address"`
   151  	SelfBondRewards     sdk.DecCoins                         `json:"self_bond_rewards" yaml:"self_bond_rewards"`
   152  	ValidatorCommission types.ValidatorAccumulatedCommission `json:"val_commission" yaml:"val_commission"`
   153  }
   154  
   155  // NewValidatorDistInfo creates a new instance of ValidatorDistInfo.
   156  func NewValidatorDistInfo(operatorAddr sdk.AccAddress, rewards sdk.DecCoins,
   157  	commission types.ValidatorAccumulatedCommission) ValidatorDistInfo {
   158  	return ValidatorDistInfo{
   159  		OperatorAddress:     operatorAddr,
   160  		SelfBondRewards:     rewards,
   161  		ValidatorCommission: commission,
   162  	}
   163  }
   164  
   165  // HTTP request handler to query validator's distribution information
   166  func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   167  	return func(w http.ResponseWriter, r *http.Request) {
   168  		valAddr, ok := checkValidatorAddressVar(w, r)
   169  		if !ok {
   170  			return
   171  		}
   172  
   173  		cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   174  		if !ok {
   175  			return
   176  		}
   177  
   178  		// query commission
   179  		bz, err := common.QueryValidatorCommission(cliCtx, queryRoute, valAddr)
   180  		if err != nil {
   181  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   182  			return
   183  		}
   184  
   185  		var commission types.ValidatorAccumulatedCommission
   186  		if err := cliCtx.Codec.UnmarshalJSON(bz, &commission); err != nil {
   187  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   188  			return
   189  		}
   190  
   191  		// self bond rewards
   192  		delAddr := sdk.AccAddress(valAddr)
   193  		bz, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr.String(), valAddr.String())
   194  		if !ok {
   195  			return
   196  		}
   197  
   198  		var rewards sdk.DecCoins
   199  		if err := cliCtx.Codec.UnmarshalJSON(bz, &rewards); err != nil {
   200  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   201  			return
   202  		}
   203  
   204  		bz, err = cliCtx.Codec.MarshalJSON(NewValidatorDistInfo(delAddr, rewards, commission))
   205  		if err != nil {
   206  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   207  			return
   208  		}
   209  
   210  		cliCtx = cliCtx.WithHeight(height)
   211  		rest.PostProcessResponse(w, cliCtx, bz)
   212  	}
   213  }
   214  
   215  // HTTP request handler to query validator's commission and self-delegation rewards
   216  func validatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   217  	return func(w http.ResponseWriter, r *http.Request) {
   218  		valAddr := mux.Vars(r)["validatorAddr"]
   219  		validatorAddr, ok := checkValidatorAddressVar(w, r)
   220  		if !ok {
   221  			return
   222  		}
   223  
   224  		cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   225  		if !ok {
   226  			return
   227  		}
   228  
   229  		delAddr := sdk.AccAddress(validatorAddr).String()
   230  		bz, height, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
   231  		if !ok {
   232  			return
   233  		}
   234  
   235  		cliCtx = cliCtx.WithHeight(height)
   236  		rest.PostProcessResponse(w, cliCtx, bz)
   237  	}
   238  }
   239  
   240  // HTTP request handler to query the distribution params values
   241  func paramsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   242  	return func(w http.ResponseWriter, r *http.Request) {
   243  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   244  		if !ok {
   245  			return
   246  		}
   247  
   248  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
   249  		res, height, err := cliCtx.QueryWithData(route, nil)
   250  		if err != nil {
   251  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   252  			return
   253  		}
   254  
   255  		cliCtx = cliCtx.WithHeight(height)
   256  		rest.PostProcessResponse(w, cliCtx, res)
   257  	}
   258  }
   259  
   260  func communityPoolHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   261  	return func(w http.ResponseWriter, r *http.Request) {
   262  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   263  		if !ok {
   264  			return
   265  		}
   266  
   267  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
   268  		if err != nil {
   269  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   270  			return
   271  		}
   272  
   273  		var result sdk.DecCoins
   274  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   275  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   276  			return
   277  		}
   278  
   279  		cliCtx = cliCtx.WithHeight(height)
   280  		rest.PostProcessResponse(w, cliCtx, result)
   281  	}
   282  }
   283  
   284  // HTTP request handler to query the outstanding rewards
   285  func outstandingRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
   286  	return func(w http.ResponseWriter, r *http.Request) {
   287  		validatorAddr, ok := checkValidatorAddressVar(w, r)
   288  		if !ok {
   289  			return
   290  		}
   291  
   292  		cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   293  		if !ok {
   294  			return
   295  		}
   296  
   297  		bin := cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
   298  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
   299  		if err != nil {
   300  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   301  			return
   302  		}
   303  
   304  		cliCtx = cliCtx.WithHeight(height)
   305  		rest.PostProcessResponse(w, cliCtx, res)
   306  	}
   307  }
   308  
   309  func checkResponseQueryDelegationRewards(
   310  	w http.ResponseWriter, cliCtx context.CLIContext, queryRoute, delAddr, valAddr string,
   311  ) (res []byte, height int64, ok bool) {
   312  
   313  	res, height, err := common.QueryDelegationRewards(cliCtx, queryRoute, delAddr, valAddr)
   314  	if err != nil {
   315  		rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   316  		return nil, 0, false
   317  	}
   318  
   319  	return res, height, true
   320  }