/
validator_voting_power.go
65 lines (55 loc) · 1.56 KB
/
validator_voting_power.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
// Copyright 2018 The QOS Authors
package hdata
import (
"net/http"
"strconv"
"time"
"github.com/QOSGroup/qmoon/handler/middleware"
"github.com/QOSGroup/qmoon/lib"
"github.com/QOSGroup/qmoon/service/metric"
"github.com/QOSGroup/qmoon/types"
"github.com/QOSGroup/qmoon/utils"
"github.com/gin-gonic/gin"
)
const validatorVotingPowerUrl = NodeProxy + "/validators/:address/votingPower"
func init() {
hdataHander[validatorVotingPowerUrl] = ValidatorVotingPowerGinRegister
}
// ValidatorVotingPowerGinRegister 注册
func ValidatorVotingPowerGinRegister(r *gin.Engine) {
r.GET(validatorVotingPowerUrl, middleware.ApiAuthGin(), validatorVotingPowerGin())
}
func validatorVotingPowerGin() gin.HandlerFunc {
return func(c *gin.Context) {
node, err := GetNodeFromUrl(c)
if err != nil {
c.JSON(http.StatusOK, types.RPCMethodNotFoundError(""))
return
}
address := lib.Bech32AddressToHex(c.Param("address"))
now := time.Now()
start := utils.NDaysAgo(now, 28).Unix()
end := now.Unix()
step := "1d"
if d, err := strconv.ParseInt(c.Query("start"), 10, 64); err == nil {
start = d
}
if d, err := strconv.ParseInt(c.Query("end"), 10, 64); err == nil {
end = d
}
if d := c.Query("step"); d != "" {
if utils.IsDigit(d) {
step = d + "s"
} else {
step = d
}
}
res, err := metric.QueryValidatorVotingPower(node.ChanID, address,
time.Unix(start, 0), time.Unix(end, 0), step)
if err != nil {
c.JSON(http.StatusOK, types.RPCServerError("", err))
return
}
c.JSON(http.StatusOK, types.NewRPCSuccessResponse(lib.Cdc, "", res))
}
}