This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
302 lines (263 loc) · 7.63 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// AppName to store application name
var AppName string = "flexassistant"
// AppVersion to set version at compilation time
var AppVersion string = "9999"
// GitCommit to set git commit at compilation time (can be empty)
var GitCommit string
// GoVersion to set Go version at compilation time
var GoVersion string
// MaxPayments defaults
const MaxPayments = 10
// MaxBlocks defaults
const MaxBlocks = 50
// initialize logging
func init() {
log.SetOutput(os.Stdout)
rand.Seed(time.Now().UnixNano())
}
func main() {
config := NewConfig()
version := flag.Bool("version", false, "Print version and exit")
quiet := flag.Bool("quiet", false, "Log errors only")
verbose := flag.Bool("verbose", false, "Print more logs")
debug := flag.Bool("debug", false, "Print even more logs")
configFileName := flag.String("config", AppName+".yaml", "Configuration file name")
flag.Parse()
if *version {
showVersion()
return
}
// Logs and configuration
log.SetLevel(log.WarnLevel)
if *debug {
log.SetLevel(log.DebugLevel)
}
if *verbose {
log.SetLevel(log.InfoLevel)
}
if *quiet {
log.SetLevel(log.ErrorLevel)
}
if *configFileName != "" {
err := config.ReadFile(*configFileName)
if err != nil {
log.Fatalf("Cannot parse configuration file: %v", err)
}
}
// Database
var db *gorm.DB
db, err := NewDatabase(config.DatabaseFile)
if err != nil {
log.Fatalf("Could not create database: %v", err)
}
if err := CreateDatabaseObjects(db); err != nil {
log.Fatalf("Could not create objects: %v", err)
}
if err := EnsureDatabaseRetention(db); err != nil {
log.Fatalf("Could not cleanup objects from database: %v", err)
}
// API client
client := NewFlexpoolClient()
// Notifications
notifier, err := NewTelegramNotifier(&config.TelegramConfig, &config.Notifications)
if err != nil {
log.Fatalf("Could not create notifier: %v", err)
}
executed, err := notifier.NotifyTest(*client)
if err != nil {
log.Fatalf("Could not send test notifications: %v", err)
}
if executed {
log.Debug("Exit after sending test notifications")
return
}
// Limits
var maxPayments int
if config.MaxPayments > 0 {
maxPayments = config.MaxPayments
} else {
maxPayments = MaxPayments
}
var maxBlocks int
if config.MaxBlocks > 0 {
maxBlocks = config.MaxBlocks
} else {
maxBlocks = MaxBlocks
}
// Handle miners
for _, configuredMiner := range config.Miners {
miner, err := NewMiner(configuredMiner.Address, configuredMiner.Coin)
if err != nil {
log.Warnf("Could not parse miner: %v", err)
continue
}
var dbMiner Miner
trx := db.Where(Miner{Address: miner.Address}).Attrs(Miner{Address: miner.Address, Coin: miner.Coin}).FirstOrCreate(&dbMiner)
if trx.Error != nil {
log.Warnf("Cannot fetch miner %s from database: %v", miner, trx.Error)
}
// Balance management
if configuredMiner.EnableBalance {
// Balance have never been persisted, skip notifications
notify := true
if dbMiner.Balance == 0 {
notify = false
}
log.Debugf("Fetching balance for %s", miner)
balance, err := client.MinerBalance(miner.Coin, miner.Address)
if err != nil {
log.Warnf("Could not fetch unpaid balance: %v", err)
continue
}
log.Debugf("Unpaid balance %.0f", balance)
miner.Balance = balance
if miner.Balance != dbMiner.Balance {
dbMiner.Balance = balance
if trx = db.Save(&dbMiner); trx.Error != nil {
log.Warnf("Cannot update miner: %v", trx.Error)
continue
}
if notify {
err = notifier.NotifyBalance(*miner)
if err != nil {
log.Warnf("Cannot send notification: %v", err)
continue
}
log.Infof("Balance notification sent for %s", miner)
}
}
}
// Payments management
if configuredMiner.EnablePayments {
// Payments have never been persisted, skip notifications
notify := true
if dbMiner.LastPaymentTimestamp == 0 {
notify = false
}
log.Debugf("Fetching payments for %s", miner)
payments, err := client.MinerPayments(miner.Coin, miner.Address, maxPayments)
if err != nil {
log.Warnf("Could not fetch payments: %v", err)
continue
}
for _, payment := range payments {
log.Debugf("Fetched %s", payment)
if dbMiner.LastPaymentTimestamp < payment.Timestamp {
dbMiner.LastPaymentTimestamp = payment.Timestamp
if trx = db.Save(&dbMiner); trx.Error != nil {
log.Warnf("Cannot update miner: %v", trx.Error)
continue
}
if notify {
err = notifier.NotifyPayment(*miner, *payment)
if err != nil {
log.Warnf("Cannot send notification: %v", err)
continue
}
log.Infof("Payment notification sent for %s", payment)
}
}
}
}
// Offline workers management
if configuredMiner.EnableOfflineWorkers {
log.Debugf("Fetching workers for %s", miner)
workers, err := client.MinerWorkers(miner.Coin, miner.Address)
if err != nil {
log.Warnf("Could not fetch workers: %v", err)
continue
}
for _, worker := range workers {
log.Debugf("Fetched %s", worker)
var dbWorker Worker
trx := db.Where(Worker{MinerAddress: miner.Address, Name: worker.Name}).Attrs(Worker{MinerAddress: miner.Address, Name: worker.Name}).FirstOrCreate(&dbWorker)
if trx.Error != nil {
log.Warnf("Cannot fetch worker %s from database: %v", worker, trx.Error)
continue
}
if dbWorker.IsOnline != worker.IsOnline {
// Skip first notification
notify := true
if dbWorker.LastSeen.IsZero() {
notify = false
}
dbWorker.IsOnline = worker.IsOnline
dbWorker.LastSeen = worker.LastSeen
if trx = db.Save(&dbWorker); trx.Error != nil {
log.Warnf("Cannot update worker: %v", trx.Error)
continue
}
if notify {
err = notifier.NotifyOfflineWorker(*worker)
if err != nil {
log.Warnf("Cannot send notification: %v", err)
continue
}
log.Infof("Offline worker notification sent for %s", worker)
}
}
}
}
}
// Handle pools
for _, configuredPool := range config.Pools {
pool := NewPool(configuredPool.Coin)
var dbPool Pool
trx := db.Where(Pool{Coin: pool.Coin}).Attrs(Pool{Coin: pool.Coin}).FirstOrCreate(&dbPool)
if trx.Error != nil {
log.Warnf("Cannot fetch pool %s from database: %v", pool, trx.Error)
}
// Blocks management
if configuredPool.EnableBlocks {
// Block number has never been persisted, skip notifications
notify := true
if dbPool.LastBlockNumber == 0 {
notify = false
}
log.Debugf("Fetching blocks for %s", pool)
blocks, err := client.PoolBlocks(pool.Coin, maxBlocks)
if err != nil {
log.Warnf("Could not fetch blocks: %v", err)
} else {
for _, block := range blocks {
log.Debugf("Fetched %s", block)
if dbPool.LastBlockNumber < block.Number {
dbPool.LastBlockNumber = block.Number
if trx = db.Save(&dbPool); trx.Error != nil {
log.Warnf("Cannot update pool: %v", trx.Error)
continue
}
convertedReward, err := ConvertCurrency(pool.Coin, block.Reward)
if err != nil {
log.Warnf("Reward for block %d cannot be converted: %v", block.Number, err)
}
if notify && convertedReward >= configuredPool.MinBlockReward {
err = notifier.NotifyBlock(*pool, *block)
if err != nil {
log.Warnf("Cannot send notification: %v", err)
continue
}
log.Infof("Block notification sent for %s", block)
}
}
}
}
}
}
}
func showVersion() {
if GitCommit != "" {
AppVersion = fmt.Sprintf("%s-%s", AppVersion, GitCommit)
}
fmt.Printf("%s version %s (compiled with %s)\n", AppName, AppVersion, GoVersion)
}