-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
186 lines (168 loc) · 4.53 KB
/
watcher.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
package main
import (
"context"
"fmt"
"log"
"math"
"math/big"
"net/http"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type watchItem struct {
Token string
Wallet string
Decimals int
Metric prometheus.Gauge
}
type Watcher struct {
geth map[string]*ethclient.Client
items map[string][]*watchItem
}
func (w Watcher) addGeth(name, url string) error {
eth, err := ethclient.Dial(url)
if err != nil {
return err
}
w.geth[name] = eth
return nil
}
func (w *Watcher) LoadConfig(config *ConfigYAML) error {
for name, url := range config.Chains {
if err := w.addGeth(name, url); err != nil {
return err
}
}
tokensMap := map[string]map[string]Token{}
for chain, tokens := range config.Tokens {
tokensMapChain := map[string]Token{}
for _, token := range tokens {
tokensMapChain[token.Symbol] = token
}
tokensMap[chain] = tokensMapChain
}
for _, wallet := range config.Wallets {
for chain, symbols := range wallet.TrackFor {
if _, ok := config.Chains[chain]; !ok {
return fmt.Errorf("Unknown chain '%s' in wallet '%s'", chain, wallet.Address)
}
for _, symbol := range symbols {
token, ok := tokensMap[chain][symbol]
if !ok {
return fmt.Errorf("Unknown token '%s' for chain '%s' in wallet '%s'", symbol, chain, wallet.Address)
}
labels := map[string]string{}
for k, v := range wallet.Labels {
labels[k] = v
}
labels["symbol"] = symbol
labels["name"] = wallet.Name
labels["wallet"] = wallet.Address
labels["token"] = token.Contract
labels["chain"] = chain
metric := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "token_balance",
ConstLabels: labels,
})
item := watchItem{
Token: token.Contract,
Wallet: wallet.Address,
Decimals: token.Decimal,
Metric: metric,
}
w.items[chain] = append(w.items[chain], &item)
}
}
}
return nil
}
func (w Watcher) Start(listen string) error {
registry := prometheus.NewRegistry()
for chain := range w.items {
for _, item := range w.items[chain] {
if err := registry.Register(item.Metric); err != nil {
return err
}
}
}
http.Handle(
"/metrics", promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
EnableOpenMetrics: false,
}),
)
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics
go func() {
err := http.ListenAndServe(listen, nil)
if err != nil {
log.Println("can't start listen:", err)
os.Exit(1)
}
}()
go func() {
for {
for chain := range w.items {
go func(c string) {
log.Printf("Updating tokens on '%s'", c)
for _, item := range w.items[c] {
balance := w.getTokenBalance(c, item)
item.Metric.Set(balance)
}
log.Printf("Updated tokens on '%s'", c)
}(chain)
}
time.Sleep(10 * time.Minute)
}
}()
return nil
}
func (w Watcher) Stop() error {
return nil
}
func (w *Watcher) tokenCaller(eth *ethclient.Client, address common.Address) (*MainCaller, error) {
caller, err := NewMainCaller(address, eth)
if err != nil {
return nil, err
}
return caller, err
}
// Fetch ETH balance from Geth server
func (w *Watcher) getTokenBalance(chain string, item *watchItem) float64 {
if (item.Token == "0x0000000000000000000000000000000000000000") ||
(item.Token == "") {
return w.getGasBalance(chain, item)
}
caller, err := w.tokenCaller(w.geth[chain], common.HexToAddress(item.Token))
if err != nil {
log.Printf("Can't tokenCaller for chain='%s', wallet='%s', token='%s': %v", chain, item.Wallet, item.Token, err)
return -1
}
balance, err := caller.BalanceOf(nil, common.HexToAddress(item.Wallet))
if err != nil {
log.Printf("Can't BalanceOf for chain='%s', wallet='%s', token='%s': %v", chain, item.Wallet, item.Token, err)
return -1
}
return toFloat(balance, item.Decimals)
}
func (w *Watcher) getGasBalance(chain string, item *watchItem) float64 {
balance, err := w.geth[chain].BalanceAt(
context.Background(), common.HexToAddress(item.Wallet), nil,
)
if err != nil {
log.Printf("Can't BalanceAt for chain='%s', wallet='%s', token='%s': %v", chain, item.Wallet, item.Token, err)
return -1
}
return toFloat(balance, item.Decimals)
}
func toFloat(balanceAt *big.Int, decimails int) float64 {
fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(decimails)))
result, _ := ethValue.Float64()
return result
}