cosmos-sdk/x/gov/client/rest/rest.go

616 lines
18 KiB
Go
Raw Normal View History

2018-06-21 17:19:14 -07:00
package rest
import (
"fmt"
"net/http"
2019-02-04 07:48:26 -08:00
"github.com/cosmos/cosmos-sdk/client/rest"
2018-06-21 17:19:14 -07:00
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
2018-06-21 17:19:14 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
2018-08-06 11:11:30 -07:00
"errors"
2018-06-21 17:19:14 -07:00
"github.com/gorilla/mux"
2018-12-10 06:27:25 -08:00
govClientUtils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
2018-06-21 17:19:14 -07:00
)
// REST Variable names
// nolint
const (
RestParamsType = "type"
RestProposalID = "proposal-id"
RestDepositor = "depositor"
RestVoter = "voter"
RestProposalStatus = "status"
RestNumLimit = "limit"
2018-06-21 17:19:14 -07:00
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
2018-08-06 11:11:30 -07:00
r.HandleFunc("/gov/proposals", postProposalHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(
fmt.Sprintf("/gov/parameters/{%s}", RestParamsType),
queryParamsHandlerFn(cdc, cliCtx),
).Methods("GET")
2018-10-15 01:41:37 -07:00
r.HandleFunc("/gov/proposals", queryProposalsWithParameterFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}", RestProposalID), queryProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(
fmt.Sprintf("/gov/proposals/{%s}/proposer", RestProposalID),
queryProposerHandlerFn(cdc, cliCtx),
).Methods("GET")
2018-10-12 19:05:22 -07:00
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), queryDepositsHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits/{%s}", RestProposalID, RestDepositor), queryDepositHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/tally", RestProposalID), queryTallyOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
2018-10-15 01:41:37 -07:00
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes/{%s}", RestProposalID, RestVoter), queryVoteHandlerFn(cdc, cliCtx)).Methods("GET")
2018-06-21 17:19:14 -07:00
}
type postProposalReq struct {
2019-02-04 07:48:26 -08:00
BaseReq rest.BaseReq `json:"base_req"`
2019-02-08 18:33:06 -08:00
Title string `json:"title"` // Title of the proposal
Description string `json:"description"` // Description of the proposal
ProposalType string `json:"proposal_type"` // Type of proposal. Initial set {PlainTextProposal, SoftwareUpgradeProposal}
Proposer sdk.AccAddress `json:"proposer"` // Address of the proposer
2018-10-24 06:37:06 -07:00
InitialDeposit sdk.Coins `json:"initial_deposit"` // Coins to add to the proposal's deposit
2018-06-21 17:19:14 -07:00
}
type depositReq struct {
2019-02-04 07:48:26 -08:00
BaseReq rest.BaseReq `json:"base_req"`
Depositor sdk.AccAddress `json:"depositor"` // Address of the depositor
2018-07-10 17:59:07 -07:00
Amount sdk.Coins `json:"amount"` // Coins to add to the proposal's deposit
2018-06-21 17:19:14 -07:00
}
type voteReq struct {
2019-02-04 07:48:26 -08:00
BaseReq rest.BaseReq `json:"base_req"`
2019-02-08 18:33:06 -08:00
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option string `json:"option"` // option from OptionSet chosen by the voter
2018-06-21 17:19:14 -07:00
}
func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
var req postProposalReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
2018-06-21 17:19:14 -07:00
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
2018-06-21 17:19:14 -07:00
return
}
proposalType, err := gov.ProposalTypeFromString(govClientUtils.NormalizeProposalType(req.ProposalType))
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-06-21 17:19:14 -07:00
// create the message
msg := gov.NewMsgSubmitProposal(req.Title, req.Description, proposalType, req.Proposer, req.InitialDeposit)
if err := msg.ValidateBasic(); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
if req.BaseReq.GenerateOnly {
2019-02-04 07:48:26 -08:00
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
2019-02-04 07:48:26 -08:00
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
2018-06-21 17:19:14 -07:00
}
}
func depositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
return
}
2018-06-21 17:19:14 -07:00
var req depositReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
2018-06-21 17:19:14 -07:00
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
2018-06-21 17:19:14 -07:00
return
}
// create the message
msg := gov.NewMsgDeposit(req.Depositor, proposalID, req.Amount)
if err := msg.ValidateBasic(); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
if req.BaseReq.GenerateOnly {
2019-02-04 07:48:26 -08:00
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
2019-02-04 07:48:26 -08:00
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
2018-06-21 17:19:14 -07:00
}
}
func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
return
}
2018-06-21 17:19:14 -07:00
var req voteReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
2018-06-21 17:19:14 -07:00
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
2018-06-21 17:19:14 -07:00
return
}
voteOption, err := gov.VoteOptionFromString(govClientUtils.NormalizeVoteOption(req.Option))
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-06-21 17:19:14 -07:00
// create the message
msg := gov.NewMsgVote(req.Voter, proposalID, voteOption)
if err := msg.ValidateBasic(); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
if req.BaseReq.GenerateOnly {
2019-02-04 07:48:26 -08:00
rest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
2019-02-04 07:48:26 -08:00
rest.CompleteAndBroadcastTxREST(w, r, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
2018-06-21 17:19:14 -07:00
}
}
func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
paramType := vars[RestParamsType]
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/gov/%s/%s", gov.QueryParams, paramType), nil)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-10-15 01:41:37 -07:00
func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2018-06-21 17:19:14 -07:00
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
2018-06-21 17:19:14 -07:00
return
}
params := gov.NewQueryProposalParams(proposalID)
2018-08-04 22:56:48 -07:00
2018-08-22 00:10:11 -07:00
bz, err := cdc.MarshalJSON(params)
2018-08-04 22:56:48 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2018-08-22 00:10:11 -07:00
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
2018-06-21 17:19:14 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2018-08-06 11:11:30 -07:00
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
2018-06-21 17:19:14 -07:00
}
}
2018-10-12 19:05:22 -07:00
func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-10-12 19:05:22 -07:00
if !ok {
return
}
params := gov.NewQueryProposalParams(proposalID)
2018-10-12 19:05:22 -07:00
bz, err := cdc.MarshalJSON(params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-10-12 19:05:22 -07:00
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
// For inactive proposals we must query the txs directly to get the deposits
// as they're no longer in state.
propStatus := proposal.GetStatus()
if !(propStatus == gov.StatusVotingPeriod || propStatus == gov.StatusDepositPeriod) {
res, err = gcutils.QueryDepositsByTxQuery(cdc, cliCtx, params)
} else {
res, err = cliCtx.QueryWithData("custom/gov/deposits", bz)
}
2018-10-12 19:05:22 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-10-12 19:05:22 -07:00
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
2018-10-12 19:05:22 -07:00
}
}
func queryProposerHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}
res, err := gcutils.QueryProposerByTxQuery(cdc, cliCtx, proposalID)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-10-15 01:41:37 -07:00
func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
bechDepositorAddr := vars[RestDepositor]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
return
}
if len(bechDepositorAddr) == 0 {
err := errors.New("depositor address required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params := gov.NewQueryDepositParams(proposalID, depositorAddr)
2018-08-06 11:11:30 -07:00
2018-08-22 00:10:11 -07:00
bz, err := cdc.MarshalJSON(params)
2018-08-04 22:56:48 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-08-22 00:10:11 -07:00
res, err := cliCtx.QueryWithData("custom/gov/deposit", bz)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2018-08-22 00:10:11 -07:00
var deposit gov.Deposit
cdc.UnmarshalJSON(res, &deposit)
// For an empty deposit, either the proposal does not exist or is inactive in
// which case the deposit would be removed from state and should be queried
// for directly via a txs query.
2018-08-04 22:56:48 -07:00
if deposit.Empty() {
bz, err := cdc.MarshalJSON(gov.NewQueryProposalParams(proposalID))
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err = cliCtx.QueryWithData("custom/gov/proposal", bz)
2018-08-04 22:56:48 -07:00
if err != nil || len(res) == 0 {
err := fmt.Errorf("proposalID %d does not exist", proposalID)
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
2018-08-04 22:56:48 -07:00
return
}
res, err = gcutils.QueryDepositByTxQuery(cdc, cliCtx, params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2018-08-04 22:56:48 -07:00
}
2018-08-22 00:10:11 -07:00
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-10-15 01:41:37 -07:00
func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-06-21 17:19:14 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
2018-06-21 17:19:14 -07:00
bechVoterAddr := vars[RestVoter]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
2018-06-21 17:19:14 -07:00
return
}
if len(bechVoterAddr) == 0 {
err := errors.New("voter address required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-07-09 16:06:05 -07:00
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
2018-06-21 17:19:14 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
params := gov.NewQueryVoteParams(proposalID, voterAddr)
2018-08-22 00:10:11 -07:00
bz, err := cdc.MarshalJSON(params)
2018-08-04 22:56:48 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2018-08-22 00:10:11 -07:00
res, err := cliCtx.QueryWithData("custom/gov/vote", bz)
2018-06-21 17:19:14 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-06-21 17:19:14 -07:00
return
}
2018-08-22 00:10:11 -07:00
var vote gov.Vote
cdc.UnmarshalJSON(res, &vote)
// For an empty vote, either the proposal does not exist or is inactive in
// which case the vote would be removed from state and should be queried for
// directly via a txs query.
2018-08-04 22:56:48 -07:00
if vote.Empty() {
bz, err := cdc.MarshalJSON(gov.NewQueryProposalParams(proposalID))
2018-08-22 00:10:11 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-08-22 00:10:11 -07:00
return
}
res, err = cliCtx.QueryWithData("custom/gov/proposal", bz)
2018-08-04 22:56:48 -07:00
if err != nil || len(res) == 0 {
err := fmt.Errorf("proposalID %d does not exist", proposalID)
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
2018-08-04 22:56:48 -07:00
return
}
res, err = gcutils.QueryVoteByTxQuery(cdc, cliCtx, params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2018-08-04 22:56:48 -07:00
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
2018-06-21 17:19:14 -07:00
}
}
// todo: Split this functionality into helper functions to remove the above
2018-10-15 01:41:37 -07:00
func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-15 17:59:15 -07:00
if !ok {
return
}
params := gov.NewQueryProposalParams(proposalID)
2018-08-22 00:10:11 -07:00
bz, err := cdc.MarshalJSON(params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
// For inactive proposals we must query the txs directly to get the votes
// as they're no longer in state.
propStatus := proposal.GetStatus()
if !(propStatus == gov.StatusVotingPeriod || propStatus == gov.StatusDepositPeriod) {
res, err = gcutils.QueryVotesByTxQuery(cdc, cliCtx, params)
} else {
res, err = cliCtx.QueryWithData("custom/gov/votes", bz)
}
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-07-09 16:16:43 -07:00
// todo: Split this functionality into helper functions to remove the above
2018-10-15 01:41:37 -07:00
func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bechVoterAddr := r.URL.Query().Get(RestVoter)
bechDepositorAddr := r.URL.Query().Get(RestDepositor)
strProposalStatus := r.URL.Query().Get(RestProposalStatus)
strNumLimit := r.URL.Query().Get(RestNumLimit)
2018-08-22 00:10:11 -07:00
params := gov.QueryProposalsParams{}
if len(bechVoterAddr) != 0 {
2018-08-22 00:10:11 -07:00
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-08-22 00:10:11 -07:00
params.Voter = voterAddr
}
if len(bechDepositorAddr) != 0 {
depositorAddr, err := sdk.AccAddressFromBech32(bechDepositorAddr)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.Depositor = depositorAddr
}
if len(strProposalStatus) != 0 {
proposalStatus, err := gov.ProposalStatusFromString(govClientUtils.NormalizeProposalStatus(strProposalStatus))
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-08-22 00:10:11 -07:00
params.ProposalStatus = proposalStatus
}
if len(strNumLimit) != 0 {
2019-02-04 07:48:26 -08:00
numLimit, ok := rest.ParseUint64OrReturnBadRequest(w, strNumLimit)
2018-08-15 17:59:15 -07:00
if !ok {
2018-08-04 22:56:48 -07:00
return
}
params.Limit = numLimit
2018-08-04 22:56:48 -07:00
}
2018-08-22 00:10:11 -07:00
bz, err := cdc.MarshalJSON(params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
2018-08-06 11:11:30 -07:00
2018-08-22 00:10:11 -07:00
res, err := cliCtx.QueryWithData("custom/gov/proposals", bz)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-08-22 18:44:35 -07:00
// todo: Split this functionality into helper functions to remove the above
2018-10-15 01:41:37 -07:00
func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
2018-08-22 18:44:35 -07:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-08-22 18:44:35 -07:00
return
}
2019-02-04 07:48:26 -08:00
proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID)
2018-08-22 18:44:35 -07:00
if !ok {
return
}
params := gov.NewQueryProposalParams(proposalID)
2018-08-22 18:44:35 -07:00
bz, err := cdc.MarshalJSON(params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
2018-08-22 18:44:35 -07:00
return
}
res, err := cliCtx.QueryWithData("custom/gov/tally", bz)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-08-22 18:44:35 -07:00
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
2018-08-22 18:44:35 -07:00
}
}