-
Notifications
You must be signed in to change notification settings - Fork 1
/
apis.go
191 lines (153 loc) · 5.42 KB
/
apis.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
package k2
import (
"encoding/json"
"net/http"
"strings"
apiv1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common"
)
const (
// Router paths
pathRoot = "/"
pathExit = "/eth/v1/exit"
pathClaim = "/eth/v1/claim"
pathRegister = "/eth/v1/register"
pathGetDelegatedValidators = "/eth/v1/delegated-validators"
pathUpdateK2Payout = "/eth/v1/update-k2-payout-recipient"
)
func (k2 *K2Service) handleRoot(w http.ResponseWriter, _ *http.Request) {
k2.respondOK(w, "K2 module is running")
}
func (k2 *K2Service) handleExit(w http.ResponseWriter, r *http.Request) {
// Post call.
// Handles the removal of the validators delegated balance from the K2 contract,
// and therefore exits the K2 protocol.
payload := phase0.BLSPubKey{}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&payload)
if err != nil {
k2.respondError(w, http.StatusBadRequest, err.Error())
return
}
result, err := k2.processExit(payload)
if err != nil {
k2.respondError(w, http.StatusInternalServerError, err.Error())
return
}
k2.respondOK(w, result)
}
func (k2 *K2Service) handleClaim(w http.ResponseWriter, r *http.Request) {
// Post call.
// Handles the claim of rewards for the validators in the K2 contract.
type claimPayload struct {
NodeOperators []common.Address `json:"nodeOperators"`
}
payload := claimPayload{}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&payload)
if err != nil {
k2.respondError(w, http.StatusBadRequest, err.Error())
return
}
// If no node operators are provided, it will claim rewards for all the configured node operators.
if len(payload.NodeOperators) == 0 {
for _, wallet := range k2.cfg.ValidatorWallets {
payload.NodeOperators = append(payload.NodeOperators, wallet.Address)
}
}
result, err := k2.batchProcessClaims(payload.NodeOperators)
if err != nil {
k2.respondError(w, http.StatusInternalServerError, err.Error())
return
}
if len(result) == 0 {
// force return an empty array instead of null
k2.respondOK(w, []string{})
return
}
k2.respondOK(w, result)
}
func (k2 *K2Service) handleUpdateK2Payout(w http.ResponseWriter, r *http.Request) {
// Post call.
// Handles the change of the payout recipient for a validator in the K2 contract.
type changeK2PayoutPayload struct {
NodeOperator common.Address `json:"nodeOperator"`
PayoutRecipient common.Address `json:"payoutRecipient"`
}
payload := changeK2PayoutPayload{}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&payload)
if err != nil {
k2.respondError(w, http.StatusBadRequest, err.Error())
return
}
if (payload.NodeOperator == common.Address{} || payload.PayoutRecipient == common.Address{}) {
k2.respondError(w, http.StatusBadRequest, "nodeOperator and payoutRecipient are required")
return
}
result, err := k2.changeK2NodeOperatorPayout(payload.NodeOperator, payload.PayoutRecipient)
if err != nil {
k2.respondError(w, http.StatusInternalServerError, err.Error())
return
}
k2.respondOK(w, result)
}
func (k2 *K2Service) handleRegister(w http.ResponseWriter, r *http.Request) {
// Post call.
// Handles the native delegation of validators in the K2 contract if configured to do so.
// This performs the registration of validators if required, in the Proposer Registry,
// and then natively delegates them to the K2 contract.
// If the module is not configured to perform native delegation, it will only register
// the validators in the Proposer Registry.
payload := []apiv1.SignedValidatorRegistration{}
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&payload)
if err != nil {
k2.respondError(w, http.StatusBadRequest, err.Error())
return
}
result, err := k2.batchProcessValidatorRegistrations(payload)
if err != nil {
k2.respondError(w, http.StatusInternalServerError, err.Error())
return
}
k2.respondOK(w, result)
}
func (k2 *K2Service) handleGetValidators(w http.ResponseWriter, r *http.Request) {
// Get call.
// Handles the retrieval of the delegated validators based on representative addresses.
// If no representative addresses are provided, it will return the delegated validators
// for all the configured representative addresses.
queryParams := r.URL.Query()
representativeAddresses := []common.Address{}
if representativeAddressesStr := queryParams.Get("representativeAddresses"); representativeAddressesStr != "" {
representativeAddressesStr := strings.Split(representativeAddressesStr, ",")
for _, address := range representativeAddressesStr {
representativeAddresses = append(representativeAddresses, common.HexToAddress(address))
}
} else {
for _, wallet := range k2.cfg.ValidatorWallets {
representativeAddresses = append(representativeAddresses, wallet.Address)
}
}
includeBalance := false
if includeBalanceStr := queryParams.Get("includeBalance"); includeBalanceStr != "" {
includeBalance = includeBalanceStr == "true"
}
result, err := k2.getDelegatedValidators(representativeAddresses, includeBalance)
if err != nil {
k2.respondError(w, http.StatusInternalServerError, err.Error())
return
}
if len(result) == 0 {
// force return an empty array instead of null
k2.respondOK(w, []string{})
return
}
k2.respondOK(w, result)
}