-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
253 lines (221 loc) · 5.2 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
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
package main
import (
"bufio"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"net/http"
"os"
"strings"
"strconv"
"time"
"io/ioutil"
"encoding/json"
"sync"
"sort"
)
var (
allWatching []*Watching
port string
updates string
eth *ethclient.Client
)
type Watching struct {
Name string
Address string
Balance string
Host string
}
var tokenList []TokenList
//
// Connect to geth server
func ConnectionToGeth(url string) error {
var err error
eth, err = ethclient.Dial(url)
return err
}
func tokenCaller(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 GetTokenBalance(token, address string, decimals int) string {
caller, _ := tokenCaller(common.HexToAddress(token))
balance, _ := caller.BalanceOf(nil, common.HexToAddress(address))
corrected := ToDecimals(balance, decimals)
return corrected
}
type Order struct {
Index int
Data string
}
func FindOrder(o []Order, index int) Order {
var n Order
for _, v := range o {
if v.Index == index {
n = v
}
}
return n
}
func Resort(o []Order) []Order {
order := []int{}
for _, v := range o {
order = append(order, v.Index)
}
sort.Ints(order)
newOrder := []Order{}
for _, v := range order {
newOrder = append(newOrder, FindOrder(o, v))
}
return newOrder
}
func exp_func(x int, y int64) (value *big.Int) {
exp:=strconv.FormatInt(y, 2)
v := big.NewInt(int64(x))
for i := 1; i < len(exp); i++ {
v.Mul(v, v)
if(exp[i]=='1') {
v.Mul(v, big.NewInt(int64(x)))
}
}
return v
}
//
// CONVERT USING THE DECIMALS
func ToDecimals(o *big.Int, decimals int) string{
if o.String()=="0" {
return "0"
}
pul, int := big.NewFloat(0), big.NewFloat(0)
int.SetInt(o)
bigDec := exp_func(10, int64(decimals))
fDec := new(big.Float).SetInt(bigDec)
pul = new(big.Float).Quo(int, fDec)
result, _ := big.NewFloat(0).SetString(pul.String())
text := fmt.Sprintf(result.Text('f', decimals))
return text
}
//
// HTTP response handler for /metrics
func MetricsHttp(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, strings.Join(outData, "\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
}
//
// Open the addresses.txt file (name:address)
func OpenTokenList(filename string) error {
var jsonList []TokenList
file, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(file, &jsonList)
if err != nil {
return err
}
tokenList = jsonList
return err
}
var outData []string
func main() {
gethUrl := os.Getenv("GETH")
port = os.Getenv("PORT")
err := OpenAddresses("addresses.txt")
if err != nil {
panic(err)
}
err = OpenTokenList("tokens-list.json")
if err != nil {
panic(err)
}
err = ConnectionToGeth(gethUrl)
if err != nil {
panic(err)
}
// check address balances
go func() {
for {
pendingData := []Order{}
pendingText := []string{}
t1 := time.Now()
guard := make(chan struct{}, 16)
for _, v := range allWatching {
wg := new(sync.WaitGroup)
for i, tk := range tokenList {
token := tk
wg.Add(1)
go func(i int, tk TokenList, v *Watching, wg *sync.WaitGroup) {
guard <- struct{}{}
balance := GetTokenBalance(tk.Address, v.Address, tk.Decimal)
data := fmt.Sprintf("token_balance{host=\"%v\",name=\"%v\",symbol=\"%v\",contract=\"%v\",address=\"%v\"} %v", v.Host, v.Name, tk.Symbol, tk.Address, v.Address, balance)
order := Order{i, data}
pendingData = append(pendingData, order)
<-guard
wg.Done()
}(i, token, v, wg)
}
wg.Wait()
pendingData = Resort(pendingData)
watchingText := OrderToString(pendingData)
pendingText = append(pendingText, watchingText)
pendingData = []Order{}
}
t2 := time.Now()
diff := t2.Sub(t1)
data := fmt.Sprintf("token_query_seconds{name=\"%v\"} %v", "all", diff.Seconds())
pendingText = append(pendingText, data)
outData = pendingText
fmt.Printf("Token balance queries completed in %f seconds\n", diff.Seconds())
time.Sleep(600 * time.Second)
}
}()
fmt.Printf("ETHexporter has started on port %v using Geth server: %v\n", port, gethUrl)
http.HandleFunc("/metrics", MetricsHttp)
panic(http.ListenAndServe("0.0.0.0:"+port, nil))
}
func OrderToString(orders []Order) string {
var outgoing []string
for _, v := range orders {
outgoing = append(outgoing, v.Data)
}
return strings.Join(outgoing, "\n")
}
type Tokens struct {
List []TokenList
}
type TokenList struct {
Address string `json:"address"`
Symbol string `json:"symbol"`
Decimal int `json:"decimal"`
Type string `json:"type"`
}