This repository was archived by the owner on Jan 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathnh-simple-ma.h
More file actions
73 lines (66 loc) · 2.46 KB
/
nh-simple-ma.h
File metadata and controls
73 lines (66 loc) · 2.46 KB
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
/// Query profitability of all algorithms on NiceHash and return index of most profitable algorithm.
/// Parameters:
/// - algorithm_names: Array of null terminated strings (should be lower case).
/// - algorithm_factors: Array of factors - speeds.
/// - algorithm_count: Number of algorithms provided in previous two arrays.
/// - opt_port: Optional out parameter that tells port number for most profitable algorithm. Set to NULL if not needed.
/// Return value:
/// Index of most profitable algorithm provided in algorithm_names. It returns ~0 if error occured.
size_t nicehash_simplemultialgo_get(const char** algorithm_names, const double* algorithm_factors, const size_t algorithm_count, int* opt_port)
{
CURL *curl;
json_t* val, *data;
size_t k, i, total;
double best_prof = 0;
size_t best_index = ~0;
int best_port = 0;
curl = curl_easy_init();
val = json_rpc_call(curl, "https://www.nicehash.com/api?method=simplemultialgo.info", NULL, "", NULL, 0);
// note: if this method fails with NULL being returned, then adding following line of code to your
// json_rpc_call method will most likely fix it:
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
// this call may fail sometimes - when NiceHash service is in maintenance mode!
if (!val) goto end;
data = json_object_get(val, "result");
if (!val) goto end;
data = json_object_get(data, "simplemultialgo");
if (!val) goto end;
total = json_array_size(data);
for (i = 0; i < total; i++)
{
const char* alg_name_s;
json_t* alg_name;
json_t* alg = json_array_get(data, i);
if (!alg) goto end;
alg_name = json_object_get(alg, "name");
if (!alg_name || !json_is_string(alg_name)) goto end;
alg_name_s = json_string_value(alg_name);
for (k = 0; k < algorithm_count; k++)
{
if (!strcmp(alg_name_s, algorithm_names[k]))
{
const char* alg_prof_s;
double alg_prof_d;
json_t* alg_prof = json_object_get(alg, "paying");
if (!alg_prof || !json_is_string(alg_prof)) goto end;
alg_prof_s = json_string_value(alg_prof);
alg_prof_d = atof(alg_prof_s);
if (algorithm_factors[k] * alg_prof_d > best_prof)
{
json_t* alg_port = json_object_get(alg, "port");
if (!alg_port || !json_is_integer(alg_port)) goto end;
best_port = (int)json_integer_value(alg_port);
best_prof = algorithm_factors[k] * alg_prof_d;
best_index = k;
}
break;
}
}
}
end:
if (opt_port)
*opt_port = best_port;
json_decref(val);
curl_easy_cleanup(curl);
return best_index;
}