2019-12-15 07:02:18 +01:00
|
|
|
import json
|
2016-07-28 11:30:13 +02:00
|
|
|
import time
|
2019-12-15 07:02:18 +01:00
|
|
|
import asyncio
|
2016-07-28 11:30:13 +02:00
|
|
|
import logging
|
2021-02-15 19:40:56 +01:00
|
|
|
from statistics import median
|
2019-03-20 06:46:23 +01:00
|
|
|
from decimal import Decimal
|
2019-12-15 07:02:18 +01:00
|
|
|
from typing import Optional, Iterable, Type
|
2021-05-04 06:20:18 +02:00
|
|
|
from aiohttp.client_exceptions import ContentTypeError, ClientConnectionError
|
2019-11-19 19:57:14 +01:00
|
|
|
from lbry.error import InvalidExchangeRateResponseError, CurrencyConversionError
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.utils import aiohttp_request
|
2019-10-29 06:26:25 +01:00
|
|
|
from lbry.wallet.dewies import lbc_to_dewies
|
2016-12-05 22:51:16 +01:00
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class ExchangeRate:
|
2016-07-28 11:30:13 +02:00
|
|
|
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):
|
2019-12-15 07:02:18 +01:00
|
|
|
return f"Currency pair:{self.currency_pair}, spot:{self.spot}, ts:{self.ts}"
|
2017-02-28 02:18:57 +01:00
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
def as_dict(self):
|
|
|
|
return {'spot': self.spot, 'ts': self.ts}
|
|
|
|
|
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
class MarketFeed:
|
2020-01-03 07:22:27 +01:00
|
|
|
name: str = ""
|
|
|
|
market: str = ""
|
|
|
|
url: str = ""
|
2019-12-15 07:02:18 +01:00
|
|
|
params = {}
|
|
|
|
fee = 0
|
2019-10-14 11:17:18 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
update_interval = 300
|
|
|
|
request_timeout = 50
|
2019-10-14 11:17:18 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.rate: Optional[float] = None
|
|
|
|
self.last_check = 0
|
|
|
|
self._last_response = None
|
2019-08-21 20:26:52 +02:00
|
|
|
self._task: Optional[asyncio.Task] = None
|
2019-12-15 07:02:18 +01:00
|
|
|
self.event = asyncio.Event()
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
@property
|
|
|
|
def has_rate(self):
|
2017-02-28 02:18:57 +01:00
|
|
|
return self.rate is not None
|
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
@property
|
2017-09-21 17:49:01 +02:00
|
|
|
def is_online(self):
|
2019-12-15 07:02:18 +01:00
|
|
|
return self.last_check+self.update_interval+self.request_timeout > time.time()
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2020-01-03 07:15:33 +01:00
|
|
|
def get_rate_from_response(self, json_response):
|
2019-01-22 15:52:43 +01:00
|
|
|
raise NotImplementedError()
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
async def get_response(self):
|
2021-02-10 19:29:05 +01:00
|
|
|
async with aiohttp_request(
|
2021-02-10 19:45:35 +01:00
|
|
|
'get', self.url, params=self.params,
|
|
|
|
timeout=self.request_timeout, headers={"User-Agent": "lbrynet"}
|
2021-02-10 19:29:05 +01:00
|
|
|
) as response:
|
2019-12-31 22:33:08 +01:00
|
|
|
try:
|
2021-02-10 19:29:05 +01:00
|
|
|
self._last_response = await response.json(content_type=None)
|
2019-12-31 22:33:08 +01:00
|
|
|
except ContentTypeError as e:
|
|
|
|
self._last_response = {}
|
|
|
|
log.warning("Could not parse exchange rate response from %s: %s", self.name, e.message)
|
|
|
|
log.debug(await response.text())
|
2019-12-15 07:02:18 +01:00
|
|
|
return self._last_response
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
async def get_rate(self):
|
2019-10-14 11:17:18 +02:00
|
|
|
try:
|
2019-12-15 07:02:18 +01:00
|
|
|
data = await self.get_response()
|
|
|
|
rate = self.get_rate_from_response(data)
|
|
|
|
rate = rate / (1.0 - self.fee)
|
|
|
|
log.debug("Saving rate update %f for %s from %s", rate, self.market, self.name)
|
|
|
|
self.rate = ExchangeRate(self.market, rate, int(time.time()))
|
|
|
|
self.last_check = time.time()
|
|
|
|
return self.rate
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
raise
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
log.warning("Timed out fetching exchange rate from %s.", self.name)
|
|
|
|
except json.JSONDecodeError as e:
|
2021-05-04 06:16:29 +02:00
|
|
|
msg = e.doc if '<html>' not in e.doc else 'unexpected content type.'
|
|
|
|
log.warning("Could not parse exchange rate response from %s: %s", self.name, msg)
|
2021-05-04 06:43:51 +02:00
|
|
|
log.debug(e.doc)
|
2019-12-15 07:02:18 +01:00
|
|
|
except InvalidExchangeRateResponseError as e:
|
|
|
|
log.warning(str(e))
|
2021-05-04 06:20:18 +02:00
|
|
|
except ClientConnectionError as e:
|
|
|
|
log.warning("Error trying to connect to exchange rate %s: %s", self.name, str(e))
|
2019-12-15 07:02:18 +01:00
|
|
|
except Exception as e:
|
|
|
|
log.exception("Exchange rate error (%s from %s):", self.market, self.name)
|
2021-05-04 06:40:57 +02:00
|
|
|
finally:
|
|
|
|
self.event.set()
|
2019-12-15 07:02:18 +01:00
|
|
|
|
|
|
|
async def keep_updated(self):
|
2019-01-22 15:52:43 +01:00
|
|
|
while True:
|
2019-12-15 07:02:18 +01:00
|
|
|
await self.get_rate()
|
|
|
|
await asyncio.sleep(self.update_interval)
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def start(self):
|
2019-01-22 15:52:43 +01:00
|
|
|
if not self._task:
|
2019-12-15 07:02:18 +01:00
|
|
|
self._task = asyncio.create_task(self.keep_updated())
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
def stop(self):
|
2019-01-22 15:52:43 +01:00
|
|
|
if self._task and not self._task.done():
|
|
|
|
self._task.cancel()
|
|
|
|
self._task = None
|
2019-12-15 07:02:18 +01:00
|
|
|
self.event.clear()
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
|
2021-02-10 19:29:05 +01:00
|
|
|
class BaseBittrexFeed(MarketFeed):
|
2019-12-15 07:02:18 +01:00
|
|
|
name = "Bittrex"
|
2021-02-10 19:29:05 +01:00
|
|
|
market = None
|
|
|
|
url = None
|
2019-12-15 07:02:18 +01:00
|
|
|
fee = 0.0025
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
def get_rate_from_response(self, json_response):
|
2021-02-03 16:10:18 +01:00
|
|
|
if 'lastTradeRate' not in json_response:
|
2019-11-19 19:57:14 +01:00
|
|
|
raise InvalidExchangeRateResponseError(self.name, 'result not found')
|
2021-02-03 16:10:18 +01:00
|
|
|
return 1.0 / float(json_response['lastTradeRate'])
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
|
2021-02-10 19:29:05 +01:00
|
|
|
class BittrexBTCFeed(BaseBittrexFeed):
|
2019-12-15 07:02:18 +01:00
|
|
|
market = "BTCLBC"
|
2021-02-10 19:29:05 +01:00
|
|
|
url = "https://api.bittrex.com/v3/markets/LBC-BTC/ticker"
|
|
|
|
|
|
|
|
|
|
|
|
class BittrexUSDFeed(BaseBittrexFeed):
|
|
|
|
market = "USDLBC"
|
|
|
|
url = "https://api.bittrex.com/v3/markets/LBC-USD/ticker"
|
|
|
|
|
|
|
|
|
|
|
|
class BaseCoinExFeed(MarketFeed):
|
|
|
|
name = "CoinEx"
|
|
|
|
market = None
|
|
|
|
url = None
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
def get_rate_from_response(self, json_response):
|
2021-02-10 19:29:05 +01:00
|
|
|
if 'data' not in json_response or \
|
|
|
|
'ticker' not in json_response['data'] or \
|
|
|
|
'last' not in json_response['data']['ticker']:
|
2019-11-19 19:57:14 +01:00
|
|
|
raise InvalidExchangeRateResponseError(self.name, 'result not found')
|
2021-02-10 19:29:05 +01:00
|
|
|
return 1.0 / float(json_response['data']['ticker']['last'])
|
2016-07-28 11:30:13 +02:00
|
|
|
|
|
|
|
|
2021-02-10 19:29:05 +01:00
|
|
|
class CoinExBTCFeed(BaseCoinExFeed):
|
2019-12-15 07:02:18 +01:00
|
|
|
market = "BTCLBC"
|
2021-02-10 19:29:05 +01:00
|
|
|
url = "https://api.coinex.com/v1/market/ticker?market=LBCBTC"
|
|
|
|
|
|
|
|
|
|
|
|
class CoinExUSDFeed(BaseCoinExFeed):
|
|
|
|
market = "USDLBC"
|
|
|
|
url = "https://api.coinex.com/v1/market/ticker?market=LBCUSDT"
|
|
|
|
|
|
|
|
|
|
|
|
class BaseHotbitFeed(MarketFeed):
|
|
|
|
name = "hotbit"
|
|
|
|
market = None
|
|
|
|
url = "https://api.hotbit.io/api/v1/market.last"
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
def get_rate_from_response(self, json_response):
|
2021-02-10 19:29:05 +01:00
|
|
|
if 'result' not in json_response:
|
2019-11-19 19:57:14 +01:00
|
|
|
raise InvalidExchangeRateResponseError(self.name, 'result not found')
|
2021-02-10 19:29:05 +01:00
|
|
|
return 1.0 / float(json_response['result'])
|
|
|
|
|
|
|
|
|
|
|
|
class HotbitBTCFeed(BaseHotbitFeed):
|
|
|
|
market = "BTCLBC"
|
|
|
|
params = {"market": "LBC/BTC"}
|
|
|
|
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2021-02-10 19:29:05 +01:00
|
|
|
class HotbitUSDFeed(BaseHotbitFeed):
|
|
|
|
market = "USDLBC"
|
|
|
|
params = {"market": "LBC/USDT"}
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2021-02-10 19:29:05 +01:00
|
|
|
|
|
|
|
class UPbitBTCFeed(MarketFeed):
|
|
|
|
name = "UPbit"
|
|
|
|
market = "BTCLBC"
|
|
|
|
url = "https://api.upbit.com/v1/ticker"
|
|
|
|
params = {"markets": "BTC-LBC"}
|
|
|
|
|
|
|
|
def get_rate_from_response(self, json_response):
|
exchange_rate_manager: raise exception if `'error'` is in `json_response`
If the error is not handled, the running daemon will continuously
print the following error message:
```
Traceback (most recent call last):
File "lbry/extras/daemon/exchange_rate_manager.py", line 77, in get_rate
File "lbry/extras/daemon/exchange_rate_manager.py", line 189, in get_rate_from_response
KeyError: 0
```
This started happening when the UPBit exchange decided to delist
the LBC coin.
Normally `json_response` should be a dictionary, not a list,
so `json_response[0]` causes an error.
By checking for the `'error'` key, we can raise the proper exception.
Once this is done, the message will be a warning, not a traceback.
```
WARNING lbry.extras.daemon.exchange_rate_manager:92:
Failed to get exchange rate from UPbit: result not found
```
2021-07-13 18:54:14 +02:00
|
|
|
if "error" in json_response or len(json_response) != 1 or 'trade_price' not in json_response[0]:
|
2021-02-10 19:29:05 +01:00
|
|
|
raise InvalidExchangeRateResponseError(self.name, 'result not found')
|
|
|
|
return 1.0 / float(json_response[0]['trade_price'])
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2019-12-15 07:02:18 +01:00
|
|
|
|
|
|
|
FEEDS: Iterable[Type[MarketFeed]] = (
|
2021-02-10 19:29:05 +01:00
|
|
|
BittrexBTCFeed,
|
|
|
|
BittrexUSDFeed,
|
|
|
|
CoinExBTCFeed,
|
|
|
|
CoinExUSDFeed,
|
2022-08-11 15:45:23 +02:00
|
|
|
# HotbitBTCFeed,
|
|
|
|
# HotbitUSDFeed,
|
|
|
|
# UPbitBTCFeed,
|
2019-12-15 07:02:18 +01:00
|
|
|
)
|
2017-09-21 17:49:01 +02:00
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class ExchangeRateManager:
|
2019-12-15 07:02:18 +01:00
|
|
|
def __init__(self, feeds=FEEDS):
|
|
|
|
self.market_feeds = [Feed() for Feed in feeds]
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
return asyncio.wait(
|
|
|
|
[feed.event.wait() for feed in self.market_feeds],
|
|
|
|
)
|
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):
|
2021-02-15 19:40:56 +01:00
|
|
|
log.debug(
|
|
|
|
"Converting %f %s to %s, rates: %s",
|
|
|
|
amount, from_currency, to_currency,
|
|
|
|
[market.rate for market in self.market_feeds]
|
|
|
|
)
|
2016-07-28 18:43:20 +02:00
|
|
|
if from_currency == to_currency:
|
2019-10-29 06:26:25 +01:00
|
|
|
return round(amount, 8)
|
2017-09-21 17:49:01 +02:00
|
|
|
|
2021-02-15 19:40:56 +01:00
|
|
|
rates = []
|
2016-07-28 11:30:13 +02:00
|
|
|
for market in self.market_feeds:
|
2019-12-15 07:02:18 +01:00
|
|
|
if (market.has_rate and market.is_online and
|
2018-07-24 18:36:00 +02:00
|
|
|
market.rate.currency_pair == (from_currency, to_currency)):
|
2021-02-15 19:40:56 +01:00
|
|
|
rates.append(market.rate.spot)
|
|
|
|
|
|
|
|
if rates:
|
|
|
|
return round(amount * Decimal(median(rates)), 8)
|
|
|
|
|
2018-08-23 01:14:14 +02:00
|
|
|
raise CurrencyConversionError(
|
2018-10-18 12:42:45 +02:00
|
|
|
f'Unable to convert {amount} from {from_currency} to {to_currency}')
|
2016-07-28 11:30:13 +02:00
|
|
|
|
2019-10-29 06:26:25 +01:00
|
|
|
def to_dewies(self, currency, amount) -> int:
|
|
|
|
converted = self.convert_currency(currency, "LBC", amount)
|
|
|
|
return lbc_to_dewies(str(converted))
|
|
|
|
|
2016-07-28 11:30:13 +02:00
|
|
|
def fee_dict(self):
|
|
|
|
return {market: market.rate.as_dict() for market in self.market_feeds}
|