forked from hunterlong/ethexporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (140 loc) · 3.55 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"net/http"
"os"
"strings"
"time"
)
var (
allWatching []*Watching
port string
updates string
prefix string
loadSeconds float64
totalLoaded int64
eth *ethclient.Client
)
type Watching struct {
Name string
Address string
Balance string
Host string
}
//
// Connect to geth server
func ConnectionToGeth(url string) error {
var err error
eth, err = ethclient.Dial(url)
return err
}
//
// Fetch ETH balance from Geth server
func GetEthBalance(address string) *big.Float {
balance, err := eth.BalanceAt(context.TODO(), common.HexToAddress(address), nil)
if err != nil {
fmt.Printf("Error fetching ETH Balance for address: %v\n", address)
}
return ToEther(balance)
}
//
// Fetch ETH balance from Geth server
func CurrentBlock() uint64 {
block, err := eth.BlockByNumber(context.TODO(), nil)
if err != nil {
fmt.Printf("Error fetching current block height: %v\n", err)
return 0
}
return block.NumberU64()
}
//
// CONVERTS WEI TO ETH
func ToEther(o *big.Int) *big.Float {
pul, int := big.NewFloat(0), big.NewFloat(0)
int.SetInt(o)
pul.Mul(big.NewFloat(0.000000000000000001), int)
return pul
}
//
// HTTP response handler for /metrics
func MetricsHttp(w http.ResponseWriter, r *http.Request) {
var allOut []string
total := big.NewFloat(0)
for _, v := range allWatching {
if v.Balance == "" {
v.Balance = "0"
}
bal := big.NewFloat(0)
bal.SetString(v.Balance)
total.Add(total, bal)
allOut = append(allOut, fmt.Sprintf("%veth_balance{host=\"%v\",name=\"%v\",address=\"%v\"} %v", prefix, v.Host, v.Name, v.Address, v.Balance))
}
allOut = append(allOut, fmt.Sprintf("%veth_balance_total %0.18f", prefix, total))
allOut = append(allOut, fmt.Sprintf("%veth_load_seconds %0.2f", prefix, loadSeconds))
allOut = append(allOut, fmt.Sprintf("%veth_loaded_addresses %v", prefix, totalLoaded))
allOut = append(allOut, fmt.Sprintf("%veth_total_addresses %v", prefix, len(allWatching)))
fmt.Fprintln(w, strings.Join(allOut, "\n"))
}
//
// Open the addresses.txt file (name:address)
func OpenAddresses(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
object := strings.Split(scanner.Text(), ":")
if common.IsHexAddress(object[1]) {
w := &Watching{
Name: object[0],
Address: object[1],
Host: object[2],
}
allWatching = append(allWatching, w)
}
}
if err := scanner.Err(); err != nil {
return err
}
return err
}
func main() {
gethUrl := os.Getenv("GETH")
port = os.Getenv("PORT")
prefix = os.Getenv("PREFIX")
err := OpenAddresses("addresses.txt")
if err != nil {
panic(err)
}
err = ConnectionToGeth(gethUrl)
if err != nil {
panic(err)
}
// check address balances
go func() {
for {
totalLoaded = 0
t1 := time.Now()
fmt.Printf("Checking %v wallets...\n", len(allWatching))
for _, v := range allWatching {
v.Balance = GetEthBalance(v.Address).String()
totalLoaded++
}
t2 := time.Now()
loadSeconds = t2.Sub(t1).Seconds()
fmt.Printf("Finished checking %v wallets in %0.0f seconds, sleeping for %v seconds.\n", len(allWatching), loadSeconds, 15)
time.Sleep(600 * time.Second)
}
}()
block := CurrentBlock()
fmt.Printf("ETHexporter has started on port %v using Geth server: %v at block #%v\n", port, gethUrl, block)
http.HandleFunc("/metrics", MetricsHttp)
panic(http.ListenAndServe("0.0.0.0:"+port, nil))
}