forked from arionum/pool
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.php
202 lines (150 loc) · 6.38 KB
/
api.php
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
<?php
/*
Usage:
/api?q=poolstatus returns current status of the pool
/api?q=minerstatus&id=<wallet> returns total hashrate and shares of miner
/api?q=payments&id=<wallet> returns payments of miner
/api.php without flags also returns pool status at the moment for compatibility reasons
*/
header('Content-Type: application/json');
require_once __DIR__.'/db.php';
$q = san($_GET['q']);
if ($q == "minerstatus") {
$id = san($_GET['id']);
if ($id == null) {
api_err("Invalid request");
} else {
$cpu_hashrate = $db->single("SELECT hashrate FROM miners WHERE id=:id",[":id"=>$id]);
if ($cpu_hashrate != 0) { // If there is result, we can load rest of values
$gpu_hashrate = $db->single("SELECT gpuhr FROM miners WHERE id=:id",[":id"=>$id]);
$historic = $db->single("SELECT historic FROM miners WHERE id=:id",[":id"=>$id]);
$shares = $db->single("SELECT shares FROM miners WHERE id=:id",[":id"=>$id]);
$update = $db->single("SELECT updated FROM miners WHERE id=:id",[":id"=>$id]);
$yesterday = time()-86400;
$yesterday_block = $aro->single("SELECT height+1 FROM blocks WHERE date<=$yesterday ORDER by height DESC LIMIT 1");
$last_payment_txn = $db->single("SELECT txn FROM payments WHERE address=:id AND done=1 ORDER by height DESC LIMIT 1",[":id"=>$id]);
$last_payment_time = $aro->single("SELECT date FROM transactions WHERE id=$last_payment_txn");
$total_paid = (int)$db->single("SELECT total_paid FROM miners WHERE id=:id",[":id"=>$id]);
$pending = $db->single("SELECT pending FROM miners WHERE id=:id",[":id"=>$id]);
$last_payment = $db->single("SELECT SUM(val) FROM payments WHERE txn=:lasttxn AND done=1",[":lasttxn"=>$last_payment_txn]);
$past_24h = $db->single("SELECT SUM(val) FROM payments WHERE address=:id AND height>=$yesterday_block AND done=1",[":id"=>$id]);
$workers = $db->single("SELECT COUNT(1) FROM workers WHERE miner=:id",[":id"=>$id]);
if ($update == null) {
$update = "No nonce submitted";
}
if ($shares == null) {
$shares = 0;
}
if ($historic == null) {
$historic = 0;
}
if ($cpu_hashrate == null) {
$cpu_hashrate = 0;
}
if ($gpu_hashrate == null) {
$gpu_hashrate = 0;
}
if ($last_payment_time == null) {
$last_payment_time = "Payment in process";
}
if ($last_payment == 0 ) {
$last_payment_time = "No payment yet";
}
if ($pending == null) {
$pending = 0;
}
if ($last_payment == null) {
$last_payment = 0;
}
if ($past_24h == null) {
$past_24h = 0;
}
if ($workers == null) {
$workers = 0;
}
echo json_encode(array(
"miner" => $id,
"cpu_hr" => $cpu_hashrate,
"gpu_hr" => $gpu_hashrate,
"workers" => $workers,
"historic_shares" => $historic,
"current_shares" => $shares,
"last_nonce_submited" => $update,
"total_paid" => $total_paid,
"pending" => $pending,
"past_24h" => $past_24h,
"last_payment" => $last_payment,
"last_payment_date" => $last_payment_time
));
} else { // No result - we don't have record for this id in our miners table
api_err("Account not found");
}
}
} elseif ($q == "poolstatus") {
$total_hr = (int)$db->single("SELECT val FROM info WHERE id='total_hash_rate'");
$total_gpu = (int)$db->single("SELECT val FROM info WHERE id='total_gpu_hr'");
$current = $aro->single("SELECT height FROM blocks ORDER by height DESC LIMIT 1");
$miners = $db->single("SELECT COUNT(1) FROM miners WHERE hashrate>0 OR gpuhr>0");
$last_won = (int)$db->single("SELECT height FROM blocks ORDER by height DESC LIMIT 1");
$last_won_time = $aro->single("SELECT date FROM blocks WHERE height=:h",[":h"=>$last_won]);
if ($last_won_time == null) {
$last_won_time = "Never";
}
if ($miners == 0) {
$avg_gpuhr = 0;
$avg_hr = 0;
}
echo json_encode(
array(
"cpu_hr" => $total_hr,
"gpu_hr" => $total_gpu,
"current_block_height" => $current,
"last_won_block" => $last_won,
"last_won_block_time" => $last_won_time,
"active miners" => $miners,
"fee" => $pool_config['fee'],
"historic_reward" => $pool_config['historic_reward'],
"current_reward" => $pool_config['current_reward'],
"miner_reward" => $pool_config['miner_reward'],
"min_payout" => $pool_config['min_payout']
));
} elseif ($q == "payments") {
$id = san($_GET['id']);
if ($id == null) {
api_err("Invalid request");
} else {
$yesterday = time()-86400;
$yesterday_block = $aro->single("SELECT height+1 FROM blocks WHERE date<=$yesterday ORDER by height DESC LIMIT 1");
$last_payment_txn = $db->single("SELECT txn FROM payments WHERE address=:id AND done=1 ORDER by height DESC LIMIT 1",[":id"=>$id]);
$last_payment_time = $aro->single("SELECT date FROM transactions WHERE id=$last_payment_txn");
$total_paid = (int)$db->single("SELECT total_paid FROM miners WHERE id=:id",[":id"=>$id]);
$pending = $db->single("SELECT pending FROM miners WHERE id=:id",[":id"=>$id]);
$last_payment = $db->single("SELECT SUM(val) FROM payments WHERE txn=:lasttxn AND done=1",[":lasttxn"=>$last_payment_txn]);
$past_24h = $db->single("SELECT SUM(val) FROM payments WHERE address=:id AND height>=$yesterday_block AND done=1",[":id"=>$id]);
if ($pending == null) {
$pending = 0;
}
if ($last_payment == null) {
$last_payment = 0;
}
if ($past_24h == null) {
$past_24h = 0;
}
if ($last_payment_time == null) {
$last_payment_time = "Payment in process";
}
if ($last_payment == 0 ) {
$last_payment_time = "No payment yet";
}
echo json_encode(array("miner"=>$id, "total paid"=>$total_paid, "pending"=>$pending, "past_24h"=>$past_24h, "last_payment"=>$last_payment, "last_payment_date"=>$last_payment_time));
}
} else {
// we keep this here as this is the old aropool.com api, for compatibility
$total_hr = $db->single("SELECT val FROM info WHERE id='total_hash_rate'");
$total_gpu = $db->single("SELECT val FROM info WHERE id='total_gpu_hr'");
$current = $aro->single("SELECT height FROM blocks ORDER by height DESC LIMIT 1");
$miners = $db->single("SELECT COUNT(1) FROM miners");
$last_won = $db->single("SELECT height FROM blocks ORDER by height DESC LIMIT 1");
$last_won_time = $aro->single("SELECT date FROM blocks WHERE height=:h",[":h"=>$last_won]);
echo json_encode(array("cpu_hr"=>$total_hr, "gpu_hr"=>$total_gpu, "current_block_height"=>$current, "last_won_block"=>$last_won, "last_won_block_time"=>$last_won_time, "miners"=>$miners, "fee"=>$pool_config['fee']));
}