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

62 lines
1.8 KiB
Go

package rest
import (
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
govrest "github.com/cosmos/cosmos-sdk/x/gov/client/rest"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
func RegisterHandlers(clientCtx client.Context, r *mux.Router) {
registerQueryRoutes(clientCtx, r)
registerTxHandlers(clientCtx, r)
}
// RegisterRoutes register distribution REST routes.
func RegisterRoutes(clientCtx client.Context, r *mux.Router, queryRoute string) {
registerQueryRoutes(clientCtx, r)
registerTxRoutes(clientCtx, r, queryRoute)
}
// TODO add proto compatible Handler after x/gov migration
// ProposalRESTHandler returns a ProposalRESTHandler that exposes the community pool spend REST handler with a given sub-route.
func ProposalRESTHandler(clientCtx client.Context) govrest.ProposalRESTHandler {
return govrest.ProposalRESTHandler{
SubRoute: "community_pool_spend",
Handler: postProposalHandlerFn(clientCtx),
}
}
func postProposalHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req CommunityPoolSpendProposalReq
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
content := types.NewCommunityPoolSpendProposal(req.Title, req.Description, req.Recipient, req.Amount)
msg, err := govtypes.NewMsgSubmitProposal(content, req.Deposit, req.Proposer)
if rest.CheckBadRequestError(w, err) {
return
}
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
}
}