-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
302 lines (249 loc) · 8.99 KB
/
query.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package rest
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/pocblockchain/pocc/x/distribution/client/common"
"github.com/pocblockchain/pocc/x/distribution/types"
"github.com/pocblockchain/pocc/client/context"
sdk "github.com/pocblockchain/pocc/types"
"github.com/pocblockchain/pocc/types/rest"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute string) {
// Get the total rewards balance from all delegations
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/rewards",
delegatorRewardsHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Query a delegation reward
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/rewards/{validatorAddr}",
delegationRewardsHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Get the rewards withdrawal address
r.HandleFunc(
"/distribution/delegators/{delegatorAddr}/withdraw_address",
delegatorWithdrawalAddrHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Validator distribution information
r.HandleFunc(
"/distribution/validators/{validatorAddr}",
validatorInfoHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Commission and self-delegation rewards of a single a validator
r.HandleFunc(
"/distribution/validators/{validatorAddr}/rewards",
validatorRewardsHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Outstanding rewards of a single validator
r.HandleFunc(
"/distribution/validators/{validatorAddr}/outstanding_rewards",
outstandingRewardsHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Get the current distribution parameter values
r.HandleFunc(
"/distribution/parameters",
paramsHandlerFn(cliCtx, queryRoute),
).Methods("GET")
// Get the amount held in the community pool
r.HandleFunc(
"/distribution/community_pool",
communityPoolHandler(cliCtx, queryRoute),
).Methods("GET")
}
// HTTP request handler to query the total rewards balance from all delegations
func delegatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// query for rewards from a particular delegator
res, ok := checkResponseQueryDelegatorTotalRewards(w, cliCtx, queryRoute, mux.Vars(r)["delegatorAddr"])
if !ok {
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// HTTP request handler to query a delegation rewards
func delegationRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// query for rewards from a particular delegation
res, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, mux.Vars(r)["delegatorAddr"], mux.Vars(r)["validatorAddr"])
if !ok {
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// HTTP request handler to query a delegation rewards
func delegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
delegatorAddr, ok := checkDelegatorAddressVar(w, r)
if !ok {
return
}
cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
bz := cliCtx.Codec.MustMarshalJSON(types.NewQueryDelegatorWithdrawAddrParams(delegatorAddr))
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/withdraw_addr", queryRoute), bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
// ValidatorDistInfo defines the properties of
// validator distribution information response.
type ValidatorDistInfo struct {
OperatorAddress sdk.AccAddress `json:"operator_address" yaml:"operator_address"`
SelfBondRewards sdk.DecCoins `json:"self_bond_rewards" yaml:"self_bond_rewards"`
ValidatorCommission types.ValidatorAccumulatedCommission `json:"val_commission" yaml:"val_commission"`
}
// NewValidatorDistInfo creates a new instance of ValidatorDistInfo.
func NewValidatorDistInfo(operatorAddr sdk.AccAddress, rewards sdk.DecCoins,
commission types.ValidatorAccumulatedCommission) ValidatorDistInfo {
return ValidatorDistInfo{
OperatorAddress: operatorAddr,
SelfBondRewards: rewards,
ValidatorCommission: commission,
}
}
// HTTP request handler to query validator's distribution information
func validatorInfoHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
valAddr := mux.Vars(r)["validatorAddr"]
validatorAddr, ok := checkValidatorAddressVar(w, r)
if !ok {
return
}
cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// query commission
commissionRes, err := common.QueryValidatorCommission(cliCtx, queryRoute, validatorAddr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var valCom types.ValidatorAccumulatedCommission
cliCtx.Codec.MustUnmarshalJSON(commissionRes, &valCom)
// self bond rewards
delAddr := sdk.AccAddress(validatorAddr)
rewardsRes, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr.String(), valAddr)
if !ok {
return
}
var rewards sdk.DecCoins
cliCtx.Codec.MustUnmarshalJSON(rewardsRes, &rewards)
// Prepare response
res := cliCtx.Codec.MustMarshalJSON(NewValidatorDistInfo(delAddr, rewards, valCom))
rest.PostProcessResponse(w, cliCtx, res)
}
}
// HTTP request handler to query validator's commission and self-delegation rewards
func validatorRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
valAddr := mux.Vars(r)["validatorAddr"]
validatorAddr, ok := checkValidatorAddressVar(w, r)
if !ok {
return
}
cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
delAddr := sdk.AccAddress(validatorAddr).String()
res, ok := checkResponseQueryDelegationRewards(w, cliCtx, queryRoute, delAddr, valAddr)
if !ok {
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// HTTP request handler to query the distribution params values
func paramsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
params, err := common.QueryParams(cliCtx, queryRoute)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, params)
}
}
func communityPoolHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var result sdk.DecCoins
if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, result)
}
}
// HTTP request handler to query the outstanding rewards
func outstandingRewardsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
validatorAddr, ok := checkValidatorAddressVar(w, r)
if !ok {
return
}
cliCtx, ok = rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
bin := cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorOutstandingRewardsParams(validatorAddr))
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_outstanding_rewards", queryRoute), bin)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
func checkResponseQueryDelegatorTotalRewards(
w http.ResponseWriter, cliCtx context.CLIContext, queryRoute, delAddr string,
) (res []byte, ok bool) {
res, err := common.QueryDelegatorTotalRewards(cliCtx, queryRoute, delAddr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return nil, false
}
return res, true
}
func checkResponseQueryDelegationRewards(
w http.ResponseWriter, cliCtx context.CLIContext, queryRoute, delAddr, valAddr string,
) (res []byte, ok bool) {
res, err := common.QueryDelegationRewards(cliCtx, queryRoute, delAddr, valAddr)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return nil, false
}
return res, true
}