This repository was archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidator.go
63 lines (60 loc) · 1.95 KB
/
validator.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
package main
import (
"context"
"fmt"
"strconv"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func getValidatorCommandResult(command string, parameters []string) string {
if len(parameters) != 1 {
log.Error("Expected 1 parameter for validator command")
return ""
}
reqIndex, err := strconv.Atoi(parameters[0])
if err != nil {
log.WithError(err).Error(err, "failed to convert")
return ""
}
req := ð.GetValidatorRequest{
QueryFilter: ð.GetValidatorRequest_Index{
Index: uint64(reqIndex),
},
}
switch command {
case validatorBalance.command, validatorBalance.shorthand:
balReq := ð.ListValidatorBalancesRequest{
Indices: []uint64{uint64(reqIndex)},
}
balances, err := beaconClient.ListValidatorBalances(context.Background(), balReq)
if err != nil {
log.WithError(err).Error(err, "failed to get balances")
return ""
}
if len(balances.Balances) > 0 && balances.Balances[0].Index == uint64(reqIndex) {
inEther := float64(balances.Balances[0].Balance) / float64(params.BeaconConfig().GweiPerEth)
return fmt.Sprintf(validatorBalance.responseText, reqIndex, inEther)
}
return fmt.Sprintf("Could not get balance of valdiator index %d", reqIndex)
case validatorActive.command, validatorActive.shorthand:
validator, err := beaconClient.GetValidator(context.Background(), req)
if err != nil {
log.WithError(err).Error(err, "failed to get validator")
return ""
}
return fmt.Sprintf(validatorActive.responseText, reqIndex, validator.ActivationEpoch)
case validatorSlashed.command, validatorSlashed.shorthand:
validator, err := beaconClient.GetValidator(context.Background(), req)
if err != nil {
log.WithError(err).Error(err, "failed to get validator")
return ""
}
resultText := "not slashed"
if validator.Slashed {
resultText = "slashed"
}
return fmt.Sprintf(validatorSlashed.responseText, reqIndex, resultText)
default:
return ""
}
}