-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathProgram.cs
1046 lines (972 loc) · 45.7 KB
/
Program.cs
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Alba.CsConsoleFormat;
using CryptoProfitSwitcher.Enums;
using CryptoProfitSwitcher.Factories;
using CryptoProfitSwitcher.Miners;
using CryptoProfitSwitcher.Models;
using CryptoProfitSwitcher.ProfitSwitchingStrategies;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using Serilog.Events;
namespace CryptoProfitSwitcher
{
internal static class Program
{
private const int Version = 14;
private static Config Config { get; set; }
private static string AppFolderPath { get; set; }
private static DirectoryInfo AppFolder { get; set; }
private static bool _newVersionAvailable;
private static bool _requestQuit;
private static bool _manualMode;
private static ProfitSwitchingStrategy? _profitSwitchingStrategy;
private static CancellationTokenSource _displayUpdaterCts;
private static CancellationTokenSource _profitFetcherTaskCts;
private static CancellationTokenSource _miningSwitcherTaskCts;
private static CancellationTokenSource _keyPressesTaskCts;
private static Dictionary<Pool, Profit> _poolProfitData;
private static Dictionary<string, MiningConfig> _currentMininigConfigs = new Dictionary<string, MiningConfig>();
private static Dictionary<string, MiningConfig> _manualMininigConfigs = null;
private static void Main(string[] args)
{
ResetConsole();
InitAppFolder();
InitConfig();
SetProcessPriority();
ResetConsole();
InitLogging();
CheckForUpdates();
DownloadMiners();
BenchmarkIfNeeded();
FetchPoolProfit(CancellationToken.None);
_manualMode = Config.EnableManualModeByDefault;
_profitSwitchingStrategy = Config.ProfitSwitchStrategy;
_keyPressesTaskCts = new CancellationTokenSource();
Task.WhenAll(
PoolProfitFetcherTaskAsync(),
MiningSwitcherTaskAsync(),
DisplayUpdaterTaskAsync(),
KeyPressesTaskAsync(_keyPressesTaskCts.Token)
).Wait();
}
private static void InitAppFolder()
{
AppFolderPath = Helpers.GetApplicationRoot();
AppFolder = new DirectoryInfo(AppFolderPath);
}
private static void InitLogging()
{
WriteInfo(" Initialize logging..");
string logPath = Path.Combine(AppFolderPath, "current_log.txt");
try
{
var logFile = new FileInfo(logPath);
if (logFile.Exists)
{
string oldlogPath = Path.Combine(AppFolderPath, "previous_log.txt");
if (File.Exists(oldlogPath))
{
File.Delete(oldlogPath);
}
logFile.MoveTo(oldlogPath);
}
}
catch (Exception e)
{
Console.WriteLine(" Couldn't initalize logging " + e.Message);
}
if (Config.EnableLogging)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console(LogEventLevel.Information)
.WriteTo.File(logPath)
.CreateLogger();
}
else
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(LogEventLevel.Information)
.CreateLogger();
}
}
private static void InitConfig()
{
WriteInfo(" Initialize config..");
var configFile = AppFolder.GetFiles("config.json").First();
var configJson = File.ReadAllText(configFile.FullName);
Config = JsonConvert.DeserializeObject<Config>(configJson);
}
private static void SaveConfig()
{
WriteInfo(" Saving new config..");
var configFile = AppFolder.GetFiles("config.json").First();
var configJson = JsonConvert.SerializeObject(Config, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter());
File.WriteAllText(configFile.FullName, configJson);
}
private static void SetProcessPriority()
{
WriteInfo(" Setting process priority to " + Config.ProcessPriority);
using Process p = Process.GetCurrentProcess();
p.PriorityClass = Config.ProcessPriority;
}
private static void CheckForUpdates()
{
try
{
WriteInfo(" Check for updates..");
var versionText = Helpers.GetJsonFromUrl("https://raw.githubusercontent.com/cryptoprofitswitcher/CryptonightProfitSwitcher/master/version.txt", false, AppFolder, CancellationToken.None);
int remoteVersion = Int32.Parse(versionText, CultureInfo.InvariantCulture);
if (remoteVersion > Version)
{
_newVersionAvailable = true;
WriteInfo(" New update available!");
}
else
{
WriteInfo(" Your version is up to date.");
}
}
catch (Exception ex)
{
Log.Warning("Couldn't check for updates: " + ex.Message);
}
}
private static void DownloadMiners()
{
try
{
if (!Config.DisableDownloadMiners)
{
var minersFolder = AppFolder.CreateSubdirectory("Miners");
if (!minersFolder.EnumerateFiles().Any())
{
WriteInfo(" Downloading miners..");
if (Helpers.IsWindows())
{
DownloadAndExtractFromGithub("https://api.github.com/repos/todxx/teamredminer/releases/latest", "win", "teamredminer", minersFolder);
DownloadAndExtractFromGithub("https://api.github.com/repos/xmrig/xmrig/releases/latest", "gcc-win64", "xmrig", minersFolder);
}
if (Helpers.IsLinux())
{
DownloadAndExtractFromGithub("https://api.github.com/repos/todxx/teamredminer/releases/latest", "linux", "teamredminer", minersFolder);
DownloadAndExtractFromGithub("https://api.github.com/repos/xmrig/xmrig/releases/latest", "xenial-x64", "xmrig", minersFolder);
}
}
}
}
catch (Exception e)
{
Log.Warning(e, "Downloading miners failed.");
}
}
private static void DownloadAndExtractFromGithub(string apiUrl, string identifier, string folderName, DirectoryInfo minersFolder)
{
string lastReleaseJson = Helpers.GetJsonFromUrl(apiUrl, false, AppFolder, CancellationToken.None);
JToken jLastRelease = JToken.Parse(lastReleaseJson);
JToken jwinAsset = jLastRelease["assets"].Children<JToken>().First(asset => asset.Value<string>("name").Contains(identifier));
string winDownloadUrl = jwinAsset.Value<string>("browser_download_url");
using var wc = new System.Net.WebClient();
string teamRedDownloadPath = Path.Combine(minersFolder.FullName, jwinAsset.Value<string>("name"));
wc.DownloadFile(winDownloadUrl, teamRedDownloadPath);
ZipFile.ExtractToDirectory(teamRedDownloadPath, minersFolder.FullName);
DirectoryInfo newFolder = minersFolder.EnumerateDirectories().OrderByDescending(d => d.CreationTimeUtc).First();
newFolder.MoveTo(Path.Combine(minersFolder.FullName, folderName));
}
private static Task PoolProfitFetcherTaskAsync()
{
return Task.Factory.StartNew(() =>
{
while (!_requestQuit)
{
try
{
_profitFetcherTaskCts = new CancellationTokenSource();
CancellationToken ct = _profitFetcherTaskCts.Token;
FetchPoolProfit(ct);
Task.Delay(TimeSpan.FromSeconds(Config.ProfitCheckInterval), ct).Wait(ct);
}
catch (Exception e)
{
Log.Debug("Profit fetching cancelled:" + e);
}
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
private static void FetchPoolProfit(CancellationToken ct)
{
try
{
WriteInfo(" Fetching profit data..");
Dictionary<Pool, Profit> poolProfitData = new Dictionary<Pool, Profit>();
Dictionary<ProfitProvider, List<Pool>> profitProviderFetches = new Dictionary<ProfitProvider, List<Pool>>();
foreach (Algorithm algorithm in Config.Algorithms)
{
if (algorithm.Enabled)
{
foreach (Pool algorithmPool in algorithm.Pools.Where(p => p.Enabled))
{
if (!profitProviderFetches.ContainsKey(algorithmPool.ProfitProvider))
{
profitProviderFetches[algorithmPool.ProfitProvider] = new List<Pool>() { algorithmPool };
}
else
{
profitProviderFetches[algorithmPool.ProfitProvider].Add(algorithmPool);
}
}
}
}
foreach (var profitProviderFetch in profitProviderFetches)
{
var profitProvider = PoolProfitProviderFactory.GetPoolProfitProvider(profitProviderFetch.Key);
var profits = profitProvider.GetProfits(profitProviderFetch.Value, Config.EnableCaching, AppFolder, ct);
foreach (var profit in profits)
{
poolProfitData[profit.Key] = profit.Value;
}
}
_poolProfitData = poolProfitData;
}
catch (Exception e)
{
Log.Debug("Fetching profit data failed: " + e);
}
}
private static Task MiningSwitcherTaskAsync()
{
return Task.Factory.StartNew(() =>
{
while (!_requestQuit)
{
try
{
_miningSwitcherTaskCts = new CancellationTokenSource();
CancellationToken ct = _miningSwitcherTaskCts.Token;
CheckSwitching();
Task.Delay(TimeSpan.FromSeconds(Config.ProfitSwitchInterval), ct).Wait(ct);
}
catch (Exception e)
{
Log.Debug("Check switching cancelled:" + e);
}
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
private static void CheckSwitching()
{
try
{
WriteInfo(" Check switching..");
IProfitSwitchingStrategy profitSwitchingStrategy = ProfitSwitchingStrategyFactory.GetProfitSwitchingStrategy(_profitSwitchingStrategy ?? ProfitSwitchingStrategy.MaximizeFiat);
// Find optimal configs
Dictionary<string, MiningConfig> optimalMiningConfigs = new Dictionary<string, MiningConfig>();
if (_manualMode)
{
foreach (var manualMininigConfig in _manualMininigConfigs)
{
MiningConfig miningConfig = new MiningConfig(manualMininigConfig.Value.DeviceConfig, manualMininigConfig.Value.Pool);
optimalMiningConfigs.Add(manualMininigConfig.Key, miningConfig);
}
}
else
{
foreach (Algorithm algorithm in Config.Algorithms)
{
if (algorithm.Enabled)
{
foreach (DeviceConfig algorithmDeviceConfig in algorithm.DeviceConfigs)
{
if (algorithmDeviceConfig.Enabled)
{
foreach (Pool algorithmPool in algorithm.Pools.Where(p => p.Enabled))
{
Profit? profit = GetAdjustedProfit(algorithmPool, algorithmDeviceConfig.ExpectedHashrate, true);
if (profit.HasValue)
{
if (!optimalMiningConfigs.ContainsKey(algorithmDeviceConfig.FullDeviceId))
{
SetOptimalMiningConfigWithThreshold(profit.Value, profitSwitchingStrategy, algorithmDeviceConfig, algorithmPool, optimalMiningConfigs);
}
else
{
MiningConfig bestMiningConfig = optimalMiningConfigs[algorithmDeviceConfig.FullDeviceId];
Profit? bestProfit = GetAdjustedProfit(bestMiningConfig.Pool, bestMiningConfig.DeviceConfig.ExpectedHashrate, true);
if (!bestProfit.HasValue || profitSwitchingStrategy.IsProfitABetterThanB(profit.Value, algorithmPool.ProfitTimeframe, bestProfit.Value, bestMiningConfig.Pool.ProfitTimeframe, 0))
{
SetOptimalMiningConfigWithThreshold(profit.Value, profitSwitchingStrategy, algorithmDeviceConfig, algorithmPool, optimalMiningConfigs);
}
}
}
}
}
}
}
}
}
// Merge miners and get list of optimal miners
List<IMiner> optimalMiners = new List<IMiner>();
foreach (var optimalMiningConfigKeyValue in optimalMiningConfigs)
{
MiningConfig miningConfig = optimalMiningConfigKeyValue.Value;
IMiner miner = MinerFactory.GetMiner(new HashSet<DeviceConfig>() { miningConfig.DeviceConfig }, miningConfig.Pool);
IMiner existingMiner = optimalMiners.FirstOrDefault(m => m.Name == miner.Name && m.Pool.Equals(miner.Pool));
if (existingMiner != null)
{
existingMiner.DeviceConfigs.Add(miningConfig.DeviceConfig);
miningConfig.Miner = existingMiner;
}
else
{
miningConfig.Miner = miner;
optimalMiners.Add(miner);
}
}
bool displayNeedsUpdate = false;
// Get list of current miners
List<IMiner> currentMiners = GetCurrentMiners();
//Check if existing miner will be kept
foreach (IMiner currentMiner in currentMiners)
{
if (optimalMiners.Any(om => om.Pool.Equals(currentMiner.Pool) && om.DeviceConfigs.SetEquals(currentMiner.DeviceConfigs)))
{
foreach (DeviceConfig currentMinerDeviceConfig in currentMiner.DeviceConfigs)
{
optimalMiningConfigs[currentMinerDeviceConfig.FullDeviceId].Miner = currentMiner;
}
}
}
//Check if existing miner has to be closed
foreach (IMiner currentMiner in currentMiners.ToArray())
{
if (!optimalMiners.Any(om => om.Pool.Equals(currentMiner.Pool) && om.DeviceConfigs.SetEquals(currentMiner.DeviceConfigs)))
{
displayNeedsUpdate = true;
currentMiner.StopMiner();
currentMiners.Remove(currentMiner);
}
}
//Check if new miner has to start
foreach (IMiner optimalMiner in optimalMiners)
{
if (!currentMiners.Any(cm => cm.Pool.Equals(optimalMiner.Pool) && cm.DeviceConfigs.SetEquals(optimalMiner.DeviceConfigs)))
{
displayNeedsUpdate = true;
foreach (DeviceConfig deviceConfig in optimalMiner.DeviceConfigs)
{
if (!string.IsNullOrEmpty(deviceConfig.PrepareScript))
{
Helpers.ExecuteScript(deviceConfig.MinerPath, AppFolderPath);
}
}
Task.Delay(TimeSpan.FromSeconds(Config.MinerStartDelay)).Wait();
optimalMiner.StartMiner(Config.StartMinerMinimized);
}
}
_currentMininigConfigs = optimalMiningConfigs;
if (displayNeedsUpdate)
{
_displayUpdaterCts?.Cancel();
}
}
catch (Exception e)
{
Log.Debug("Check switching failed: " + e);
}
}
private static void SetOptimalMiningConfigWithThreshold(Profit profit, IProfitSwitchingStrategy profitSwitchingStrategy, DeviceConfig algorithmDeviceConfig, Pool algorithmPool, Dictionary<string, MiningConfig> optimalMiningConfigs)
{
MiningConfig newMiningConfig = new MiningConfig(algorithmDeviceConfig, algorithmPool);
if (_currentMininigConfigs.ContainsKey(algorithmDeviceConfig.FullDeviceId))
{
MiningConfig currentMiningConfig = _currentMininigConfigs[algorithmDeviceConfig.FullDeviceId];
Profit? currentProfit = GetAdjustedProfit(currentMiningConfig.Pool, currentMiningConfig.DeviceConfig.ExpectedHashrate, true);
if (!currentProfit.HasValue || profitSwitchingStrategy.IsProfitABetterThanB(profit, algorithmPool.ProfitTimeframe, currentProfit.Value, currentMiningConfig.Pool.ProfitTimeframe, Config.ProfitSwitchThreshold))
{
optimalMiningConfigs[newMiningConfig.DeviceConfig.FullDeviceId] = newMiningConfig;
}
else
{
optimalMiningConfigs[algorithmDeviceConfig.FullDeviceId] = new MiningConfig(currentMiningConfig.DeviceConfig, currentMiningConfig.Pool);
}
}
else
{
optimalMiningConfigs[newMiningConfig.DeviceConfig.FullDeviceId] = newMiningConfig;
}
}
private static Task KeyPressesTaskAsync(CancellationToken ct)
{
return Task.Factory.StartNew(() =>
{
try
{
while (!ct.IsCancellationRequested)
{
Task.Delay(3000, ct).Wait(ct);
while (Console.CursorVisible)
{
Task.Delay(1000, ct).Wait(ct);
}
ConsoleKeyInfo readKey = Console.ReadKey(true);
if (!Console.CursorVisible)
{
switch (readKey.Key)
{
case ConsoleKey.A:
if (_manualMode)
{
WriteInfo(" Disabling manual mode..");
_manualMode = false;
_manualMininigConfigs = null;
_miningSwitcherTaskCts?.Cancel();
}
else
{
WriteInfo(" Reconfigure profit switching strategy due to key press..");
_profitSwitchingStrategy = null;
_displayUpdaterCts?.Cancel();
}
break;
case ConsoleKey.U:
WriteInfo(" Updating display due to key press..");
_displayUpdaterCts?.Cancel();
break;
case ConsoleKey.F:
WriteInfo(" Fetching profits due to key press..");
_profitFetcherTaskCts?.Cancel();
break;
case ConsoleKey.C:
WriteInfo(" Check switching due to key press..");
_miningSwitcherTaskCts?.Cancel();
break;
case ConsoleKey.M:
if (_manualMode)
{
WriteInfo(" Reconfigure manual mode due to key press..");
_manualMininigConfigs = null;
_displayUpdaterCts?.Cancel();
}
else
{
WriteInfo(" Activating manual mode..");
_manualMininigConfigs = null;
_manualMode = true;
_displayUpdaterCts?.Cancel();
}
break;
case ConsoleKey.R:
WriteInfo(" Reloading config due to key press..");
InitConfig();
break;
case ConsoleKey.Q:
WriteInfo(" Quitting due to key press..");
Quit();
break;
}
}
}
}
catch (Exception e)
{
Log.Debug("Key presses task failed:" + e);
throw;
}
}, ct, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
private static Task DisplayUpdaterTaskAsync()
{
return Task.Factory.StartNew(() =>
{
while (!_requestQuit)
{
try
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
_displayUpdaterCts = new CancellationTokenSource();
CancellationToken ct = _displayUpdaterCts.Token;
UpdateDisplay();
Task.Delay(TimeSpan.FromSeconds(Config.DisplayUpdateInterval), ct).Wait(ct);
}
catch (Exception e)
{
Log.Debug("Update display cancelled:" + e);
}
finally
{
Thread.CurrentThread.Priority = ThreadPriority.Normal;
}
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}
private static void UpdateDisplay()
{
try
{
ResetConsole();
if (_poolProfitData != null)
{
List<Pool> printedPools = PrintProfits();
if (_manualMode && _manualMininigConfigs == null)
{
Console.WriteLine();
Console.WriteLine();
PrintManualModeConfig(printedPools);
}
}
if (!_manualMode && _profitSwitchingStrategy == null)
{
Console.WriteLine();
Console.WriteLine();
PrintAutoModeConfig();
}
Console.WriteLine();
Console.WriteLine();
PrintMiningStatus();
Console.WriteLine();
}
catch (Exception e)
{
Log.Debug("Update display failed: " + e);
}
}
private static List<Pool> PrintProfits()
{
var printedPools = new List<Pool>();
WriteLineColor(" EXPECTED PROFIT FOR POOLS: ", ConsoleColor.Cyan);
var headerThickness = new LineThickness(LineWidth.Double, LineWidth.Single);
var poolGrid = new Grid
{
Color = ConsoleColor.White,
Margin = new Thickness(1, 0, 0, 0),
Columns = { GridLength.Auto },
Children =
{
new Cell("Pool") { Stroke = headerThickness, Color = ConsoleColor.Magenta, Padding = new Thickness(2,0,10,0) },
},
};
HashSet<string> deviceIds = GetActiveDeviceIds();
foreach (var deviceId in deviceIds)
{
poolGrid.Columns.Add(GridLength.Auto);
poolGrid.Children.Add(new Cell(deviceId) { Stroke = headerThickness, Color = ConsoleColor.Magenta, Padding = new Thickness(2, 0, 2, 0) });
}
List<DevicePoolProfitData> devicePoolProfitDatas = new List<DevicePoolProfitData>();
foreach (var poolProfit in _poolProfitData)
{
DevicePoolProfitData devicePoolProfitData = new DevicePoolProfitData(poolProfit.Key);
foreach (string deviceId in deviceIds)
{
double expectedHashrate = 0;
DeviceConfig deviceConfig = GetDeviceConfigForPool(poolProfit.Key, deviceId);
if (deviceConfig != null)
{
expectedHashrate = deviceConfig.ExpectedHashrate;
}
Profit? profit = GetAdjustedProfit(poolProfit.Key, expectedHashrate, false);
devicePoolProfitData.DeviceProfits[deviceId] = profit;
}
devicePoolProfitDatas.Add(devicePoolProfitData);
}
Dictionary<string, double> maxProfits = new Dictionary<string, double>();
foreach (string deviceId in deviceIds)
{
maxProfits[deviceId] = devicePoolProfitDatas.Max(dppds => dppds.DeviceProfits[deviceId]?.GetMostCurrentUsdReward() ?? 0);
}
int poolIndex = 0;
foreach (DevicePoolProfitData devicePoolProfitData in devicePoolProfitDatas)
{
poolIndex++;
printedPools.Add(devicePoolProfitData.Pool);
string displayName = _manualMode && _manualMininigConfigs == null ? $"[{poolIndex}] " + devicePoolProfitData.Pool.UniqueName : devicePoolProfitData.Pool.UniqueName;
poolGrid.Children.Add(new Cell(displayName) { Color = ConsoleColor.Yellow, Padding = new Thickness(2, 0, 10, 0) });
foreach (string deviceId in deviceIds)
{
double maxProfit = maxProfits[deviceId];
Profit? profit = devicePoolProfitData.DeviceProfits[deviceId];
if (profit != null)
{
ConsoleColor color = profit.Value.GetMostCurrentUsdReward() < maxProfit ? ConsoleColor.Gray : ConsoleColor.Green;
poolGrid.Children.Add(!profit.Value.HasValues()
? new Cell("No data") { Color = ConsoleColor.DarkGray, Align = Align.Center, Padding = new Thickness(2, 0, 2, 0) }
: new Cell(profit) { Color = color, Align = Align.Center, Padding = new Thickness(2, 0, 2, 0) });
}
}
}
var poolDoc = new Document(poolGrid);
ConsoleRenderer.RenderDocument(poolDoc);
return printedPools;
}
private static void PrintManualModeConfig(List<Pool> printedPools)
{
var manualMininigConfigs = new Dictionary<string, MiningConfig>();
WriteLineColor(" MANUAL MODE CONFIG: ", ConsoleColor.Cyan);
WriteLineColor(" Use the numbers above to set a pool.", ConsoleColor.DarkGray);
WriteLineColor(" Or use \"0\" to disable the device.", ConsoleColor.DarkGray);
Console.WriteLine();
Console.CursorVisible = true;
foreach (string deviceId in GetActiveDeviceIds())
{
DeviceConfig deviceConfig = null;
Pool selectedPool = null;
bool noPool = false;
while ((selectedPool == null || deviceConfig == null) && !noPool)
{
Console.Write(" Set the pool for ");
WriteColor(deviceId, ConsoleColor.Yellow);
Console.Write(": ");
string userInput = Console.ReadLine();
if (int.TryParse(userInput, out int poolUserIndex))
{
if (poolUserIndex == 0)
{
noPool = true;
}
else if (poolUserIndex > 0 && poolUserIndex <= printedPools.Count)
{
selectedPool = printedPools[poolUserIndex - 1];
deviceConfig = GetDeviceConfigForPool(selectedPool, deviceId);
if (deviceConfig == null)
{
Console.WriteLine(" The selected pool is not valid for this device.");
}
}
}
}
if (!noPool)
{
manualMininigConfigs[deviceConfig.FullDeviceId] = new MiningConfig(deviceConfig, selectedPool);
}
}
Console.CursorVisible = false;
_manualMininigConfigs = manualMininigConfigs;
_miningSwitcherTaskCts?.Cancel();
Console.WriteLine();
}
private static void PrintAutoModeConfig()
{
Console.CursorVisible = true;
WriteLineColor(" AUTO MODE CONFIG: ", ConsoleColor.Cyan);
WriteLineColor(" Use the numbers below to set the profit switching strategy.", ConsoleColor.DarkGray);
Console.WriteLine();
var strategies = Enum.GetValues(typeof(ProfitSwitchingStrategy)).Cast<ProfitSwitchingStrategy>().ToList();
for (var index = 0; index < strategies.Count; index++)
{
ProfitSwitchingStrategy profitSwitchingStrategy = strategies[index];
Console.Write(" [");
WriteColor((index + 1).ToString(CultureInfo.InvariantCulture), ConsoleColor.Yellow);
Console.WriteLine($"] {profitSwitchingStrategy.ToString()}");
}
Console.WriteLine();
while (!_profitSwitchingStrategy.HasValue)
{
Console.Write(" Type the number: ");
string userInput = Console.ReadLine();
if (int.TryParse(userInput, out int userIndex))
{
if (userIndex > 0 && userIndex <= strategies.Count)
{
_profitSwitchingStrategy = strategies[userIndex - 1];
}
}
}
Console.CursorVisible = false;
Console.WriteLine();
_miningSwitcherTaskCts?.Cancel();
}
private static void PrintMiningStatus()
{
const int extraPadding = 3;
const string total = "Total";
WriteLineColor(" CURRENT STATUS: ", ConsoleColor.Cyan);
Console.WriteLine();
if (_currentMininigConfigs.Count > 0)
{
List<IMiner> currentMiners = GetCurrentMiners();
var deviceStatuses = new List<DeviceStatus>();
foreach (var currentMiner in currentMiners)
{
if (currentMiner.SupportsIndividualHashrate || currentMiner.DeviceConfigs.Count == 1)
{
foreach (DeviceConfig deviceConfig in currentMiner.DeviceConfigs)
{
double currentHashrate = currentMiner.GetCurrentHashrate(deviceConfig);
Profit? profit = GetAdjustedProfit(currentMiner.Pool, currentHashrate, false);
if (profit.HasValue)
{
deviceStatuses.Add(new DeviceStatus(deviceConfig.FullDeviceId, currentMiner.Pool, currentHashrate, profit.Value));
}
}
}
else
{
double currentHashrate = currentMiner.GetCurrentHashrate(null);
Profit? profit = GetAdjustedProfit(currentMiner.Pool, currentHashrate, false);
string combinedDeviceNames = string.Join(" | ", currentMiner.DeviceConfigs.Select(dc => dc.FullDeviceId));
if (profit.HasValue)
{
deviceStatuses.Add(new DeviceStatus(combinedDeviceNames, currentMiner.Pool, currentHashrate, profit.Value));
}
}
}
int maxDeviceNameLength = Math.Max(deviceStatuses.Max(ds => ds.Name.Length), total.Length);
PrintMode(maxDeviceNameLength + extraPadding);
foreach (DeviceStatus deviceStatus in deviceStatuses)
{
Console.WriteLine();
string leftText = $" {deviceStatus.Name}:".PadRight(maxDeviceNameLength + extraPadding);
WriteColor(leftText, ConsoleColor.Magenta);
Console.Write(" Mining on ");
WriteColor(deviceStatus.Pool.UniqueName, ConsoleColor.Yellow);
Console.Write(" at ");
WriteColor(deviceStatus.Hashrate.ToHashrate(), ConsoleColor.Yellow);
double usdProfit = deviceStatus.Profit.GetMostCurrentUsdReward();
if (usdProfit > 0)
{
Console.Write(" (");
WriteColor(usdProfit.ToCurrency("$"), ConsoleColor.Yellow);
Console.Write(" per day)");
}
}
Console.WriteLine();
Console.WriteLine();
WriteColor($" {total}:".PadRight(maxDeviceNameLength + extraPadding), ConsoleColor.Magenta);
Console.Write(" You are currently earning ");
double totalUsdProfit = deviceStatuses.Sum(ds => ds.Profit.GetMostCurrentUsdReward());
WriteColor(totalUsdProfit.ToCurrency("$"), ConsoleColor.Green);
Console.Write(" per day.");
Console.WriteLine();
}
else
{
PrintMode(4 + extraPadding);
Console.WriteLine();
WriteLineColor(" Not mining.", ConsoleColor.Yellow);
}
}
private static void PrintMode(int leftWidth)
{
Console.Write(" Mode:".PadRight(leftWidth));
if (_manualMode)
{
Console.Write(" Manual -> switch pools using the key '");
WriteColor("m", ConsoleColor.Yellow);
Console.WriteLine("'");
}
else
{
Console.Write(" Auto -> profit switching with ");
WriteColor(_profitSwitchingStrategy.Value.ToString(), ConsoleColor.Yellow);
Console.Write(" strategy (press '");
WriteColor("a", ConsoleColor.Yellow);
Console.WriteLine("' to change strategy)");
}
}
private static void ResetConsole()
{
Console.CursorVisible = false;
Console.Clear();
string updateText = _newVersionAvailable ? " (NEW UPDATE AVAILABLE!)" : String.Empty;
WriteLineColor($" CRYPTO PROFIT SWITCHER | VERSION: {Helpers.GetApplicationVersion()}{updateText}", ConsoleColor.Cyan);
WriteLineColor(" https://github.com/cryptoprofitswitcher/CryptoProfitSwitcher", ConsoleColor.DarkGray);
if (Config != null)
{
Console.Write(" Config: DisplayUpdateInterval ");
WriteColor(Config.DisplayUpdateInterval.ToString(CultureInfo.InvariantCulture), ConsoleColor.Yellow);
Console.Write("s | ProfitCheckInterval ");
WriteColor(Config.ProfitCheckInterval.ToString(CultureInfo.InvariantCulture), ConsoleColor.Yellow);
Console.Write("s | SwitchInterval ");
WriteColor(Config.ProfitSwitchInterval.ToString(CultureInfo.InvariantCulture), ConsoleColor.Yellow);
Console.Write("s | SwitchThreshold ");
WriteLineColor(Config.ProfitSwitchThreshold.ToString(CultureInfo.InvariantCulture), ConsoleColor.Yellow);
}
Console.Write(" Hotkeys: ");
WriteColor("u", ConsoleColor.Yellow);
Console.Write(" - update display, ");
WriteColor("f", ConsoleColor.Yellow);
Console.Write(" - fetch profits, ");
WriteColor("c", ConsoleColor.Yellow);
Console.Write(" - check switch, ");
if (_manualMode)
{
WriteColor("a", ConsoleColor.Yellow);
Console.Write(" - auto mode, ");
}
else
{
WriteColor("m", ConsoleColor.Yellow);
Console.Write(" - manual mode, ");
}
WriteColor("r", ConsoleColor.Yellow);
Console.Write(" - reload config, ");
WriteColor("q", ConsoleColor.Yellow);
Console.WriteLine(" - quit");
Console.WriteLine();
}
private static void BenchmarkIfNeeded()
{
if (!Config.DisableBenchmarking)
{
WriteInfo(" Checking if benchmarking is needed..");
List<MiningConfig> configsToTest = new List<MiningConfig>();
foreach (Algorithm algorithm in Config.Algorithms)
{
if (algorithm.Enabled)
{
foreach (DeviceConfig deviceConfig in algorithm.DeviceConfigs)
{
if (deviceConfig.Enabled && deviceConfig.ExpectedHashrate <= 0)
{
configsToTest.Add(new MiningConfig(deviceConfig, algorithm.Pools.First(p => p.Enabled)));
}
}
}
}
if (configsToTest.Count == 0)
{
WriteInfo(" Benchmarking not needed: All algorithm-device-combinations have a valid expected hashrate.");
}
else
{
WriteInfo(" Benchmarking needed: Not all algorithm-device-combinations have a valid expected hashrate.");
Console.WriteLine();
WriteLineColor(" BENCHMARKING:", ConsoleColor.Cyan);
foreach (MiningConfig miningConfig in configsToTest)
{
Console.WriteLine();
Console.Write(" Benchmarking ");
WriteColor(miningConfig.DeviceConfig.FullDeviceId, ConsoleColor.Yellow);
Console.Write(" on ");
WriteColor(miningConfig.Pool.UniqueName, ConsoleColor.Yellow);
Console.WriteLine("..");
IMiner miner = MinerFactory.GetMiner(new HashSet<DeviceConfig>() { miningConfig.DeviceConfig }, miningConfig.Pool);
miner.StartMiner(Config.StartMinerMinimized);
int userInputHashrate = 0;
while (userInputHashrate <= 0)
{
Console.Write(" Please enter the hashrate: ");
Console.CursorVisible = true;
Console.ForegroundColor = ConsoleColor.Yellow;
string input = Console.ReadLine();
Console.ResetColor();
int.TryParse(input, out userInputHashrate);
}
Console.CursorVisible = false;
miningConfig.DeviceConfig.ExpectedHashrate = userInputHashrate;
SaveConfig();
miner.StopMiner();
}
}
}
}
private static void Quit()
{
_requestQuit = true;
_profitFetcherTaskCts?.Cancel();
_miningSwitcherTaskCts?.Cancel();
_displayUpdaterCts?.Cancel();
_keyPressesTaskCts?.Cancel();
StopMiners();
}
private static void StopMiners()
{
if (_currentMininigConfigs != null)
{
foreach (var currentMininigConfig in _currentMininigConfigs)
{
currentMininigConfig.Value.Miner?.StopMiner();
}
}
_currentMininigConfigs = new Dictionary<string, MiningConfig>();
}
private static void WriteLineColor(string text, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
Console.ResetColor();
}
private static void WriteColor(string text, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.Write(text);
Console.ResetColor();
}
private static List<IMiner> GetCurrentMiners()
{
var currentMiners = new List<IMiner>();
foreach (var currentMininigConfigKeyValue in _currentMininigConfigs)
{
MiningConfig miningConfig = currentMininigConfigKeyValue.Value;
if (!currentMiners.Contains(miningConfig.Miner))
{
currentMiners.Add(miningConfig.Miner);
}
}
return currentMiners;
}
private static HashSet<string> GetActiveDeviceIds()
{
var deviceIds = new HashSet<string>();
foreach (Algorithm algorithm in Config.Algorithms)
{
if (algorithm.Enabled)
{
foreach (DeviceConfig deviceConfig in algorithm.DeviceConfigs)
{
if (!deviceIds.Contains(deviceConfig.FullDeviceId))
{
deviceIds.Add(deviceConfig.FullDeviceId);
}
}
}
}