/
rest.go
106 lines (87 loc) · 3.21 KB
/
rest.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
package client
import (
"fmt"
"github.com/QOSGroup/qbase/client/context"
"github.com/QOSGroup/qbase/client/rpc"
"github.com/QOSGroup/qbase/types"
"github.com/gorilla/mux"
"net/http"
)
const (
ValidatorAddressErrorPrompt = "validator address not valid. it must start with qosval1. current address: %s. err: %s"
AccountAddressErrorPrompt = "account address not valid. it must start with qosacc1.current address: %s. err: %s"
)
func RegisterRoutes(ctx context.CLIContext, r *mux.Router) {
registerQueryRoutes(ctx, r)
}
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/distribution/validators/{validatorAddr}/periods", queryValidatorPeriodsHandleFn(cliCtx)).Methods("GET")
r.HandleFunc("/distribution/delegators/{delegatorAddr}/validators/{validatorAddr}/incomes", queryDelegatorIncomeHandleFn(cliCtx)).Methods("GET")
r.HandleFunc("/distribution/communityFeePools", getCommunityFeePoolsHandleFn(cliCtx)).Methods("GET")
}
func queryValidatorPeriodsHandleFn(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
br, _ := rpc.ParseRequestForm(request)
ctx := br.Setup(cliContext)
vars := mux.Vars(request)
valAddr, ok := MustParseValidatorAddress(writer, vars["validatorAddr"])
if !ok {
return
}
result, err := queryValidatorPeriods(ctx, valAddr)
if err != nil {
rpc.Write40XErrorResponse(writer, err)
return
}
rpc.PostProcessResponseBare(writer, cliContext, result)
}
}
func queryDelegatorIncomeHandleFn(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
br, _ := rpc.ParseRequestForm(request)
ctx := br.Setup(cliContext)
vars := mux.Vars(request)
valAddr, ok := MustParseValidatorAddress(writer, vars["validatorAddr"])
if !ok {
return
}
deleAddr, ok := MustParseAccountAddress(writer, vars["delegatorAddr"])
if !ok {
return
}
result, err := queryDelegatorIncomes(ctx, deleAddr, valAddr)
if err != nil {
rpc.Write40XErrorResponse(writer, err)
return
}
rpc.PostProcessResponseBare(writer, cliContext, result)
}
}
func getCommunityFeePoolsHandleFn(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
br, _ := rpc.ParseRequestForm(request)
ctx := br.Setup(cliContext)
result, err := getCommunityFeePool(ctx)
if err != nil {
rpc.Write40XErrorResponse(writer, err)
return
}
rpc.PostProcessResponseBare(writer, cliContext, result)
}
}
func MustParseValidatorAddress(w http.ResponseWriter, addrStr string) (types.ValAddress, bool) {
addr, err := types.ValAddressFromBech32(addrStr)
if err != nil {
rpc.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf(ValidatorAddressErrorPrompt, addrStr, err.Error()))
return nil, false
}
return addr, true
}
func MustParseAccountAddress(w http.ResponseWriter, addrStr string) (types.AccAddress, bool) {
addr, err := types.AccAddressFromBech32(addrStr)
if err != nil {
rpc.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf(AccountAddressErrorPrompt, addrStr, err.Error()))
return nil, false
}
return addr, true
}