update currency conversion

This commit is contained in:
Jack Robison 2017-05-30 17:07:23 -04:00
parent 96927ec985
commit 9f87d502c3
4 changed files with 30 additions and 52 deletions

View file

@ -822,7 +822,9 @@ class Daemon(AuthJSONRPCServer):
return d return d
def _add_key_fee_to_est_data_cost(self, fee, data_cost): 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 return data_cost + fee_amount
@defer.inlineCallbacks @defer.inlineCallbacks
@ -1855,8 +1857,11 @@ class Daemon(AuthJSONRPCServer):
If no path is given but a metadata dict is provided, If no path is given but a metadata dict is provided,
the source from the given metadata will be used. the source from the given metadata will be used.
--fee=<fee> : Dictionary representing key fee to download content: --fee=<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 supported currencies: LBC, USD, BTC
If an address is not provided a new one will be If an address is not provided a new one will be
automatically generated. Default fee is zero. automatically generated. Default fee is zero.

View file

@ -94,24 +94,28 @@ class GetStream(object):
log.info("Downloading stream descriptor blob (%i seconds)", self.timeout_counter) log.info("Downloading stream descriptor blob (%i seconds)", self.timeout_counter)
def convert_max_fee(self): def convert_max_fee(self):
max_fee = Fee(self.max_key_fee) currency, amount = self.max_key_fee['currency'], self.max_key_fee['amount']
if max_fee.currency_symbol == "LBC": return self.exchange_rate_manager.convert_currency(currency, "LBC", amount)
return max_fee.amount
return self.exchange_rate_manager.to_lbc(self.max_key_fee).amount
def set_status(self, status, name): def set_status(self, status, name):
log.info("Download lbry://%s status changed to %s" % (name, status)) log.info("Download lbry://%s status changed to %s" % (name, status))
self.code = next(s for s in STREAM_STAGES if s[0] == status) self.code = next(s for s in STREAM_STAGES if s[0] == status)
def check_fee(self, fee): def check_fee_and_convert(self, fee):
max_key_fee = self.convert_max_fee() max_key_fee_amount = self.convert_max_fee()
converted_fee = self.exchange_rate_manager.to_lbc(fee).amount converted_fee_amount = self.exchange_rate_manager.convert_currency(fee.currency, "LBC",
if converted_fee > self.wallet.get_balance(): fee.amount)
raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee) if converted_fee_amount > self.wallet.get_balance():
if converted_fee > max_key_fee: raise InsufficientFundsError('Unable to pay the key fee of %s' % converted_fee_amount)
raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee, if converted_fee_amount > max_key_fee_amount:
max_key_fee)) raise KeyFeeAboveMaxAllowed('Key fee %s above max allowed %s' % (converted_fee_amount,
return fee 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): def get_downloader_factory(self, factories):
for factory in factories: for factory in factories:
@ -142,8 +146,7 @@ class GetStream(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def pay_key_fee(self, fee, name): def pay_key_fee(self, fee, name):
if fee is not None: if fee is not None:
fee_lbc = self.exchange_rate_manager.to_lbc(fee).amount yield self._pay_key_fee(fee.address, fee.amount, name)
yield self._pay_key_fee(fee.address, fee_lbc, name)
else: else:
defer.returnValue(None) defer.returnValue(None)
@ -168,7 +171,7 @@ class GetStream(object):
if stream_info.has_fee: if stream_info.has_fee:
try: try:
fee = yield threads.deferToThread(self.check_fee, fee = yield threads.deferToThread(self.check_fee_and_convert,
stream_info.source_fee) stream_info.source_fee)
except Exception as err: except Exception as err:
self._running = False self._running = False

View file

@ -5,8 +5,6 @@ import json
from twisted.internet import defer, threads from twisted.internet import defer, threads
from twisted.internet.task import LoopingCall from twisted.internet.task import LoopingCall
from lbryschema.fee import Fee
from lbrynet import conf from lbrynet import conf
from lbrynet.core.Error import InvalidExchangeRateResponse from lbrynet.core.Error import InvalidExchangeRateResponse
@ -201,20 +199,6 @@ class ExchangeRateManager(object):
def fee_dict(self): def fee_dict(self):
return {market: market.rate.as_dict() for market in self.market_feeds} 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): class DummyBTCLBCFeed(MarketFeed):
def __init__(self): def __init__(self):
@ -258,17 +242,3 @@ class DummyExchangeRateManager(object):
market.rate.currency_pair[0] == from_currency): market.rate.currency_pair[0] == from_currency):
return self.convert_currency( return self.convert_currency(
market.rate.currency_pair[1], to_currency, amount * market.rate.spot) 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
})

View file

@ -23,18 +23,18 @@ class FeeTest(unittest.TestCase):
util.resetTime(self) util.resetTime(self)
def test_fee_converts_to_lbc(self): def test_fee_converts_to_lbc(self):
fee_dict = { fee = Fee({
'currency':'USD', 'currency':'USD',
'amount': 10.0, 'amount': 10.0,
'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9" 'address': "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
} })
rates = { rates = {
'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1}, 'BTCLBC': {'spot': 3.0, 'ts': util.DEFAULT_ISO_TIME + 1},
'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2} 'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}
} }
manager = ExchangeRateManager.DummyExchangeRateManager(rates) 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) self.assertEqual(60.0, result)
class GoogleBTCFeedTest(unittest.TestCase): class GoogleBTCFeedTest(unittest.TestCase):