-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
177 lines (148 loc) · 5.28 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
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
package main
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
querytypes "github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tb "gopkg.in/tucnak/telebot.v2"
)
func getValidatorInfo(message *tb.Message) {
args := strings.SplitAfterN(message.Text, " ", 2)
if len(args) < 2 {
log.Info().Msg("getWalletInfo: args length < 2")
sendMessage(message, "Usage: validator <validator operator address or name>")
return
}
address := args[1]
log.Debug().Str("address", address).Msg("getValidatorInfo: address")
// --------------------------------
validator, err := getValidator(address)
if err != nil {
log.Error().Err(err).Msg("Could not get validator")
sendMessage(message, "Could not find validator")
return
}
rank, err := getValidatorRank(validator)
if err != nil {
log.Error().Err(err).Msg("Could not get validator rank")
sendMessage(message, "Could not find validator rank")
return
}
// --------------------------------
var sb strings.Builder
sb.WriteString(fmt.Sprintf("<code>%s</code>\n", validator.Description.Moniker))
sb.WriteString(fmt.Sprintf("<a href=\"https://mintscan.io/%s/validators/%s\">Mintscan</a>\n\n", MintscanPrefix, validator.OperatorAddress))
sb.WriteString(fmt.Sprintf("<strong>Moniker: </strong><code>%s</code>\n", validator.Description.Moniker))
sb.WriteString(fmt.Sprintf("<strong>Operator address: </strong><code>%s</code>\n", validator.OperatorAddress))
sb.WriteString(fmt.Sprintf("<strong>Description: </strong><code>%s</code>\n", validator.Description.Details))
sb.WriteString(fmt.Sprintf("<strong>Website: </strong><code>%s</code>\n", validator.Description.Website))
sb.WriteString(fmt.Sprintf("<strong>Security contact: </strong><code>%s</code>\n", validator.Description.SecurityContact))
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(validator.Commission.CommissionRates.Rate.String(), 64); err != nil {
log.Error().
Str("address", address).
Err(err).
Msg("Could not parse balance")
sendMessage(message, "Could not parse balance")
return
} else {
sb.WriteString(fmt.Sprintf("<strong>Commission rate: </strong><code>%.1f%%</code>\n", value*100))
}
if value, err := strconv.ParseFloat(validator.DelegatorShares.String(), 64); err != nil {
log.Error().
Str("address", address).
Err(err).
Msg("Could not parse delegator shares")
sendMessage(message, "Could not parse delegator shares")
return
} else {
sb.WriteString(fmt.Sprintf(
"\n<strong>Total tokens delegated: </strong><code>%.1f %s</code>\n",
value/DenomCoefficient,
Denom,
))
}
if validator.Jailed {
sb.WriteString("<strong>Rank: </strong>JAILED\n")
} else {
sb.WriteString(fmt.Sprintf("<strong>Rank: </strong>%d\n", rank))
}
sendMessage(message, sb.String())
log.Info().
Str("query", address).
Str("validator", validator.OperatorAddress).
Str("user", message.Sender.Username).
Msg("Successfully returned validator info")
}
func getValidator(address string) (stakingtypes.Validator, error) {
if strings.HasPrefix(address, ValidatorPrefix) {
log.Debug().Str("address", address).Msg("Searching validator by address")
stakingClient := stakingtypes.NewQueryClient(grpcConn)
validatorResponse, err := stakingClient.Validator(
context.Background(),
&stakingtypes.QueryValidatorRequest{ValidatorAddr: address},
)
if err != nil {
log.Error().
Str("address", address).
Err(err).
Msg("Could not get validator")
return stakingtypes.Validator{}, err
}
return validatorResponse.Validator, nil
}
log.Debug().Str("address", address).Msg("Searching validator by name")
stakingClient := stakingtypes.NewQueryClient(grpcConn)
validatorsResponse, err := stakingClient.Validators(
context.Background(),
&stakingtypes.QueryValidatorsRequest{
Pagination: &querytypes.PageRequest{Limit: PaginationLimit},
},
)
if err != nil {
log.Error().
Str("address", address).
Err(err).
Msg("Could not get validators")
return stakingtypes.Validator{}, err
}
for _, validator := range validatorsResponse.Validators {
if strings.Contains(
strings.ToLower(validator.Description.Moniker),
strings.ToLower(address),
) {
log.Debug().Str("address", address).Str("moniker", validator.Description.Moniker).Msg("Found validator")
return validator, nil
}
}
return stakingtypes.Validator{}, fmt.Errorf("validator is not found")
}
func getValidatorRank(validator stakingtypes.Validator) (int, error) {
stakingClient := stakingtypes.NewQueryClient(grpcConn)
validatorsResponse, err := stakingClient.Validators(
context.Background(),
&stakingtypes.QueryValidatorsRequest{
Pagination: &querytypes.PageRequest{Limit: PaginationLimit},
},
)
if err != nil {
log.Error().
Str("address", validator.OperatorAddress).
Err(err).
Msg("Could not get validators")
return 0, err
}
validators := validatorsResponse.Validators
sort.Slice(validators[:], func(i, j int) bool {
return validators[i].DelegatorShares.RoundInt64() > validators[j].DelegatorShares.RoundInt64()
})
for index, iteratedValidator := range validators {
if validator.OperatorAddress == iteratedValidator.OperatorAddress {
return index + 1, nil
}
}
return 0, fmt.Errorf("could not find validator rank")
}