From 9729a20021efb075ed427e45c59102834bdf2a28 Mon Sep 17 00:00:00 2001 From: Ryan Young Date: Sat, 12 Oct 2019 19:41:44 -0700 Subject: [PATCH] gui: add unpaid mining balance indicator --- nuxhash/gui/mining.py | 32 ++++++++++++++++---------------- nuxhash/nicehash.py | 7 +++++-- tests/test_nicehash.py | 9 +++++++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/nuxhash/gui/mining.py b/nuxhash/gui/mining.py index d39a1fb..ea589e0 100644 --- a/nuxhash/gui/mining.py +++ b/nuxhash/gui/mining.py @@ -45,7 +45,7 @@ def __init__(self, parent, *args, devices=[], **kwargs): pub.subscribe(self._OnClose, 'app.close') - pub.subscribe(self._OnNewBalance, 'nicehash.balance') + pub.subscribe(self._OnNewUnpaidBalance, 'nicehash.unpaid') pub.subscribe(self._OnMiningStatus, 'mining.status') sizer = wx.BoxSizer(orient=wx.VERTICAL) @@ -78,11 +78,11 @@ def __init__(self, parent, *args, devices=[], **kwargs): self._Revenue.SetFont(self.GetFont().Bold()) balances.Add(self._Revenue, wx.SizerFlags().Expand()) - balances.Add(wx.StaticText(self, label='Address balance')) - self._Balance = wx.StaticText( + balances.Add(wx.StaticText(self, label='Unpaid mining balance')) + self._Unpaid = wx.StaticText( self, style=wx.ALIGN_RIGHT|wx.ST_NO_AUTORESIZE) - self._Balance.SetFont(self.GetFont().Bold()) - balances.Add(self._Balance, wx.SizerFlags().Expand()) + self._Unpaid.SetFont(self.GetFont().Bold()) + balances.Add(self._Unpaid, wx.SizerFlags().Expand()) bottomSizer.AddSpacer(main.PADDING_PX) @@ -95,7 +95,7 @@ def __init__(self, parent, *args, devices=[], **kwargs): def _OnSettings(self, settings): if settings != self._Settings: self._Settings = settings - self._UpdateBalance() + self._UpdateBalances() self._UpdateMining() def _OnBenchmarks(self, benchmarks): @@ -131,18 +131,18 @@ def _OnClose(self): self._Thread.stop() def _OnBalanceTimer(self, event): - self._UpdateBalance() + self._UpdateBalances() - def _UpdateBalance(self): + def _UpdateBalances(self): address = self._Settings['nicehash']['wallet'] if check_bc(address): - def request(address, target): - balance = unpaid_balance(address) - main.sendMessage(target, 'nicehash.balance', balance=balance) - thread = threading.Thread(target=request, args=(address, self)) + def do_requests(address, target): + unpaid = unpaid_balance(address) + main.sendMessage(target, 'nicehash.unpaid', balance=unpaid) + thread = threading.Thread(target=do_requests, args=(address, self)) thread.start() else: - pub.sendMessage('nicehash.balance', balance=None) + pub.sendMessage('nicehash.unpaid', balance=None) def OnStartStop(self, event): if not self._Thread: @@ -166,12 +166,12 @@ def _StopMining(self): self._Thread.stop() self._Thread = None - def _OnNewBalance(self, balance): + def _OnNewUnpaidBalance(self, balance): if balance is None: - self._Balance.SetLabel('') + self._Unpaid.SetLabel('') else: unit = self._Settings['gui']['units'] - self._Balance.SetLabel(utils.format_balance(balance, unit)) + self._Unpaid.SetLabel(utils.format_balance(balance, unit)) def _OnMiningStatus(self, speeds, revenue, devices): totalRevenue = sum(revenue.values()) diff --git a/nuxhash/nicehash.py b/nuxhash/nicehash.py index 97cbaf0..de65f73 100644 --- a/nuxhash/nicehash.py +++ b/nuxhash/nicehash.py @@ -20,6 +20,9 @@ def stratums(nx_settings): return {algorithm: f'{algorithm}.{region}.nicehash.com:{port}' for algorithm, port in ports.items()} -def unpaid_balance(nx_settings, address): - return None +def unpaid_balance(address): + api = nh.public_api(HOST) + response = api.request( + 'GET', f'/main/api/v2/mining/external/{address}/rigs/', '', None) + return float(response['unpaidAmount']) diff --git a/tests/test_nicehash.py b/tests/test_nicehash.py index 0f36683..93f88f3 100644 --- a/tests/test_nicehash.py +++ b/tests/test_nicehash.py @@ -2,6 +2,7 @@ import nuxhash.nicehash as nh from nuxhash.settings import DEFAULT_SETTINGS +from nuxhash.daemon import DONATE_ADDRESS class TestNHMultialgo(TestCase): @@ -18,6 +19,14 @@ def test_stratum(self): stratums = nh.stratums(self.settings) self.assertIn('cryptonight.eu.nicehash.com', stratums['cryptonight']) + +class TestNHBalances(TestCase): + + def test_unpaid_balance(self): + balance = nh.unpaid_balance(DONATE_ADDRESS) + self.assertGreaterEqual(balance, 0.0) + + if __name__ == '__main__': main()