From 9f87d502c3d0442b35847c50e0e9139fa0916e49 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 30 May 2017 17:07:23 -0400 Subject: [PATCH] update currency conversion --- lbrynet/lbrynet_daemon/Daemon.py | 11 ++++-- lbrynet/lbrynet_daemon/Downloader.py | 35 ++++++++++--------- lbrynet/lbrynet_daemon/ExchangeRateManager.py | 30 ---------------- .../test_ExchangeRateManager.py | 6 ++-- 4 files changed, 30 insertions(+), 52 deletions(-) diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 7989f5e9e..0c8aabcf4 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -822,7 +822,9 @@ class Daemon(AuthJSONRPCServer): return d def _add_key_fee_to_est_data_cost(self, fee, data_cost): - fee_amount = 0.0 if not fee else self.exchange_rate_manager.to_lbc(fee).amount + fee_amount = 0.0 if not fee else self.exchange_rate_manager.convert_currency(fee.currency, + "LBC", + fee.amount) return data_cost + fee_amount @defer.inlineCallbacks @@ -1855,8 +1857,11 @@ class Daemon(AuthJSONRPCServer): If no path is given but a metadata dict is provided, the source from the given metadata will be used. --fee= : Dictionary representing key fee to download content: - {currency_symbol: {'amount': float, - 'address': str, optional}} + { + 'currency': currency_symbol, + 'amount': float, + 'address': str, optional + } supported currencies: LBC, USD, BTC If an address is not provided a new one will be automatically generated. Default fee is zero. diff --git a/lbrynet/lbrynet_daemon/Downloader.py b/lbrynet/lbrynet_daemon/Downloader.py index 7e5727ee4..b69c8914c 100644 --- a/lbrynet/lbrynet_daemon/Downloader.py +++ b/lbrynet/lbrynet_daemon/Downloader.py @@ -94,24 +94,28 @@ class GetStream(object): log.info("Downloading stream descriptor blob (%i seconds)", self.timeout_counter) def convert_max_fee(self): - max_fee = Fee(self.max_key_fee) - if max_fee.currency_symbol == "LBC": - return max_fee.amount - return self.exchange_rate_manager.to_lbc(self.max_key_fee).amount + currency, amount = self.max_key_fee['currency'], self.max_key_fee['amount'] + return self.exchange_rate_manager.convert_currency(currency, "LBC", amount) def set_status(self, status, name): log.info("Download lbry://%s status changed to %s" % (name, status)) self.code = next(s for s in STREAM_STAGES if s[0] == status) - def check_fee(self, fee): - max_key_fee = self.convert_max_fee() - converted_fee = self.exchange_rate_manager.to_lbc(fee).amount - if converted_fee > self.wallet.get_balance(): - raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee) - if converted_fee > max_key_fee: - raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee, - max_key_fee)) - return fee + def check_fee_and_convert(self, fee): + max_key_fee_amount = self.convert_max_fee() + converted_fee_amount = self.exchange_rate_manager.convert_currency(fee.currency, "LBC", + fee.amount) + if converted_fee_amount > self.wallet.get_balance(): + raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee_amount) + if converted_fee_amount > max_key_fee_amount: + raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee_amount, + max_key_fee_amount)) + converted_fee = { + 'currency': 'LBC', + 'amount': converted_fee_amount, + 'address': fee.address + } + return Fee(converted_fee) def get_downloader_factory(self, factories): for factory in factories: @@ -142,8 +146,7 @@ class GetStream(object): @defer.inlineCallbacks def pay_key_fee(self, fee, name): if fee is not None: - fee_lbc = self.exchange_rate_manager.to_lbc(fee).amount - yield self._pay_key_fee(fee.address, fee_lbc, name) + yield self._pay_key_fee(fee.address, fee.amount, name) else: defer.returnValue(None) @@ -168,7 +171,7 @@ class GetStream(object): if stream_info.has_fee: try: - fee = yield threads.deferToThread(self.check_fee, + fee = yield threads.deferToThread(self.check_fee_and_convert, stream_info.source_fee) except Exception as err: self._running = False diff --git a/lbrynet/lbrynet_daemon/ExchangeRateManager.py b/lbrynet/lbrynet_daemon/ExchangeRateManager.py index 68262bed2..3cd43dcc0 100644 --- a/lbrynet/lbrynet_daemon/ExchangeRateManager.py +++ b/lbrynet/lbrynet_daemon/ExchangeRateManager.py @@ -5,8 +5,6 @@ import json from twisted.internet import defer, threads from twisted.internet.task import LoopingCall -from lbryschema.fee import Fee - from lbrynet import conf from lbrynet.core.Error import InvalidExchangeRateResponse @@ -201,20 +199,6 @@ class ExchangeRateManager(object): def fee_dict(self): return {market: market.rate.as_dict() for market in self.market_feeds} - def to_lbc(self, fee): - if fee is None: - return None - if not isinstance(fee, Fee): - fee_in = Fee(fee) - else: - fee_in = fee - - return Fee({ - 'currency':fee_in.currency, - 'amount': self.convert_currency(fee_in.currency, "LBC", fee_in.amount), - 'address': fee_in.address - }) - class DummyBTCLBCFeed(MarketFeed): def __init__(self): @@ -258,17 +242,3 @@ class DummyExchangeRateManager(object): market.rate.currency_pair[0] == from_currency): return self.convert_currency( market.rate.currency_pair[1], to_currency, amount * market.rate.spot) - - def to_lbc(self, fee): - if fee is None: - return None - if not isinstance(fee, Fee): - fee_in = Fee(fee) - else: - fee_in = fee - - return Fee({ - 'currency':fee_in.currency, - 'amount': self.convert_currency(fee_in.currency, "LBC", fee_in.amount), - 'address': fee_in.address - }) diff --git a/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py b/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py index eacf7708b..7e00192ca 100644 --- a/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py +++ b/tests/unit/lbrynet_daemon/test_ExchangeRateManager.py @@ -23,18 +23,18 @@ class FeeTest(unittest.TestCase): util.resetTime(self) def test_fee_converts_to_lbc(self): - fee_dict = { + fee = Fee({ 'currency':'USD', 'amount': 10.0, 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" - } + }) rates = { 'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1}, 'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2} } manager = ExchangeRateManager.DummyExchangeRateManager(rates) - result = manager.to_lbc(fee_dict).amount + result = manager.convert_currency(fee.currency, "LBC", fee.amount) self.assertEqual(60.0, result) class GoogleBTCFeedTest(unittest.TestCase):