2016-07-28 11:30:13 +02:00
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import json
|
2018-05-05 07:33:37 +02:00
|
|
|
|
|
|
|
import treq
|
|
|
|
from twisted.internet import defer
|
2016-07-28 11:30:13 +02:00
|
|
|
from twisted.internet.task import LoopingCall
|
|
|
|
|
2017-02-28 02:18:57 +01:00
|
|
|
from lbrynet.core.Error import InvalidExchangeRateResponse
|
2016-12-05 22:51:16 +01:00
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CURRENCY_PAIRS = ["USDBTC", "BTCLBC"]
|
|
|
|
BITTREX_FEE = 0.0025
|
|
|
|
COINBASE_FEE = 0.0 #add fee
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRate(object):
|
|
|
|
def __init__(self, market, spot, ts):
|
2017-10-09 21:20:58 +02:00
|
|
|
if not int(time.time()) - ts < 600:
|
2017-10-17 04:11:20 +02:00
|
|
|
raise ValueError('The timestamp is too dated.')
|
2017-10-09 21:20:58 +02:00
|
|
|
if not spot > 0:
|
2017-10-17 04:11:20 +02:00
|
|
|
raise ValueError('Spot must be greater than 0.')
|
2016-07-28 11:30:13 +02:00
|
|
|
self.currency_pair = (market[0:3], market[3:6])
|
|
|
|
self.spot = spot
|
|
|
|
self.ts = ts
|
|
|
|
|
2017-02-28 02:18:57 +01:00
|
|
|
def __repr__(self):
|
|
|
|
out = "Currency pair:{}, spot:{}, ts:{}".format(
|
|
|
|
self.currency_pair, self.spot, self.ts)
|
|
|
|
return out
|
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
def as_dict(self):
|
|
|
|
return {'spot': self.spot, 'ts': self.ts}
|
|
|
|
|
|
|
|
|
|
|
|
class MarketFeed(object):
|
2017-02-28 02:18:57 +01:00
|
|
|
REQUESTS_TIMEOUT = 20
|
|
|
|
EXCHANGE_RATE_UPDATE_RATE_SEC = 300
|
2016-07-28 11:30:13 +02:00
|
|
|
def __init__(self, market, name, url, params, fee):
|
|
|
|
self.market = market
|
|
|
|
self.name = name
|
|
|
|
self.url = url
|
|
|
|
self.params = params
|
|
|
|
self.fee = fee
|
|
|
|
self.rate = None
|
|
|
|
self._updater = LoopingCall(self._update_price)
|
2017-09-21 17:49:01 +02:00
|
|
|
self._online = True
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2017-02-28 02:18:57 +01:00
|
|
|
def rate_is_initialized(self):
|
|
|
|
return self.rate is not None
|
|
|
|
|
2017-09-21 17:49:01 +02:00
|
|
|
def is_online(self):
|
|
|
|
return self._online
|
|
|
|
|
2018-05-05 07:33:37 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-07-28 11:30:13 +02:00
|
|
|
def _make_request(self):
|
2018-05-05 07:33:37 +02:00
|
|
|
response = yield treq.get(self.url, params=self.params, timeout=self.REQUESTS_TIMEOUT)
|
|
|
|
defer.returnValue((yield response.content()))
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def _handle_response(self, response):
|
|
|
|
return NotImplementedError
|
|
|
|
|
|
|
|
def _subtract_fee(self, from_amount):
|
2016-09-28 05:56:08 +02:00
|
|
|
# increase amount to account for market fees
|
2016-07-28 11:30:13 +02:00
|
|
|
return defer.succeed(from_amount / (1.0 - self.fee))
|
|
|
|
|
|
|
|
def _save_price(self, price):
|
2017-10-23 22:04:10 +02:00
|
|
|
log.debug("Saving price update %f for %s from %s" % (price, self.market, self.name))
|
2016-07-28 11:30:13 +02:00
|
|
|
self.rate = ExchangeRate(self.market, price, int(time.time()))
|
2017-10-23 22:44:26 +02:00
|
|
|
self._online = True
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2017-10-23 22:44:26 +02:00
|
|
|
def _on_error(self, err):
|
2017-11-10 20:23:14 +01:00
|
|
|
log.warning("There was a problem updating %s exchange rate information from %s",
|
2017-11-10 16:34:36 +01:00
|
|
|
self.market, self.name)
|
|
|
|
log.debug("Exchange rate error (%s from %s): %s", self.market, self.name, err)
|
2017-10-23 22:44:26 +02:00
|
|
|
self._online = False
|
2016-09-28 05:56:08 +02:00
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
def _update_price(self):
|
2018-05-05 07:33:37 +02:00
|
|
|
d = self._make_request()
|
2016-07-28 11:30:13 +02:00
|
|
|
d.addCallback(self._handle_response)
|
|
|
|
d.addCallback(self._subtract_fee)
|
|
|
|
d.addCallback(self._save_price)
|
2017-10-23 22:44:26 +02:00
|
|
|
d.addErrback(self._on_error)
|
2017-02-28 02:18:57 +01:00
|
|
|
return d
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if not self._updater.running:
|
2017-02-28 02:18:57 +01:00
|
|
|
self._updater.start(self.EXCHANGE_RATE_UPDATE_RATE_SEC)
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self._updater.running:
|
|
|
|
self._updater.stop()
|
|
|
|
|
|
|
|
|
|
|
|
class BittrexFeed(MarketFeed):
|
|
|
|
def __init__(self):
|
|
|
|
MarketFeed.__init__(
|
|
|
|
self,
|
|
|
|
"BTCLBC",
|
|
|
|
"Bittrex",
|
2017-10-23 22:05:41 +02:00
|
|
|
"https://bittrex.com/api/v1.1/public/getmarkethistory",
|
2016-07-28 11:30:13 +02:00
|
|
|
{'market': 'BTC-LBC', 'count': 50},
|
|
|
|
BITTREX_FEE
|
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_response(self, response):
|
2017-02-28 02:18:57 +01:00
|
|
|
json_response = json.loads(response)
|
|
|
|
if 'result' not in json_response:
|
|
|
|
raise InvalidExchangeRateResponse(self.name, 'result not found')
|
|
|
|
trades = json_response['result']
|
|
|
|
if len(trades) == 0:
|
|
|
|
raise InvalidExchangeRateResponse(self.market, 'trades not found')
|
|
|
|
totals = sum([i['Total'] for i in trades])
|
|
|
|
qtys = sum([i['Quantity'] for i in trades])
|
|
|
|
if totals <= 0 or qtys <= 0:
|
|
|
|
raise InvalidExchangeRateResponse(self.market, 'quantities were not positive')
|
|
|
|
vwap = totals/qtys
|
2016-07-28 11:30:13 +02:00
|
|
|
return defer.succeed(float(1.0 / vwap))
|
|
|
|
|
|
|
|
|
2017-04-05 04:41:57 +02:00
|
|
|
class LBRYioFeed(MarketFeed):
|
|
|
|
def __init__(self):
|
|
|
|
MarketFeed.__init__(
|
|
|
|
self,
|
|
|
|
"BTCLBC",
|
|
|
|
"lbry.io",
|
|
|
|
"https://api.lbry.io/lbc/exchange_rate",
|
|
|
|
{},
|
|
|
|
0.0,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_response(self, response):
|
|
|
|
json_response = json.loads(response)
|
|
|
|
if 'data' not in json_response:
|
|
|
|
raise InvalidExchangeRateResponse(self.name, 'result not found')
|
|
|
|
return defer.succeed(1.0 / json_response['data']['lbc_btc'])
|
|
|
|
|
|
|
|
|
2017-09-07 19:55:36 +02:00
|
|
|
class LBRYioBTCFeed(MarketFeed):
|
2016-07-28 11:30:13 +02:00
|
|
|
def __init__(self):
|
|
|
|
MarketFeed.__init__(
|
|
|
|
self,
|
|
|
|
"USDBTC",
|
2017-09-07 19:55:36 +02:00
|
|
|
"lbry.io",
|
|
|
|
"https://api.lbry.io/lbc/exchange_rate",
|
|
|
|
{},
|
|
|
|
0.0,
|
2016-07-28 11:30:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_response(self, response):
|
2017-09-15 19:49:07 +02:00
|
|
|
try:
|
|
|
|
json_response = json.loads(response)
|
|
|
|
except ValueError:
|
2017-09-15 21:10:31 +02:00
|
|
|
raise InvalidExchangeRateResponse(self.name, "invalid rate response : %s" % response)
|
2017-09-07 19:55:36 +02:00
|
|
|
if 'data' not in json_response:
|
|
|
|
raise InvalidExchangeRateResponse(self.name, 'result not found')
|
|
|
|
return defer.succeed(1.0 / json_response['data']['btc_usd'])
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
|
2017-09-21 17:49:01 +02:00
|
|
|
class CryptonatorBTCFeed(MarketFeed):
|
|
|
|
def __init__(self):
|
|
|
|
MarketFeed.__init__(
|
|
|
|
self,
|
|
|
|
"USDBTC",
|
|
|
|
"cryptonator.com",
|
|
|
|
"https://api.cryptonator.com/api/ticker/usd-btc",
|
|
|
|
{},
|
|
|
|
0.0,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_response(self, response):
|
2017-10-08 16:15:53 +02:00
|
|
|
try:
|
|
|
|
json_response = json.loads(response)
|
|
|
|
except ValueError:
|
2018-02-27 20:06:45 +01:00
|
|
|
raise InvalidExchangeRateResponse(self.name, "invalid rate response")
|
2017-10-08 16:15:53 +02:00
|
|
|
if 'ticker' not in json_response or len(json_response['ticker']) == 0 or \
|
|
|
|
'success' not in json_response or json_response['success'] is not True:
|
2017-09-21 17:49:01 +02:00
|
|
|
raise InvalidExchangeRateResponse(self.name, 'result not found')
|
|
|
|
return defer.succeed(float(json_response['ticker']['price']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CryptonatorFeed(MarketFeed):
|
|
|
|
def __init__(self):
|
|
|
|
MarketFeed.__init__(
|
|
|
|
self,
|
|
|
|
"BTCLBC",
|
|
|
|
"cryptonator.com",
|
|
|
|
"https://api.cryptonator.com/api/ticker/btc-lbc",
|
|
|
|
{},
|
|
|
|
0.0,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_response(self, response):
|
2017-10-08 16:15:53 +02:00
|
|
|
try:
|
|
|
|
json_response = json.loads(response)
|
|
|
|
except ValueError:
|
2018-02-27 20:06:45 +01:00
|
|
|
raise InvalidExchangeRateResponse(self.name, "invalid rate response")
|
2017-10-08 16:15:53 +02:00
|
|
|
if 'ticker' not in json_response or len(json_response['ticker']) == 0 or \
|
|
|
|
'success' not in json_response or json_response['success'] is not True:
|
2017-09-21 17:49:01 +02:00
|
|
|
raise InvalidExchangeRateResponse(self.name, 'result not found')
|
|
|
|
return defer.succeed(float(json_response['ticker']['price']))
|
|
|
|
|
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
class ExchangeRateManager(object):
|
|
|
|
def __init__(self):
|
2016-11-30 21:20:45 +01:00
|
|
|
self.market_feeds = [
|
2017-10-23 22:05:41 +02:00
|
|
|
LBRYioBTCFeed(), LBRYioFeed(), BittrexFeed(), CryptonatorBTCFeed(), CryptonatorFeed()]
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
log.info("Starting exchange rate manager")
|
|
|
|
for feed in self.market_feeds:
|
|
|
|
feed.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
log.info("Stopping exchange rate manager")
|
|
|
|
for source in self.market_feeds:
|
|
|
|
source.stop()
|
|
|
|
|
|
|
|
def convert_currency(self, from_currency, to_currency, amount):
|
2017-02-28 02:18:57 +01:00
|
|
|
rates = [market.rate for market in self.market_feeds]
|
|
|
|
log.info("Converting %f %s to %s, rates: %s" % (amount, from_currency, to_currency, rates))
|
2016-07-28 18:43:20 +02:00
|
|
|
if from_currency == to_currency:
|
|
|
|
return amount
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
for market in self.market_feeds:
|
2017-10-30 21:01:25 +01:00
|
|
|
if (market.rate_is_initialized() and market.is_online() and
|
2017-02-28 02:18:57 +01:00
|
|
|
market.rate.currency_pair == (from_currency, to_currency)):
|
2016-07-28 11:30:13 +02:00
|
|
|
return amount * market.rate.spot
|
|
|
|
for market in self.market_feeds:
|
2017-10-30 21:01:25 +01:00
|
|
|
if (market.rate_is_initialized() and market.is_online() and
|
2017-02-28 02:18:57 +01:00
|
|
|
market.rate.currency_pair[0] == from_currency):
|
2016-11-30 21:20:45 +01:00
|
|
|
return self.convert_currency(
|
|
|
|
market.rate.currency_pair[1], to_currency, amount * market.rate.spot)
|
|
|
|
raise Exception(
|
|
|
|
'Unable to convert {} from {} to {}'.format(amount, from_currency, to_currency))
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def fee_dict(self):
|
|
|
|
return {market: market.rate.as_dict() for market in self.market_feeds}
|