-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddress_balance_stats.go
60 lines (55 loc) · 1.58 KB
/
address_balance_stats.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
package main
import (
"context"
alephium "github.com/alephium/go-sdk"
"github.com/prometheus/client_golang/prometheus"
"time"
)
type AddressBalanceStats struct {
alephiumClient *alephium.APIClient
addresses []string
metrics *metrics
}
func newAddressBalanceStats(alephiumClient *alephium.APIClient, addresses []string, metrics *metrics) (*AddressBalanceStats, error) {
handler := &AddressBalanceStats{
alephiumClient: alephiumClient,
addresses: addresses,
metrics: metrics,
}
return handler, nil
}
func (h *AddressBalanceStats) Stats(ctx context.Context) error {
err := h.doStats(ctx)
if err != nil {
return err
}
for range time.Tick(time.Minute) {
select {
case <-ctx.Done():
return nil
default:
}
err = h.doStats(ctx)
if err != nil {
return err
}
}
return nil
}
func (h *AddressBalanceStats) doStats(ctx context.Context) error {
for _, address := range h.addresses {
addressBalanceReq := h.alephiumClient.AddressesApi.GetAddressesAddressBalance(ctx, address)
balance, _, err := addressBalanceReq.Execute()
if err != nil {
return err
}
if addressBalance, ok := ALPHFromCoinString(balance.Balance); ok {
h.metrics.addressTotalBalance.With(prometheus.Labels{"address": address}).Set(addressBalance.FloatALPH())
}
if addressLockedBalance, ok := ALPHFromCoinString(balance.LockedBalance); ok {
h.metrics.addressLockedBalance.With(prometheus.Labels{"address": address}).Set(addressLockedBalance.FloatALPH())
}
h.metrics.addressUtxos.With(prometheus.Labels{"address": address}).Set(float64(balance.UtxoNum))
}
return nil
}