This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
arkgopool.go
363 lines (310 loc) · 10.2 KB
/
arkgopool.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"sync"
log "github.com/sirupsen/logrus"
"github.com/kristjank/ark-go/arkcoin"
"github.com/kristjank/ark-go/core"
"github.com/fatih/color"
"github.com/spf13/viper"
"github.com/asdine/storm"
)
var arkclient = core.NewArkClient(nil)
var reader = bufio.NewReader(os.Stdin)
var arkpooldb *storm.DB
var wg sync.WaitGroup
var wgConfirmations sync.WaitGroup
var ArkGoPoolVersion string
func initLogger() {
// Log as JSON instead of the default ASCII formatter.
//log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})
// You could set this to any `io.Writer` such as a file
file, err := os.OpenFile("log/arkgo-pool.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
log.SetOutput(io.MultiWriter(file))
} else {
log.Error("Failed to log to file, using default stderr")
}
}
func initializeBoltClient() {
var err error
arkpooldb, err = storm.Open(viper.GetString("client.dbfilename"))
if err != nil {
log.Panic(err.Error())
broadCastServiceMode(false)
}
log.Println("DB Opened at:", arkpooldb.Path)
//defer arkpooldb.Close()
}
func broadCastServiceMode(status bool) {
var url string
if status {
url = "http://127.0.0.1:" + strconv.Itoa(viper.GetInt("server.port")) + "/service/start"
} else {
arkpooldb.Close()
url = "http://127.0.0.1:" + strconv.Itoa(viper.GetInt("server.port")) + "/service/stop"
}
res, err := http.Get(url)
if err != nil {
log.Error("Error setting service mode on arkgopool server", err.Error(), url)
} else {
log.Info("Status mode set to", status, res.StatusCode)
}
}
func readAccountData() (string, string) {
fmt.Println("\nEnter account passphrase")
fmt.Print("-->")
pass1, _ := reader.ReadString('\n')
re := regexp.MustCompile("\r?\n")
pass1 = re.ReplaceAllString(pass1, "")
pass2 := ""
key := arkcoin.NewPrivateKeyFromPassword(pass1, arkcoin.ActiveCoinConfig)
accountResp, _, _ := arkclient.GetAccount(core.AccountQueryParams{Address: key.PublicKey.Address()})
deleResp, _, _ := arkclient.GetDelegate(core.DelegateQueryParams{PublicKey: string(key.PublicKey.Serialize())})
if !accountResp.Success {
log.Info("Error getting account data for delegate: " + deleResp.SingleDelegate.Username + "[" + key.PublicKey.Address() + "]")
return "error", ""
}
if accountResp.Account.SecondSignature == 1 {
fmt.Println("\nEnter second account passphrase for delegate: " + deleResp.SingleDelegate.Username + "[" + key.PublicKey.Address() + "]")
fmt.Print("-->")
pass2, _ = reader.ReadString('\n')
re := regexp.MustCompile("\r?\n")
pass2 = re.ReplaceAllString(pass2, "")
}
return pass1, pass2
}
func loadConfig() {
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("settings") // path to look for the config file in
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil {
log.Info("No productive config found - loading sample")
// try to load sample config
viper.SetConfigName("sample.config")
viper.AddConfigPath("settings")
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
log.Info("No configuration file loaded - using defaults")
}
}
viper.SetDefault("delegate.address", "")
viper.SetDefault("delegate.pubkey", "")
viper.SetDefault("delegate.Daddress", "")
viper.SetDefault("delegate.Dpubkey", "")
viper.SetDefault("voters.shareRatio", 0.0)
viper.SetDefault("voters.txdescription", "share tx by ark-go")
viper.SetDefault("voters.fidelity", true)
viper.SetDefault("voters.fidelityLimit", 24)
viper.SetDefault("voters.minamount", 0.0)
viper.SetDefault("voters.minVoteTime", 0)
viper.SetDefault("voters.deductTxFees", true)
viper.SetDefault("voters.blocklist", "")
viper.SetDefault("voters.capBalance", false)
viper.SetDefault("voters.balanceCapAmount", 0.0)
viper.SetDefault("voters.whitelist", "")
viper.SetDefault("voters.blockBalanceCap", true)
viper.SetDefault("costs.address", "")
viper.SetDefault("costs.shareRatio", 0.0)
viper.SetDefault("costs.txdescription", "cost tx by ark-go")
viper.SetDefault("costs.Daddress", "")
viper.SetDefault("reserve.address", "")
viper.SetDefault("reserve.shareRatio", 0.0)
viper.SetDefault("reserve.txdescription", "reserve tx by ark-go")
viper.SetDefault("reserve.Daddress", "")
viper.SetDefault("personal.address", "")
viper.SetDefault("personal.shareRatio", 0.0)
viper.SetDefault("personal.txdescription", "personal tx by ark-go")
viper.SetDefault("personal.Daddress", "")
viper.SetDefault("client.network", "DEVNET")
viper.SetDefault("client.dbFilename", "payment.db")
viper.SetDefault("client.multibroadcast", 10)
viper.SetDefault("client.statistics", true)
viper.SetDefault("client.statPeer", "164.8.251.91")
viper.SetDefault("client.statPort", 54010)
viper.SetDefault("client.nodeversion", "1.0.1")
viper.SetDefault("client.Dnodeversion", "1.1.0")
}
//////////////////////////////////////////////////////////////////////////////
//GUI RELATED STUFF
func pause() {
color.Set(color.FgHiYellow)
fmt.Println("")
fmt.Print("Press 'ENTER' key to continue... ")
//bufio.NewReader(os.Stdin).ReadBytes('\n')
reader.ReadString('\n')
}
func clearScreen() {
cmd := exec.Command("clear")
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "cls")
}
cmd.Stdout = os.Stdout
cmd.Run()
}
func printNetworkInfo() {
color.Set(color.FgHiCyan)
if core.EnvironmentParams.Network.Type == core.MAINNET {
fmt.Println("Connected to ARK MAINNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
log.Info("Connected to ARK MAINNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
}
if core.EnvironmentParams.Network.Type == core.DEVNET {
fmt.Println("Connected to ARK DEVNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
log.Info("Connected to ARK DEVNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
}
if core.EnvironmentParams.Network.Type == core.KAPU {
fmt.Println("Connected to KAPU MAINNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
log.Info("Connected to KAPU MAINNET on peer:", core.BaseURL, "| ARKGoPool version", ArkGoPoolVersion)
}
}
func printBanner() {
color.Set(color.FgHiGreen)
dat, _ := ioutil.ReadFile("settings/banner.txt")
fmt.Print(string(dat))
}
func printMenu() {
log.Info("--------- MAIN MENU ----------------")
clearScreen()
printBanner()
printNetworkInfo()
color.Set(color.FgHiYellow)
fmt.Println("")
fmt.Println("\t1-Display contributors")
fmt.Println("\t2-Send reward payments")
fmt.Println("\t3-Link account")
fmt.Println("\t4-List payment history")
fmt.Println("\t5-Send bonus payments")
fmt.Println("\t6-Switch networks ARK")
fmt.Println("\t7-Switch networks KAPU")
fmt.Println("\t8-Check transaction confirmations (last payment run)")
fmt.Println("\t0-Exit")
fmt.Println("")
fmt.Print("\tSelect option [0-9]:")
color.Unset()
}
func main() {
//sending ARKGO Server that we are working with payments
//setting the version
ArkGoPoolVersion = "v0.8.1"
// Load configration and defaults
// Order is important
loadConfig()
broadCastServiceMode(true)
initLogger()
log.Info("=============================================================================")
log.Info("ARKGO client starting")
log.Info("ArkApiClient connected, active peer: ", arkclient.GetActivePeer())
initializeBoltClient()
//switch to preset network
if viper.GetString("client.network") == "DEVNET" {
arkclient = arkclient.SetActiveConfiguration(core.DEVNET)
}
//switch to preset network
if viper.GetString("client.network") == "KAPU" {
arkclient = arkclient.SetActiveConfiguration(core.KAPU)
}
//SILENT MODE CHECKING AND AUTOMATION RUNNING
modeSilentPtr := flag.Bool("silent", false, "Is silent mode")
//autoPayment := flag.Bool("autopay", true, "Process auto payment")
flag.Parse()
log.Info(flag.Args())
if *modeSilentPtr {
log.Info("Silent Mode active")
log.Info("Starting to send payments")
SendPayments(true)
log.Info("Waiting for threads to complete")
color.Unset()
wg.Wait()
log.Info("Exiting silent mode and arkgopool")
//sending ARKGO Server that we are working with payments
broadCastServiceMode(false)
os.Exit(1985)
}
var choice = 1
for choice != 0 {
//pause()
printMenu()
//fmt.Scan(&choice)
fmt.Fscan(reader, &choice)
reader.ReadString('\n')
switch choice {
case 1:
clearScreen()
color.Set(color.FgMagenta)
DisplayCalculatedVoteRatio()
color.Unset()
case 2:
clearScreen()
color.Set(color.FgHiGreen)
SendPayments(false)
wg.Wait()
color.Unset()
case 6:
if core.EnvironmentParams.Network.Type == core.MAINNET {
arkclient = arkclient.SetActiveConfiguration(core.DEVNET)
} else {
arkclient = arkclient.SetActiveConfiguration(core.MAINNET)
}
case 3:
clearScreen()
save(readAccountData())
color.Set(color.FgHiGreen)
log.Info("Account succesfully linked")
fmt.Println("Account succesfully linked")
pause()
color.Unset()
case 5:
clearScreen()
color.Set(color.FgHiGreen)
fmt.Println("\nEnter bonus amount to send to loyal voters")
fmt.Print("-->")
sAmount2Send, err := reader.ReadString('\n')
re := regexp.MustCompile("\r?\n")
sAmount2Send = re.ReplaceAllString(sAmount2Send, "")
fmt.Println("\nEnter bonus transaction description (vendor field)")
fmt.Print("-->")
txBonusDesc, err := reader.ReadString('\n')
txBonusDesc = re.ReplaceAllString(txBonusDesc, "")
iAmount2Send, err := strconv.Atoi(sAmount2Send)
if err != nil {
log.Error("Stopping bonus payment", err.Error())
return
}
SendBonusPayment(iAmount2Send, txBonusDesc)
pause()
color.Unset()
case 7:
arkclient = arkclient.SetActiveConfiguration(core.KAPU)
case 8:
clearScreen()
color.Set(color.FgHiGreen)
fmt.Println("Almost there, but 1.0.2 was released to fast...")
fmt.Println("Will follow up soon...")
pause()
color.Unset()
case 4:
clearScreen()
color.Set(color.FgHiGreen)
listPaymentsDB()
pause()
//listPaymentsDetailsFromDB()
//pause()
color.Unset()
}
}
color.Unset()
broadCastServiceMode(false)
log.Info("Exiting arkgopool....")
}