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