-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nicehash.py
324 lines (248 loc) · 12.3 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
from datetime import datetime
from time import mktime
import uuid
import hmac
import requests
import json
from hashlib import sha256
import optparse
import sys
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__":
parser = optparse.OptionParser()
parser.add_option('-b', '--base_url', dest="base", help="Api base url", default="https://api2.nicehash.com")
parser.add_option('-o', '--organization_id', dest="org", help="Organization id")
parser.add_option('-k', '--key', dest="key", help="Api key")
parser.add_option('-s', '--secret', dest="secret", help="Secret for api key")
parser.add_option('-m', '--method', dest="method", help="Method for request", default="GET")
parser.add_option('-p', '--path', dest="path", help="Path for request", default="/")
parser.add_option('-q', '--params', dest="params", help="Parameters for request")
parser.add_option('-d', '--body', dest="body", help="Body for request")
options, args = parser.parse_args()
private_api = private_api(options.base, options.org, options.key, options.secret)
params = ''
if options.params is not None:
params = options.params
try:
response = private_api.request(options.method, options.path, params, options.body)
except Exception as ex:
print("Unexpected error:", ex)
exit(1)
print(response)
exit(0)