2020-03-14 01:16:45 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
|
2020-03-23 14:32:50 +00:00
|
|
|
"github.com/kava-labs/kava/x/committee/client/common"
|
2020-03-14 01:16:45 +00:00
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/committees", types.ModuleName), queryCommitteesHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/committees/{%s}", types.ModuleName, RestCommitteeID), queryCommitteeHandlerFn(cliCtx)).Methods("GET")
|
2021-09-17 20:45:37 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/next-proposal-id", types.ModuleName), queryNextProposalIdHandlerFn(cliCtx)).Methods("GET")
|
2020-03-14 01:16:45 +00:00
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/committees/{%s}/proposals", types.ModuleName, RestCommitteeID), queryProposalsHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/proposals/{%s}", types.ModuleName, RestProposalID), queryProposalHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/proposals/{%s}/proposer", types.ModuleName, RestProposalID), queryProposerHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/proposals/{%s}/tally", types.ModuleName, RestProposalID), queryTallyOnProposalHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
r.HandleFunc(fmt.Sprintf("/%s/proposals/{%s}/votes", types.ModuleName, RestProposalID), queryVotesOnProposalHandlerFn(cliCtx)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Committees
|
|
|
|
// ------------------------------------------
|
2020-03-14 01:16:45 +00:00
|
|
|
|
|
|
|
func queryCommitteesHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryCommittees), nil)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryCommitteeHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if len(vars[RestCommitteeID]) == 0 {
|
|
|
|
err := errors.New("committeeID required but not specified")
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
committeeID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestCommitteeID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-03-23 20:57:15 +00:00
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryCommitteeParams(committeeID))
|
2020-03-14 01:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryCommittee), bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Proposals
|
|
|
|
// ------------------------------------------
|
2020-03-14 01:16:45 +00:00
|
|
|
|
2021-09-17 20:45:37 +00:00
|
|
|
func queryNextProposalIdHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryNextProposalID), nil)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:16:45 +00:00
|
|
|
func queryProposalsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if len(vars[RestCommitteeID]) == 0 {
|
|
|
|
err := errors.New("committeeID required but not specified")
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
committeeID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestCommitteeID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-03-23 20:57:15 +00:00
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryCommitteeParams(committeeID))
|
2020-03-14 01:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryProposals), bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if len(vars[RestProposalID]) == 0 {
|
|
|
|
err := errors.New("proposalID required but not specified")
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestProposalID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-06-29 23:39:35 +00:00
|
|
|
|
|
|
|
proposal, height, err := common.QueryProposalByID(cliCtx, cliCtx.Codec, types.ModuleName, proposalID)
|
2020-03-14 01:16:45 +00:00
|
|
|
if err != nil {
|
2020-06-29 23:39:35 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2020-03-14 01:16:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:39:35 +00:00
|
|
|
res, err := cliCtx.Codec.MarshalJSON(proposal)
|
2020-03-14 01:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryProposerHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestProposalID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
2020-03-23 14:32:50 +00:00
|
|
|
res, err := common.QueryProposer(cliCtx, proposalID)
|
2020-03-14 01:16:45 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:06:31 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// Votes
|
|
|
|
// ------------------------------------------
|
2020-03-14 01:16:45 +00:00
|
|
|
|
|
|
|
func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if len(vars[RestProposalID]) == 0 {
|
2020-04-24 18:15:57 +00:00
|
|
|
err := errors.New(fmt.Sprintf("%s required but not specified", RestProposalID))
|
2020-03-14 01:16:45 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestProposalID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryProposalParams(proposalID))
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryVotes), bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryTallyOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the query height
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare params for querier
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
if len(vars[RestProposalID]) == 0 {
|
2020-04-24 18:15:57 +00:00
|
|
|
err := errors.New(fmt.Sprintf("%s required but not specified", RestProposalID))
|
2020-03-14 01:16:45 +00:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[RestProposalID])
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryProposalParams(proposalID))
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryTally), bz)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write response
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|