github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/gov/client/rest/tx.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 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils" 13 gcutils "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/client/utils" 14 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types" 15 ) 16 17 func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, phs []ProposalRESTHandler) { 18 propSubRtr := r.PathPrefix("/gov/proposals").Subrouter() 19 for _, ph := range phs { 20 propSubRtr.HandleFunc(fmt.Sprintf("/%s", ph.SubRoute), ph.Handler).Methods("POST") 21 } 22 23 r.HandleFunc("/gov/proposals", postProposalHandlerFn(cliCtx)).Methods("POST") 24 r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(cliCtx)).Methods("POST") 25 r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(cliCtx)).Methods("POST") 26 } 27 28 func postProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 29 return func(w http.ResponseWriter, r *http.Request) { 30 var req PostProposalReq 31 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 32 return 33 } 34 35 req.BaseReq = req.BaseReq.Sanitize() 36 if !req.BaseReq.ValidateBasic(w) { 37 return 38 } 39 40 proposalType := gcutils.NormalizeProposalType(req.ProposalType) 41 content := types.ContentFromProposalType(req.Title, req.Description, proposalType) 42 43 msg := types.NewMsgSubmitProposal(content, req.InitialDeposit, req.Proposer) 44 if err := msg.ValidateBasic(); err != nil { 45 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 46 return 47 } 48 49 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 50 } 51 } 52 53 func depositHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 54 return func(w http.ResponseWriter, r *http.Request) { 55 vars := mux.Vars(r) 56 strProposalID := vars[RestProposalID] 57 58 if len(strProposalID) == 0 { 59 rest.WriteErrorResponse(w, http.StatusBadRequest, "proposalId required but not specified") 60 return 61 } 62 63 proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID) 64 if !ok { 65 return 66 } 67 68 var req DepositReq 69 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 70 return 71 } 72 73 req.BaseReq = req.BaseReq.Sanitize() 74 if !req.BaseReq.ValidateBasic(w) { 75 return 76 } 77 78 // create the message 79 msg := types.NewMsgDeposit(req.Depositor, proposalID, req.Amount) 80 if err := msg.ValidateBasic(); err != nil { 81 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 82 return 83 } 84 85 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 86 } 87 } 88 89 func voteHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { 90 return func(w http.ResponseWriter, r *http.Request) { 91 vars := mux.Vars(r) 92 strProposalID := vars[RestProposalID] 93 94 if len(strProposalID) == 0 { 95 rest.WriteErrorResponse(w, http.StatusBadRequest, "proposalId required but not specified") 96 return 97 } 98 99 proposalID, ok := rest.ParseUint64OrReturnBadRequest(w, strProposalID) 100 if !ok { 101 return 102 } 103 104 var req VoteReq 105 if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) { 106 return 107 } 108 109 req.BaseReq = req.BaseReq.Sanitize() 110 if !req.BaseReq.ValidateBasic(w) { 111 return 112 } 113 114 voteOption, err := types.VoteOptionFromString(gcutils.NormalizeVoteOption(req.Option)) 115 if err != nil { 116 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 117 return 118 } 119 120 // create the message 121 msg := types.NewMsgVote(req.Voter, proposalID, voteOption) 122 if err := msg.ValidateBasic(); err != nil { 123 rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) 124 return 125 } 126 127 utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg}) 128 } 129 }