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

84 lines
3.1 KiB
Python
Raw Normal View History

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