/
main.go
242 lines (206 loc) · 6.96 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
package main
import (
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
const (
coinsRegexp = `([G|g]obyte|[E|e]thereum|[T|t]rezarcoin|[Z|z]cash|[Z|z]classic|[Z|z]encash)`
)
func init() {
// array that contains names of gpus
var GPU_Names [numOfGPUs]string
// initialize array that contains names of gpus
GPU_Names[0] = "GPU280x"
GPU_Names[1] = "GPU380"
GPU_Names[2] = "GPUFury"
GPU_Names[3] = "GPU470"
GPU_Names[4] = "GPU480"
GPU_Names[5] = "GPU570"
GPU_Names[6] = "GPU580"
GPU_Names[7] = "GPUVega56"
GPU_Names[8] = "GPUVega64"
GPU_Names[9] = "GPU750Ti"
GPU_Names[10] = "GPU1050Ti"
GPU_Names[11] = "GPU1060"
GPU_Names[12] = "GPU1070"
GPU_Names[13] = "GPU1080"
GPU_Names[14] = "GPU1080Ti"
// initilize array that contains GPU characteristics in an order corresponding the name written in the GPU_Names array
var GPU_HashRates [numOfGPUs]GPU
GPU_HashRates[0] = GPU280x
GPU_HashRates[1] = GPU380
GPU_HashRates[2] = GPUFury
GPU_HashRates[3] = GPU470
GPU_HashRates[4] = GPU480
GPU_HashRates[5] = GPU570
GPU_HashRates[6] = GPU580
GPU_HashRates[7] = GPUVega56
GPU_HashRates[8] = GPUVega64
GPU_HashRates[9] = GPU750Ti
GPU_HashRates[10] = GPU1050Ti
GPU_HashRates[11] = GPU1060
GPU_HashRates[12] = GPU1070
GPU_HashRates[13] = GPU1080
GPU_HashRates[14] = GPU1080Ti
// initialize map that contains GPUs characteristics scraped from whattomine
GPUs = make(map[string]GPU)
for i := range GPU_Names {
GPUs[GPU_Names[i]] = GPU_HashRates[i]
}
}
func calculateHashRateAndPowerForRig(totalGPUsDevices map[string]uint64) GPU {
// total GPU characteristics
partialGPUsCharacteristics := make(map[string]GPU)
for k, _ := range totalGPUsDevices {
gpu := GPUs[k]
// Multiply each algorithm explicilty per the number of GPUs
// Another way of doing it is using reflection to iterate over the fields of the structure
r := reflect.ValueOf(&gpu)
e := r.Elem()
for i := 0; i < e.NumField(); i++ {
castedAlgo, ok := e.Field(i).Interface().(Algorithm)
checkFatalTypeAssertion(ok)
castedAlgo.HashRate *= float64(totalGPUsDevices[k])
castedAlgo.Power *= float64(totalGPUsDevices[k])
castedAlgoAsValue := reflect.ValueOf(castedAlgo)
e.Field(i).Set(castedAlgoAsValue)
}
// store back the total GPU Characteristics
partialGPUsCharacteristics[k] = gpu
}
// instance GPU that contains the total hashing rate and power for all the GPUS listed in conf.json
var totalGPUsCharacteristics GPU
totalReflect := reflect.ValueOf(&totalGPUsCharacteristics)
totalReflectElem := totalReflect.Elem()
for _, v := range partialGPUsCharacteristics {
partialReflect := reflect.ValueOf(v)
for i := 0; i < totalReflectElem.NumField(); i++ {
castedPartialAlgo, ok := partialReflect.Field(i).Interface().(Algorithm)
checkFatalTypeAssertion(ok)
castedTotalAlgo, ok := totalReflectElem.Field(i).Interface().(Algorithm)
checkFatalTypeAssertion(ok)
castedTotalAlgo.HashRate += castedPartialAlgo.HashRate
castedTotalAlgo.Power += castedPartialAlgo.Power
castedTotalAlgoAsValue := reflect.ValueOf(castedTotalAlgo)
totalReflectElem.Field(i).Set(castedTotalAlgoAsValue)
}
}
return totalGPUsCharacteristics
}
func getNumberOfGPUs(config ConfigFileJson) map[string]uint64 {
// store the gpus and quantities used (taken from conf.json)
r := reflect.ValueOf(config.GPU)
// map of type [ GPU_Name ] -> Number of gpus
totalGPUsDevices := make(map[string]uint64)
for k, _ := range GPUs {
numOfGPUs := r.FieldByName(k).Uint()
if numOfGPUs != 0 {
totalGPUsDevices[k] = numOfGPUs
}
}
return totalGPUsDevices
}
func main() {
// Parse command
config := parseConfig()
// get number of GPUs
numOfGPUs := getNumberOfGPUs(config)
// Calculate final hashrate and power
totalGPUsCharacteristics := calculateHashRateAndPowerForRig(numOfGPUs)
// read current values from www.whattomine.com
url := constructUrlQuery(config, totalGPUsCharacteristics)
regexp := compileRegex()
log.Println("Checking for new best coin")
var bestCoin string
bestCoin = getMostProfitableCoin(url, regexp, config)
log.Println("Starting to mine: " + bestCoin)
cmd := exec.Command(bestCoin)
checkFatalError(cmd.Start())
// now we need to start that script if it is not started
// and loop forever
ticker := time.NewTicker(time.Minute * 5)
// go checkAndRun(ticker, url, bestCoin)
for _ = range ticker.C {
log.Println("Checking for new best coin")
// checked new bestCoin
newBestCoin := getMostProfitableCoin(url, regexp, config)
if bestCoin != newBestCoin {
// start new bestCoin
log.Println("Starting to mine: " + newBestCoin)
cmd := exec.Command(newBestCoin)
checkFatalError(cmd.Start())
bestCoin = newBestCoin
} else {
log.Println("Still mining " + bestCoin)
}
}
}
// Returns the compiled regexp
func compileRegex() *regexp.Regexp {
re, err := regexp.Compile(coinsRegexp)
checkFatalError(err)
return re
}
// checks for err and return log fatal if any error
func checkFatalError(err error) {
if err != nil {
log.Fatal(err)
}
}
// checks for err and return log fatal if any error
func checkFatalTypeAssertion(ok bool) {
if !ok {
log.Fatal("Type assertion failed")
}
}
// Returns the most profitable script filename
func getMostProfitableCoin(url string, regexp *regexp.Regexp, config ConfigFileJson) string {
// read json from url
var coins Coins
readJsonFromUrl(url, &coins)
// read current value of bitcoin
bitcoin := make([]CoinMarketCapCoin, 0)
readJsonFromUrl(bitcoinUrl, &bitcoin)
// Create map 'coinName' -> USD revenue 24 hr
dailyDollarRevenue := make(map[string]float64)
// Convert bitcoin price to float64
bitcoinPrice := convertToFloat64(bitcoin[0].Price_USD)
for coinName, coinContent := range coins.Coins {
dailyDollarRevenue[coinName] = convertToFloat64(coinContent.Btc_revenue24) * bitcoinPrice
}
// sort the map into a sorted pairlist
sortedDailyDollarRevenue := SortMapByValue(dailyDollarRevenue)
// Print the coins and their revenue
log.Println("Daily $ revenue (BTC price: " + bitcoin[0].Price_USD + ")")
for i := 0; i < len(sortedDailyDollarRevenue); i++ {
log.Println(sortedDailyDollarRevenue[i].key + " = " + strconv.FormatFloat(sortedDailyDollarRevenue[i].value, 'f', -1, 64))
}
// Get the miners scripts in the minerDirectory
minerDirectory := filepath.Clean(config.MinerDirectory)
files, err := ioutil.ReadDir(minerDirectory)
checkFatalError(err)
// Create a map of type: map[coin name] = script name
minersScripts := make(map[string]string, len(files))
for _, file := range files {
if result := regexp.FindString(file.Name()); result != "" {
minersScripts[strings.ToLower(result)] = file.Name()
}
}
// Select the most profitable coin from the corresponding mining scripts available
var bestCoin string
for i := 0; i < len(sortedDailyDollarRevenue); i++ {
bestCoin = minersScripts[strings.ToLower(sortedDailyDollarRevenue[i].key)]
if bestCoin != "" {
log.Println("Most profitable is: " + bestCoin)
break
}
}
return minerDirectory + string(filepath.Separator) + bestCoin
}