lbry-sdk/tests/unit/lbrynet_daemon/test_exchange_rate_manager.py

84 lines
3.1 KiB
Python
Raw Permalink Normal View History

2019-12-15 07:02:18 +01:00
import asyncio
2019-03-20 06:46:23 +01:00
from decimal import Decimal
2019-12-15 07:02:18 +01:00
from time import time
from lbry.schema.claim import Claim
2019-12-15 07:02:18 +01:00
from lbry.extras.daemon.exchange_rate_manager import (
ExchangeRate, ExchangeRateManager, CurrencyConversionError,
BittrexUSDFeed, BittrexBTCFeed,
CoinExBTCFeed
2019-12-15 07:02:18 +01:00
)
2019-12-31 21:30:13 +01:00
from lbry.testcase import AsyncioTestCase, FakeExchangeRateManager, get_fake_exchange_rate_manager
2019-11-20 02:34:39 +01:00
from lbry.error import InvalidExchangeRateResponseError
2019-01-22 23:54:25 +01:00
2019-12-15 07:02:18 +01:00
class ExchangeRateTests(AsyncioTestCase):
2017-05-31 20:15:15 +02:00
def test_invalid_rates(self):
with self.assertRaises(ValueError):
2019-12-15 07:02:18 +01:00
ExchangeRate('USDBTC', 0, time())
with self.assertRaises(ValueError):
2019-12-15 07:02:18 +01:00
ExchangeRate('USDBTC', -1, time())
2017-02-28 02:18:57 +01:00
def test_fee_converts_to_lbc(self):
2019-03-20 06:46:23 +01:00
fee = Claim().stream.fee
fee.usd = Decimal(10.0)
fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
manager = get_fake_exchange_rate_manager()
2017-05-30 23:07:23 +02:00
result = manager.convert_currency(fee.currency, "LBC", fee.amount)
2021-02-10 19:46:03 +01:00
self.assertEqual(20.0, result)
2017-02-28 02:18:57 +01:00
2017-05-31 20:15:15 +02:00
def test_missing_feed(self):
2019-03-20 06:46:23 +01:00
fee = Claim().stream.fee
fee.usd = Decimal(1.0)
fee.address = "bRcHraa8bYJZL7vkh5sNmGwPDERFUjGPP9"
2021-02-10 19:46:03 +01:00
manager = FakeExchangeRateManager([BittrexBTCFeed()], {'BTCLBC': 1.0})
2019-12-15 07:02:18 +01:00
with self.assertRaises(CurrencyConversionError):
2017-09-07 20:01:06 +02:00
manager.convert_currency(fee.currency, "LBC", fee.amount)
2017-05-31 20:15:15 +02:00
2019-12-15 07:02:18 +01:00
def test_bittrex_feed_response(self):
2021-02-10 19:46:03 +01:00
feed = BittrexBTCFeed()
2019-12-15 07:02:18 +01:00
out = feed.get_rate_from_response({
"symbol": "LBC-BTC",
"lastTradeRate": "0.00000323",
"bidRate": "0.00000322",
"askRate": "0.00000327"
2019-12-15 07:02:18 +01:00
})
self.assertEqual(1.0 / 0.00000323, out)
2019-11-20 02:34:39 +01:00
with self.assertRaises(InvalidExchangeRateResponseError):
2019-12-15 07:02:18 +01:00
feed.get_rate_from_response({})
2019-11-20 02:34:39 +01:00
with self.assertRaises(InvalidExchangeRateResponseError):
2019-12-15 07:02:18 +01:00
feed.get_rate_from_response({
"success": True,
"result": []
})
2019-10-14 11:17:18 +02:00
2021-02-10 19:46:03 +01:00
class BadMarketFeed(BittrexUSDFeed):
2019-10-14 11:17:18 +02:00
2019-12-15 07:02:18 +01:00
def get_response(self):
raise InvalidExchangeRateResponseError(self.name, 'bad stuff')
2019-10-14 11:17:18 +02:00
2019-12-15 07:02:18 +01:00
class ExchangeRateManagerTests(AsyncioTestCase):
2019-10-14 11:17:18 +02:00
2019-12-15 07:02:18 +01:00
async def test_get_rate_failure_retrieved(self):
manager = ExchangeRateManager([BadMarketFeed])
manager.start()
2021-05-07 19:38:52 +02:00
await manager.wait()
for feed in manager.market_feeds: # no rate but it tried
self.assertFalse(feed.has_rate)
self.assertTrue(feed.event.is_set())
2019-12-15 07:02:18 +01:00
self.addCleanup(manager.stop)
async def test_median_rate_used(self):
2021-05-07 19:33:38 +02:00
manager = ExchangeRateManager([BittrexBTCFeed, CoinExBTCFeed])
for feed in manager.market_feeds:
feed.last_check = time()
2021-05-07 19:33:38 +02:00
bittrex, coinex = manager.market_feeds
bittrex.rate = ExchangeRate(bittrex.market, 1.0, time())
2021-05-07 19:33:38 +02:00
coinex.rate = ExchangeRate(coinex.market, 2.0, time())
coinex.rate = ExchangeRate(coinex.market, 3.0, time())
self.assertEqual(14.0, manager.convert_currency("BTC", "LBC", Decimal(7.0)))
2021-05-07 19:33:38 +02:00
coinex.rate.spot = 4.0
self.assertEqual(17.5, manager.convert_currency("BTC", "LBC", Decimal(7.0)))