-
Notifications
You must be signed in to change notification settings - Fork 1
/
nicehash.py
409 lines (337 loc) · 13.5 KB
/
nicehash.py
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
""" Gets current NiceHash mining statistics, writes to InfluxDB. Not yet mypy-compliant. """
import pprint
from influxdb import InfluxDBClient
from datetime import datetime
from time import mktime, sleep
import uuid
import hmac
import requests
import json
from hashlib import sha256
import os
client = InfluxDBClient(
os.getenv("INFLUX_HOSTNAME"),
8086,
os.getenv("INFLUX_USERNAME"),
os.getenv("INFLUX_PASSWORD"),
os.getenv("INFLUX_DATABASE"),
)
class public_api:
def __init__(self, host, verbose=False):
self.host = host
self.verbose = verbose
def request(self, method, path, query, body):
url = self.host + path
if query:
url += "?" + query
if self.verbose:
print(method, url)
s = requests.Session()
if body:
body_json = json.dumps(body)
response = s.request(method, url, data=body_json)
else:
response = s.request(method, url)
if response.status_code == 200:
return response.json()
elif response.content:
raise Exception(
str(response.status_code)
+ ": "
+ response.reason
+ ": "
+ str(response.content)
)
else:
raise Exception(str(response.status_code) + ": " + response.reason)
def get_current_global_stats(self):
return self.request(
"GET", "/main/api/v2/public/stats/global/current/", "", None
)
def get_global_stats_24(self):
return self.request("GET", "/main/api/v2/public/stats/global/24h/", "", None)
def get_active_orders(self):
return self.request("GET", "/main/api/v2/public/orders/active/", "", None)
def get_active_orders2(self):
return self.request("GET", "/main/api/v2/public/orders/active2/", "", None)
def buy_info(self):
return self.request("GET", "/main/api/v2/public/buy/info/", "", None)
def get_algorithms(self):
return self.request("GET", "/main/api/v2/mining/algorithms/", "", None)
def get_markets(self):
return self.request("GET", "/main/api/v2/mining/markets/", "", None)
def get_currencies(self):
return self.request("GET", "/main/api/v2/public/currencies/", "", None)
def get_multialgo_info(self):
return self.request(
"GET", "/main/api/v2/public/simplemultialgo/info/", "", None
)
def get_exchange_markets_info(self):
return self.request("GET", "/exchange/api/v2/info/status", "", None)
def get_exchange_trades(self, market):
return self.request("GET", "/exchange/api/v2/trades", "market=" + market, None)
def get_candlesticks(self, market, from_s, to_s, resolution):
return self.request(
"GET",
"/exchange/api/v2/candlesticks",
"market={}&from={}&to={}&resolution={}".format(
market, from_s, to_s, resolution
),
None,
)
def get_exchange_orderbook(self, market, limit):
return self.request(
"GET",
"/exchange/api/v2/orderbook",
"market={}&limit={}".format(market, limit),
None,
)
class private_api:
def __init__(self, host, organisation_id, key, secret, verbose=False):
self.key = key
self.secret = secret
self.organisation_id = organisation_id
self.host = host
self.verbose = verbose
def request(self, method, path, query, body):
xtime = self.get_epoch_ms_from_now()
xnonce = str(uuid.uuid4())
message = bytearray(self.key, "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(str(xtime), "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(xnonce, "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(self.organisation_id, "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(method, "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(path, "utf-8")
message += bytearray("\x00", "utf-8")
message += bytearray(query, "utf-8")
if body:
body_json = json.dumps(body)
message += bytearray("\x00", "utf-8")
message += bytearray(body_json, "utf-8")
digest = hmac.new(bytearray(self.secret, "utf-8"), message, sha256).hexdigest()
xauth = self.key + ":" + digest
headers = {
"X-Time": str(xtime),
"X-Nonce": xnonce,
"X-Auth": xauth,
"Content-Type": "application/json",
"X-Organization-Id": self.organisation_id,
"X-Request-Id": str(uuid.uuid4()),
}
s = requests.Session()
s.headers = headers
url = self.host + path
if query:
url += "?" + query
if self.verbose:
print(method, url)
if body:
response = s.request(method, url, data=body_json)
else:
response = s.request(method, url)
if response.status_code == 200:
return response.json()
elif response.content:
raise Exception(
str(response.status_code)
+ ": "
+ response.reason
+ ": "
+ str(response.content)
)
else:
raise Exception(str(response.status_code) + ": " + response.reason)
def get_epoch_ms_from_now(self):
now = datetime.now()
now_ec_since_epoch = mktime(now.timetuple()) + now.microsecond / 1000000.0
return int(now_ec_since_epoch * 1000)
def algo_settings_from_response(self, algorithm, algo_response):
algo_setting = None
for item in algo_response["miningAlgorithms"]:
if item["algorithm"] == algorithm:
algo_setting = item
if algo_setting is None:
raise Exception(
"Settings for algorithm not found in algo_response parameter"
)
return algo_setting
def get_accounts(self):
return self.request("GET", "/main/api/v2/accounting/accounts2/", "", None)
def get_accounts_for_currency(self, currency):
return self.request(
"GET", "/main/api/v2/accounting/account2/" + currency, "", None
)
def get_withdrawal_addresses(self, currency, size, page):
params = "currency={}&size={}&page={}".format(currency, size, page)
return self.request(
"GET", "/main/api/v2/accounting/withdrawalAddresses/", params, None
)
def get_withdrawal_types(self):
return self.request(
"GET", "/main/api/v2/accounting/withdrawalAddresses/types/", "", None
)
def withdraw_request(self, address_id, amount, currency):
withdraw_data = {
"withdrawalAddressId": address_id,
"amount": amount,
"currency": currency,
}
return self.request(
"POST", "/main/api/v2/accounting/withdrawal/", "", withdraw_data
)
def get_my_active_orders(self, algorithm, market, limit):
ts = self.get_epoch_ms_from_now()
params = "algorithm={}&market={}&ts={}&limit={}&op=LT".format(
algorithm, market, ts, limit
)
return self.request("GET", "/main/api/v2/hashpower/myOrders", params, None)
def create_pool(self, name, algorithm, pool_host, pool_port, username, password):
pool_data = {
"name": name,
"algorithm": algorithm,
"stratumHostname": pool_host,
"stratumPort": pool_port,
"username": username,
"password": password,
}
return self.request("POST", "/main/api/v2/pool/", "", pool_data)
def delete_pool(self, pool_id):
return self.request("DELETE", "/main/api/v2/pool/" + pool_id, "", None)
def get_my_pools(self, page, size):
return self.request("GET", "/main/api/v2/pools/", "", None)
def get_hashpower_orderbook(self, algorithm):
return self.request(
"GET", "/main/api/v2/hashpower/orderBook/", "algorithm=" + algorithm, None
)
def create_hashpower_order(
self, market, type, algorithm, price, limit, amount, pool_id, algo_response
):
algo_setting = self.algo_settings_from_response(algorithm, algo_response)
order_data = {
"market": market,
"algorithm": algorithm,
"amount": amount,
"price": price,
"limit": limit,
"poolId": pool_id,
"type": type,
"marketFactor": algo_setting["marketFactor"],
"displayMarketFactor": algo_setting["displayMarketFactor"],
}
return self.request("POST", "/main/api/v2/hashpower/order/", "", order_data)
def cancel_hashpower_order(self, order_id):
return self.request(
"DELETE", "/main/api/v2/hashpower/order/" + order_id, "", None
)
def refill_hashpower_order(self, order_id, amount):
refill_data = {"amount": amount}
return self.request(
"POST",
"/main/api/v2/hashpower/order/" + order_id + "/refill/",
"",
refill_data,
)
def set_price_hashpower_order(self, order_id, price, algorithm, algo_response):
algo_setting = self.algo_settings_from_response(algorithm, algo_response)
price_data = {
"price": price,
"marketFactor": algo_setting["marketFactor"],
"displayMarketFactor": algo_setting["displayMarketFactor"],
}
return self.request(
"POST",
"/main/api/v2/hashpower/order/" + order_id + "/updatePriceAndLimit/",
"",
price_data,
)
def set_limit_hashpower_order(self, order_id, limit, algorithm, algo_response):
algo_setting = self.algo_settings_from_response(algorithm, algo_response)
limit_data = {
"limit": limit,
"marketFactor": algo_setting["marketFactor"],
"displayMarketFactor": algo_setting["displayMarketFactor"],
}
return self.request(
"POST",
"/main/api/v2/hashpower/order/" + order_id + "/updatePriceAndLimit/",
"",
limit_data,
)
def set_price_and_limit_hashpower_order(
self, order_id, price, limit, algorithm, algo_response
):
algo_setting = self.algo_settings_from_response(algorithm, algo_response)
price_data = {
"price": price,
"limit": limit,
"marketFactor": algo_setting["marketFactor"],
"displayMarketFactor": algo_setting["displayMarketFactor"],
}
return self.request(
"POST",
"/main/api/v2/hashpower/order/" + order_id + "/updatePriceAndLimit/",
"",
price_data,
)
def get_my_exchange_orders(self, market):
return self.request(
"GET", "/exchange/api/v2/myOrders", "market=" + market, None
)
def get_my_exchange_trades(self, market):
return self.request(
"GET", "/exchange/api/v2/myTrades", "market=" + market, None
)
def create_exchange_limit_order(self, market, side, quantity, price):
query = "market={}&side={}&type=limit&quantity={}&price={}".format(
market, side, quantity, price
)
return self.request("POST", "/exchange/api/v2/order", query, None)
def create_exchange_buy_market_order(self, market, quantity):
query = "market={}&side=buy&type=market&secQuantity={}".format(market, quantity)
return self.request("POST", "/exchange/api/v2/order", query, None)
def create_exchange_sell_market_order(self, market, quantity):
query = "market={}&side=sell&type=market&quantity={}".format(market, quantity)
return self.request("POST", "/exchange/api/v2/order", query, None)
def cancel_exchange_order(self, market, order_id):
query = "market={}&orderId={}".format(market, order_id)
return self.request("DELETE", "/exchange/api/v2/order", query, None)
if __name__ == "__main__":
apikey = os.getenv("NICEHASH_APIKEY")
apisecret = os.getenv("NICEHASH_APISECRET")
org = os.getenv("NICEHASH_ORG")
private_api = private_api("https://api2.nicehash.com", org, apikey, apisecret)
while True:
btc = private_api.get_accounts()["currencies"][0]
available = btc["available"]
balance = btc["totalBalance"]
usdrate = requests.get(
"https://api2.nicehash.com/exchange/api/v2/info/prices"
).json()["BTCUSDC"]
raw = requests.get(
"https://api2.nicehash.com/main/api/v2/mining/external/3M6uRT9JF7VeSKQqtwzSUJZfgVWGW4xvw2/rigs2"
).json()
rigs = raw["miningRigs"]
btc_tp_per_24_hour = raw["totalProfitability"]
usd_tp_per_24_hour = btc_tp_per_24_hour * usdrate
per_month_profitability = usd_tp_per_24_hour * 30
json_body = [
{
"measurement": "nicehash",
"fields": {
"active_devices": raw["totalDevices"],
"per_month_profitability": per_month_profitability,
"nicehash_wallet_balance": balance,
"nicehash_wallet_balance_usd": float(balance) * float(usdrate),
},
}
]
client.write_points(json_body)
pprint.pprint(json_body)
sleep(10)