This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathNiceHashStats.cs
218 lines (189 loc) · 6.48 KB
/
NiceHashStats.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using NiceHashMiner.Enums;
using NiceHashMiner.Miners;
using Newtonsoft.Json.Linq;
namespace NiceHashMiner
{
#pragma warning disable 649
public class NiceHashSMA
{
public int port;
public string name;
public int algo;
public double paying;
}
#pragma warning restore 649
class NiceHashStats
{
#pragma warning disable 649
//class nicehash_global_stats
//{
// public double profitability_above_ltc;
// public double price;
// public double profitability_ltc;
// public int algo;
// public double speed;
//}
public class nicehash_stats
{
public double balance;
public double balance_unexchanged;
public double balance_immature;
public double balance_confirmed;
public double accepted_speed;
public double rejected_speed;
public int algo;
}
public class nicehash_result_2
{
public NiceHashSMA[] simplemultialgo;
}
public class nicehash_json_2
{
public nicehash_result_2 result;
public string method;
}
class nicehash_result<T>
{
public T[] stats;
}
class nicehash_json<T>
{
public nicehash_result<T> result;
public string method;
}
class nicehash_json_T<T> {
public T result;
public string method;
}
class nicehash_error
{
public string error;
public string method;
}
class nicehashminer_version
{
public string version;
}
#pragma warning restore 649
public static Dictionary<AlgorithmType, NiceHashSMA> GetAlgorithmRates(string worker)
{
string r1 = GetNiceHashAPIData(Links.NHM_API_info, worker);
if (r1 == null) return null;
nicehash_json_2 nhjson_current;
try
{
nhjson_current = JsonConvert.DeserializeObject<nicehash_json_2>(r1, Globals.JsonSettings);
Dictionary<AlgorithmType, NiceHashSMA> ret = new Dictionary<AlgorithmType, NiceHashSMA>();
NiceHashSMA[] temp = nhjson_current.result.simplemultialgo;
if (temp != null) {
foreach (var sma in temp) {
ret.Add((AlgorithmType)sma.algo, sma);
}
return ret;
}
return null;
}
catch
{
return null;
}
}
public static nicehash_stats GetStats(string btc, int algo, string worker)
{
string r1 = GetNiceHashAPIData(Links.NHM_API_stats + btc, worker);
if (r1 == null) return null;
nicehash_json<nicehash_stats> nhjson_current;
try
{
nhjson_current = JsonConvert.DeserializeObject<nicehash_json<nicehash_stats>>(r1, Globals.JsonSettings);
for (int i = 0; i < nhjson_current.result.stats.Length; i++)
{
if (nhjson_current.result.stats[i].algo == algo)
return nhjson_current.result.stats[i];
}
return null;
}
catch
{
return null;
}
}
public static double GetBalance(string btc, string worker)
{
double balance = 0;
string r1 = GetNiceHashAPIData(Links.NHM_API_stats + btc, worker);
if (r1 != null)
{
nicehash_json<nicehash_stats> nhjson_current;
try
{
nhjson_current = JsonConvert.DeserializeObject<nicehash_json<nicehash_stats>>(r1, Globals.JsonSettings);
for (int i = 0; i < nhjson_current.result.stats.Length; i++)
{
if (nhjson_current.result.stats[i].algo != 999)
{
balance += nhjson_current.result.stats[i].balance;
}
else if (nhjson_current.result.stats[i].algo == 999)
{
balance += nhjson_current.result.stats[i].balance_confirmed;
}
}
}
catch { }
}
return balance;
}
public static string GetVersion(string worker)
{
string r1 = GetNiceHashAPIData(Links.NHM_API_version, worker);
if (r1 == null) return null;
nicehashminer_version nhjson;
try
{
nhjson = JsonConvert.DeserializeObject<nicehashminer_version>(r1, Globals.JsonSettings);
return nhjson.version;
}
catch
{
return null;
}
}
public static string GetNiceHashAPIData(string URL, string worker)
{
string ResponseFromServer;
try
{
string ActiveMinersGroup = MinersManager.GetActiveMinersGroup();
HttpWebRequest WR = (HttpWebRequest)WebRequest.Create(URL);
WR.UserAgent = "NiceHashMiner/" + Application.ProductVersion;
if (worker.Length > 64) worker = worker.Substring(0, 64);
WR.Headers.Add("NiceHash-Worker-ID", worker);
WR.Headers.Add("NHM-Active-Miners-Group", ActiveMinersGroup);
WR.Timeout = 30 * 1000;
WebResponse Response = WR.GetResponse();
Stream SS = Response.GetResponseStream();
SS.ReadTimeout = 20 * 1000;
StreamReader Reader = new StreamReader(SS);
ResponseFromServer = Reader.ReadToEnd();
if (ResponseFromServer.Length == 0 || ResponseFromServer[0] != '{')
throw new Exception("Not JSON!");
Reader.Close();
Response.Close();
}
catch (Exception ex)
{
Helpers.ConsolePrint("NICEHASH", ex.Message);
return null;
}
return ResponseFromServer;
}
}
}