forked from terra-money/classic-core
/
main.go
279 lines (227 loc) · 8.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package rest
import (
"fmt"
"net/http"
"terra/x/budget"
"terra/x/oracle"
"github.com/cosmos/cosmos-sdk/client/context"
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// REST Variable names
// nolint
const (
RestParamsType = "type"
RestProgramID = "program-id"
RestVoter = "voter"
RestProgramStatus = "status"
RestNumLimit = "limit"
queryRoute = "budget"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc("/budget/program/submit", submitProgramHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}/withdraw", RestProgramID), voteHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}/vote", RestProgramID), voteHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}", RestProgramID), queryProgramHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}/actives", RestProgramID), queryActivesHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}/candidates", RestProgramID), queryCandidatesHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/budget/program/{%s}/votes/{%s}", RestProgramID, RestVoter), queryVotesHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc("/budget/params", queryParamsHandlerFn(cdc, cliCtx)).Methods("GET")
}
type submitProgramReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"` // Title of the Program
Description string `json:"description"` // Description of the Program
Submitter sdk.AccAddress `json:"submitter"` // Address of the submitter
Executor sdk.AccAddress `json:"executor"` // Address of the executor
Deposit sdk.Coin `json:"deposit"` // Coins to add to the Program's deposit
}
type voteReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Voter sdk.AccAddress `json:"voter"` // address of the voter
Option bool `json:"option"` // option from OptionSet chosen by the voter
}
type withdrawReq struct {
BaseReq rest.BaseReq `json:"base_req"`
}
func submitProgramHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req submitProgramReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
fromAddress := cliCtx.GetFromAddress()
if !req.Submitter.Equals(fromAddress) {
rest.WriteErrorResponse(w, http.StatusUnauthorized, "Must use own address")
return
}
// create the message
msg := budget.NewSubmitProgramMsg(req.Title, req.Description, req.Deposit, req.Submitter, req.Executor)
err := msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if req.BaseReq.GenerateOnly {
clientrest.WriteGenerateStdTxResponse(w, cdc, cliCtx, req.BaseReq, []sdk.Msg{msg})
return
}
clientrest.CompleteAndBroadcastTxREST(w, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
func withdrawProgramHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProgramID := vars[RestProgramID]
if len(strProgramID) == 0 {
err := errors.New("programID required but not specified")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
programID, ok := rest.ParseUint64OrReturnBadRequest(w, strProgramID)
if !ok {
return
}
var req withdrawReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
fromAddress := cliCtx.GetFromAddress()
// create the message
msg := budget.NewWithdrawProgramMsg(programID, fromAddress)
err := msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
clientrest.CompleteAndBroadcastTxREST(w, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProgramID := vars[RestProgramID]
if len(strProgramID) == 0 {
err := errors.New("programID required but not specified")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
programID, ok := rest.ParseUint64OrReturnBadRequest(w, strProgramID)
if !ok {
return
}
var req voteReq
if !rest.ReadRESTReq(w, r, cdc, &req) {
return
}
baseReq := req.BaseReq.Sanitize()
if !baseReq.ValidateBasic(w) {
return
}
// create the message
msg := budget.NewVoteMsg(programID, req.Option, req.Voter)
err := msg.ValidateBasic()
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
clientrest.CompleteAndBroadcastTxREST(w, cliCtx, req.BaseReq, []sdk.Msg{msg}, cdc)
}
}
func queryProgramHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProgramID := vars[RestProgramID]
if len(strProgramID) == 0 {
err := errors.New("programID required but not specified")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, budget.QueryProgram, strProgramID), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryActivesHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, budget.QueryActiveList), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryCandidatesHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, budget.QueryCandidateList), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
// todo: Split this functionality into helper functions to remove the above
func queryVotesHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProgramID := vars[RestProgramID]
strVoterAddr := vars[RestVoter]
params := budget.QueryVotesParams{}
if len(strProgramID) != 0 {
programID, ok := rest.ParseUint64OrReturnBadRequest(w, strProgramID)
if !ok {
err := errors.New("programID must be an unsigned int")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.ProgramID = programID
}
if len(strVoterAddr) != 0 {
voterAcc, err := sdk.AccAddressFromBech32(strVoterAddr)
if err != nil {
err := errors.New("voteraccount malformed")
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params.Voter = voterAcc
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, budget.QueryVotes), bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, oracle.QueryParams), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}