forked from a41-official/cosmos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
327 lines (291 loc) · 9.32 KB
/
wallet.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"net/http"
"strconv"
"sync"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)
func WalletHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
address := r.URL.Query().Get("address")
myAddress, err := sdk.AccAddressFromBech32(address)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get address")
return
}
walletBalanceGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_wallet_balance",
Help: "Balance of the Cosmos-based blockchain wallet",
ConstLabels: ConstLabels,
},
[]string{"address", "denom"},
)
walletDelegationGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_wallet_delegations",
Help: "Delegations of the Cosmos-based blockchain wallet",
ConstLabels: ConstLabels,
},
[]string{"address", "denom", "delegated_to"},
)
walletRedelegationGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_wallet_redelegations",
Help: "Redlegations of the Cosmos-based blockchain wallet",
ConstLabels: ConstLabels,
},
[]string{"address", "denom", "redelegated_from", "redelegated_to"},
)
walletUnbondingsGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_wallet_unbondings",
Help: "Unbondings of the Cosmos-based blockchain wallet",
ConstLabels: ConstLabels,
},
[]string{"address", "denom", "unbonded_from"},
)
walletRewardsGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_wallet_rewards",
Help: "Rewards of the Cosmos-based blockchain wallet",
ConstLabels: ConstLabels,
},
[]string{"address", "denom", "validator_address"},
)
registry := prometheus.NewRegistry()
registry.MustRegister(walletBalanceGauge)
registry.MustRegister(walletDelegationGauge)
registry.MustRegister(walletUnbondingsGauge)
registry.MustRegister(walletRedelegationGauge)
registry.MustRegister(walletRewardsGauge)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("address", address).
Msg("Started querying balance")
queryStart := time.Now()
bankClient := banktypes.NewQueryClient(grpcConn)
bankRes, err := bankClient.AllBalances(
context.Background(),
&banktypes.QueryAllBalancesRequest{Address: myAddress.String()},
)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get balance")
return
}
sublogger.Debug().
Str("address", address).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying balance")
for _, balance := range bankRes.Balances {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(balance.Amount.String(), 64); err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not parse balance")
} else {
walletBalanceGauge.With(prometheus.Labels{
"address": address,
"denom": Denom,
}).Set(value / DenomCoefficient)
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("address", address).
Msg("Started querying delegations")
queryStart := time.Now()
stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingRes, err := stakingClient.DelegatorDelegations(
context.Background(),
&stakingtypes.QueryDelegatorDelegationsRequest{DelegatorAddr: myAddress.String()},
)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get delegations")
return
}
sublogger.Debug().
Str("address", address).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying delegations")
for _, delegation := range stakingRes.DelegationResponses {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(delegation.Balance.Amount.String(), 64); err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get delegation")
} else {
walletDelegationGauge.With(prometheus.Labels{
"address": address,
"denom": Denom,
"delegated_to": delegation.Delegation.ValidatorAddress,
}).Set(value / DenomCoefficient)
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("address", address).
Msg("Started querying unbonding delegations")
queryStart := time.Now()
stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingRes, err := stakingClient.DelegatorUnbondingDelegations(
context.Background(),
&stakingtypes.QueryDelegatorUnbondingDelegationsRequest{DelegatorAddr: myAddress.String()},
)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get unbonding delegations")
return
}
sublogger.Debug().
Str("address", address).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying unbonding delegations")
for _, unbonding := range stakingRes.UnbondingResponses {
var sum float64 = 0
for _, entry := range unbonding.Entries {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(entry.Balance.String(), 64); err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not parse unbonding delegation")
} else {
sum += value
}
}
walletUnbondingsGauge.With(prometheus.Labels{
"address": unbonding.DelegatorAddress,
"denom": Denom, // unbonding does not have denom in response for some reason
"unbonded_from": unbonding.ValidatorAddress,
}).Set(sum / DenomCoefficient)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("address", address).
Msg("Started querying redelegations")
queryStart := time.Now()
stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingRes, err := stakingClient.Redelegations(
context.Background(),
&stakingtypes.QueryRedelegationsRequest{DelegatorAddr: myAddress.String()},
)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get redelegations")
return
}
sublogger.Debug().
Str("address", address).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying redelegations")
for _, redelegation := range stakingRes.RedelegationResponses {
var sum float64 = 0
for _, entry := range redelegation.Entries {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(entry.Balance.String(), 64); err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not parse redelegation")
} else {
sum += value
}
}
walletRedelegationGauge.With(prometheus.Labels{
"address": redelegation.Redelegation.DelegatorAddress,
"denom": Denom, // redelegation does not have denom in response for some reason
"redelegated_from": redelegation.Redelegation.ValidatorSrcAddress,
"redelegated_to": redelegation.Redelegation.ValidatorDstAddress,
}).Set(sum / DenomCoefficient)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().
Str("address", address).
Msg("Started querying rewards")
queryStart := time.Now()
distributionClient := distributiontypes.NewQueryClient(grpcConn)
distributionRes, err := distributionClient.DelegationTotalRewards(
context.Background(),
&distributiontypes.QueryDelegationTotalRewardsRequest{DelegatorAddress: myAddress.String()},
)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get rewards")
return
}
sublogger.Debug().
Str("address", address).
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying rewards")
for _, reward := range distributionRes.Rewards {
for _, entry := range reward.Reward {
// because cosmos's dec doesn't have .toFloat64() method or whatever and returns everything as int
if value, err := strconv.ParseFloat(entry.Amount.String(), 64); err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not parse reward")
} else {
walletRewardsGauge.With(prometheus.Labels{
"address": address,
"denom": Denom,
"validator_address": reward.ValidatorAddress,
}).Set(value / DenomCoefficient)
}
}
}
}()
wg.Wait()
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
sublogger.Info().
Str("method", "GET").
Str("endpoint", "/metrics/wallet?address="+address).
Float64("request-time", time.Since(requestStart).Seconds()).
Msg("Request processed")
}