Skip to content

Commit

Permalink
Switch to NiceHash's demo API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
YoRyan committed Oct 12, 2019
1 parent d29c573 commit c283681
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 70 deletions.
64 changes: 6 additions & 58 deletions nuxhash/nicehash.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,19 @@
import json
import logging
from nuxhash.nhrest.python import nicehash as nh

from requests import Request, Session


API2_ENDPOINT = 'https://api2.nicehash.com/main/api/v2/'
TIMEOUT = 20


class NicehashException(Exception):
"""An exceptional reponse returned by the NiceHash service."""
pass


class APIError(NicehashException):

def __init__(self, result):
self.result = result

def __str__(self):
return f"NH: {self.result['error']}"


class BadResponse(NicehashException):

def __str__(self):
return 'Bad JSON response'


def api2_get(*path, **params):
return Request('GET', f"{API2_ENDPOINT}{'/'.join(path)}",
params=params).prepare()


def api2_post(body, *path, **params):
return Request('POST', f"{API2_ENDPOINT}{'/'.join(path)}",
data=body, params=params).prepare()


def send(prepped_request):
with Session() as session:
response = session.send(prepped_request, timeout=TIMEOUT)
try:
json_response = response.json()
except (ValueError, KeyError):
raise BadResponse
else:
if 'error' in json_response:
raise APIError(json_response)
else:
return json_response


def unpaid_balance(address):
result = api_call('stats.provider', { 'addr': address })
balances = result['stats']
return sum([float(b['balance']) for b in balances])
HOST = 'https://api2.nicehash.com'


def simplemultialgo_info(nx_settings):
response = send(api2_get('public', 'simplemultialgo', 'info'))
api = nh.public_api(HOST)
response = api.get_multialgo_info()
pay_factor = 1e-9 # GH -> H/s/day
return {algorithm['algorithm'].lower(): float(algorithm['paying'])*pay_factor
for algorithm in response['miningAlgorithms']}

def stratums(nx_settings):
response = send(api2_get('mining', 'algorithms'))
api = nh.public_api(HOST)
response = api.get_algorithms()
ports = {algorithm['algorithm'].lower(): algorithm['port']
for algorithm in response['miningAlgorithms']}
region = nx_settings['nicehash']['region']
Expand Down
12 changes: 0 additions & 12 deletions tests/test_nicehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
from nuxhash.settings import DEFAULT_SETTINGS


class TestNHApi(TestCase):

def test_get_request(self):
response = nh.send(nh.api2_get('exchangeRate', 'list'))
self.assertIn('list', response)

exchange = response['list'][0]
self.assertIn('exchangeRate', exchange)
self.assertIn('toCurrency', exchange)
self.assertIn('fromCurrency', exchange)


class TestNHMultialgo(TestCase):

def setUp(self):
Expand Down

0 comments on commit c283681

Please sign in to comment.