-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.go
67 lines (60 loc) · 1.81 KB
/
balance.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
package main
import (
"context"
"fmt"
"log/slog"
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
PROM_LABEL_NETWORK_NAME = "network_name"
PROM_LABEL_CHAIN_ID = "chain_id"
PROM_LABEL_ADDRESS_OWNER = "addr_owner"
PROM_LABEL_ADDRESS = "addr"
PROM_LABEL_KIND = "kind"
)
func newBalanceCheckerJob(ep *Endpoint, addressOwner string, addressToCheck common.Address) JobFunc {
gaugeBalance := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: PROM_NAMESPACE_RECALL,
Name: "balance",
ConstLabels: ep.ConstLabels(
PROM_LABEL_ADDRESS_OWNER, addressOwner,
PROM_LABEL_ADDRESS, addressToCheck.Hex(),
PROM_LABEL_KIND, "native",
),
})
return func(logger *slog.Logger) error {
balance, err := ep.Client.BalanceAt(context.Background(), addressToCheck, nil)
if err != nil {
return fmt.Errorf("failed to get balance: %w", err)
} else {
logger.Debug("got balance", "balance", balance)
val, _ := balance.Float64()
gaugeBalance.Set(val)
}
return nil
}
}
func newErc20TokenBalanceCheckerJob(ep *ParentChainEndpoint, addressOwner string, addressToCheck common.Address) JobFunc {
gaugeBalance := promauto.NewGauge(prometheus.GaugeOpts{
Namespace: PROM_NAMESPACE_RECALL,
Name: "balance",
ConstLabels: ep.ConstLabels(
PROM_LABEL_ADDRESS_OWNER, addressOwner,
PROM_LABEL_ADDRESS, addressToCheck.Hex(),
PROM_LABEL_KIND, "erc20",
),
})
return func(logger *slog.Logger) error {
balance, err := ep.SupplySourceCaller.BalanceOf(nil, addressToCheck)
if err != nil {
return fmt.Errorf("failed to get balance: %w", err)
} else {
logger.Debug("got balance", "balance", balance)
val, _ := balance.Float64()
gaugeBalance.Set(val)
}
return nil
}
}