diff --git a/config/constants.go b/config/constants.go index 67d66bd..2f598ae 100755 --- a/config/constants.go +++ b/config/constants.go @@ -44,6 +44,7 @@ const ( QueryStakingPool = "/api/kira/staking-pool" QueryDelegations = "/api/kira/delegations" + QueryUndelegations = "/api/kira/undelegations" QuerySpendingPools = "/api/kira/spending-pools" QuerySpendingPoolProposals = "/api/kira/spending-pool-proposals" diff --git a/gateway/kira/multistaking.go b/gateway/kira/multistaking.go index 5803507..1bc8a88 100644 --- a/gateway/kira/multistaking.go +++ b/gateway/kira/multistaking.go @@ -29,9 +29,11 @@ type QueryBalancesResponse struct { func RegisterKiraMultiStakingRoutes(r *mux.Router, gwCosmosmux *runtime.ServeMux, rpcAddr string) { r.HandleFunc(config.QueryStakingPool, QueryStakingPoolRequest(gwCosmosmux, rpcAddr)).Methods("GET") r.HandleFunc(config.QueryDelegations, QueryDelegationsRequest(gwCosmosmux, rpcAddr)).Methods("GET") + r.HandleFunc(config.QueryUndelegations, QueryUndelegationsRequest(gwCosmosmux, rpcAddr)).Methods("GET") common.AddRPCMethod("GET", config.QueryStakingPool, "This is an API to query staking pool.", true) common.AddRPCMethod("GET", config.QueryDelegations, "This is an API to query delegations.", true) + common.AddRPCMethod("GET", config.QueryUndelegations, "This is an API to query undelegations.", true) } func parseCoinString(input string) sdk.Coin { @@ -128,6 +130,20 @@ func QueryStakingPoolRequest(gwCosmosmux *runtime.ServeMux, rpcAddr string) http } } +// queryUndelegationsHandler is a function to query all undelegations for a delegator +func queryUndelegationsHandler(r *http.Request, gwCosmosmux *runtime.ServeMux) (interface{}, interface{}, int) { + queries := r.URL.Query() + account := queries["undelegatorAddress"] + + if len(account) == 0 { + common.GetLogger().Error("[query-undelegations] 'delegator address' is not set") + return common.ServeError(0, "'delegator address' is not set", "", http.StatusBadRequest) + } + + r.URL.Path = strings.Replace(r.URL.Path, "/api/kira/undelegations", "/kira/multistaking/v1beta1/undelegations", -1) + return common.ServeGRPC(r, gwCosmosmux) +} + // queryDelegationsHandler is a function to query all delegations for a delegator func queryDelegationsHandler(r *http.Request, gwCosmosmux *runtime.ServeMux) (interface{}, interface{}, int) { queries := r.URL.Query() @@ -256,3 +272,32 @@ func QueryDelegationsRequest(gwCosmosmux *runtime.ServeMux, rpcAddr string) http common.WrapResponse(w, request, *response, statusCode, common.RPCMethods["GET"][config.QueryDelegations].CachingEnabled) } } + +func QueryUndelegationsRequest(gwCosmosmux *runtime.ServeMux, rpcAddr string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var statusCode int + request := common.GetInterxRequest(r) + response := common.GetResponseFormat(request, rpcAddr) + + common.GetLogger().Info("[query-undelegations] Entering undelegations query") + + if !common.RPCMethods["GET"][config.QueryUndelegations].Enabled { + response.Response, response.Error, statusCode = common.ServeError(0, "", "API disabled", http.StatusForbidden) + } else { + if common.RPCMethods["GET"][config.QueryUndelegations].CachingEnabled { + found, cacheResponse, cacheError, cacheStatus := common.SearchCache(request, response) + if found { + response.Response, response.Error, statusCode = cacheResponse, cacheError, cacheStatus + common.WrapResponse(w, request, *response, statusCode, false) + + common.GetLogger().Info("[query-staking-pool] Returning from the cache") + return + } + } + + response.Response, response.Error, statusCode = queryUndelegationsHandler(r, gwCosmosmux) + } + + common.WrapResponse(w, request, *response, statusCode, common.RPCMethods["GET"][config.QueryUndelegations].CachingEnabled) + } +}