-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathvalidator_withdrawalcredentials_v1.go
More file actions
86 lines (73 loc) · 2.67 KB
/
Copy pathvalidator_withdrawalcredentials_v1.go
File metadata and controls
86 lines (73 loc) · 2.67 KB
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
package api
import (
"encoding/json"
"net/http"
"strconv"
"github.com/ethpandaops/dora/dbtypes"
"github.com/ethpandaops/dora/services"
"github.com/ethpandaops/dora/utils"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
type ApiWithdrawalCredentialsResponseV1 struct {
PublicKey string `json:"publickey"`
ValidatorIndex uint64 `json:"validatorindex"`
}
// ApiWithdrawalCredentialsValidators godoc
// @Summary Get all validators that have a specific withdrawal credentials
// @Tags Validator
// @Produce json
// @Param withdrawalCredentialsOrEth1address path string true "Provide a withdrawal credential or an eth1 address with an optional 0x prefix". It can also be a valid ENS name.
// @Param limit query int false "Limit the number of results, maximum: 200" default(10)
// @Param offset query int false "Offset the number of results" default(0)
// @Success 200 {object} ApiResponse{data=[]ApiWithdrawalCredentialsResponseV1}
// @Failure 400 {object} ApiResponse
// @Router /v1/validator/withdrawalCredentials/{withdrawalCredentialsOrEth1address} [get]
// @ID getWithdrawalCredentialsValidators
func ApiWithdrawalCredentialsValidatorsV1(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
q := r.URL.Query()
limitQuery := q.Get("limit")
offsetQuery := q.Get("offset")
limit, err := strconv.ParseUint(limitQuery, 10, 64)
if err != nil {
limit = 2000
}
offset, err := strconv.ParseUint(offsetQuery, 10, 64)
if err != nil {
offset = 0
}
if limit == 0 || limit > 2000 {
limit = 2000
}
vars := mux.Vars(r)
search := vars["withdrawalCredentialsOrEth1address"]
withdrawalAddress, withdrawalCreds, err := utils.ParseWithdrawalAddressOrCredentials(search)
if err != nil {
sendBadRequestResponse(w, r.URL.String(), "invalid withdrawal address or credentials provided")
return
}
filter := &dbtypes.ValidatorFilter{
WithdrawalAddress: withdrawalAddress,
WithdrawalCreds: withdrawalCreds,
Limit: limit,
Offset: offset,
}
relevantValidators, _ := services.GlobalBeaconService.GetFilteredValidatorSet(r.Context(), filter, true)
data := []*ApiWithdrawalCredentialsResponseV1{}
for _, validator := range relevantValidators {
data = append(data, &ApiWithdrawalCredentialsResponseV1{
PublicKey: validator.Validator.PublicKey.String(),
ValidatorIndex: uint64(validator.Index),
})
}
j := json.NewEncoder(w)
response := &ApiResponse{}
response.Status = "OK"
response.Data = data
err = j.Encode(response)
if err != nil {
sendServerErrorResponse(w, r.URL.String(), "could not serialize data results")
logrus.Errorf("error serializing json data for API %v route: %v", r.URL, err)
}
}