Libraries refactor and add some sinks #4

Closed
m wants to merge 136 commits from library-refactor into master
3 changed files with 170 additions and 0 deletions
Showing only changes of commit a8fdc49281 - Show all commits

View File

@ -411,6 +411,44 @@ class Agora(object):
else: else:
deferToThread(self.update_prices, xmr, btc) deferToThread(self.update_prices, xmr, btc)
@handle_exceptions
def get_all_public_ads(self):
"""
Get all public ads for our listed currencies.
:return: dict of public ads keyed by currency
:rtype: dict
"""
public_ads = {}
crypto_map = {
"XMR": "monero",
"BTC": "bitcoin",
}
# Get all currencies we have ads for, deduplicated
currencies = list(set([x[0] for x in loads(settings.Agora.DistList)]))
providers = loads(settings.Agora.ProviderList)
# We want to get the ads for each of these currencies and return the result
for asset in loads(settings.Agora.AssetList):
rates_crypto = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies)
for currency in currencies:
cg_asset_name = crypto_map[asset]
try:
rates_current_crypto = rates_crypto[cg_asset_name][currency.lower()]
except KeyError:
self.log.error("Error getting public ads for currency {currency}", currency=currency)
continue
ads = self.wrap_public_ads(asset, currency, providers=providers, rates=rates_current_crypto)
if not ads:
continue
if currency in public_ads:
for ad in list(ads): # Copy the list so we don't mutate and get stuck in an infinite loop
public_ads[currency].append(ad)
else:
public_ads[currency] = ads
return public_ads
@handle_exceptions @handle_exceptions
def update_prices(self, xmr=True, btc=True): def update_prices(self, xmr=True, btc=True):
if xmr and btc: if xmr and btc:

109
handler/tests/common.py Normal file
View File

@ -0,0 +1,109 @@
fake_public_ads = {
"NOK": [
["9c3d9fb6-c74c-4a35-bd9f-b6c74c7a3504", "topmonero", "2023.02", "REVOLUT", 1.3],
["9c3d9fb6-c74c-4a35-bd9f-b6c74c7a3504", "topmonero", "2023.02", "REVOLUT", 1.3],
],
"EUR": [
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["87af6467-be02-476e-af64-67be02676e9a", "topmonero", "184.28", "REVOLUT", 1.19],
["d2c6645c-6d56-4094-8664-5c6d5640941b", "topmonero", "184.28", "REVOLUT", 1.19],
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["87af6467-be02-476e-af64-67be02676e9a", "topmonero", "184.28", "REVOLUT", 1.19],
["d2c6645c-6d56-4094-8664-5c6d5640941b", "topmonero", "184.28", "REVOLUT", 1.19],
],
"GBP": [
["15e821b8-e570-4b0f-a821-b8e5709b0ffc", "SecureMole", "166.08", "REVOLUT", 1.27],
["071ab272-ba37-4a14-9ab2-72ba37fa1484", "Boozymad89", "166.08", "REVOLUT", 1.27],
["850f28eb-ce63-4ca7-8f28-ebce63dca707", "topmonero", "166.34", "REVOLUT", 1.27],
["6727d9e5-c038-43f5-a7d9-e5c038c3f5be", "topmonero", "166.34", "REVOLUT", 1.27],
["ca4feeb9-22d5-456d-8fee-b922d5c56d27", "Boozymad89", "175.51", "REVOLUT", 1.34],
["15e821b8-e570-4b0f-a821-b8e5709b0ffc", "SecureMole", "166.08", "REVOLUT", 1.27],
["071ab272-ba37-4a14-9ab2-72ba37fa1484", "Boozymad89", "166.08", "REVOLUT", 1.27],
["850f28eb-ce63-4ca7-8f28-ebce63dca707", "topmonero", "166.34", "REVOLUT", 1.27],
["6727d9e5-c038-43f5-a7d9-e5c038c3f5be", "topmonero", "166.34", "REVOLUT", 1.27],
["ca4feeb9-22d5-456d-8fee-b922d5c56d27", "Boozymad89", "175.51", "REVOLUT", 1.34],
],
"SEK": [
["f0e840b9-29ab-4a6f-a840-b929ab7a6fde", "topmonero", "2020.45", "REVOLUT", 1.25],
["2252a3f7-6d6b-400b-92a3-f76d6bb00b50", "SwishaMonero", "2020.45", "REVOLUT", 1.25],
["f0e840b9-29ab-4a6f-a840-b929ab7a6fde", "topmonero", "2020.45", "REVOLUT", 1.25],
["2252a3f7-6d6b-400b-92a3-f76d6bb00b50", "SwishaMonero", "2020.45", "REVOLUT", 1.25],
],
"CZK": [
["80aa52ef-a5d3-462c-aa52-efa5d3862cbe", "topmonero", "4872.73", "REVOLUT", 1.3],
["80aa52ef-a5d3-462c-aa52-efa5d3862cbe", "topmonero", "4872.73", "REVOLUT", 1.3],
],
}
currency_map = {
"CZK": [["80aa52ef-a5d3-462c-aa52-efa5d3862cbe", "topmonero", "4872.73", "REVOLUT", 1.3]],
"GBP": [
["15e821b8-e570-4b0f-a821-b8e5709b0ffc", "SecureMole", "166.08", "REVOLUT", 1.27],
["071ab272-ba37-4a14-9ab2-72ba37fa1484", "Boozymad89", "166.08", "REVOLUT", 1.27],
["850f28eb-ce63-4ca7-8f28-ebce63dca707", "topmonero", "166.34", "REVOLUT", 1.27],
["6727d9e5-c038-43f5-a7d9-e5c038c3f5be", "topmonero", "166.34", "REVOLUT", 1.27],
["ca4feeb9-22d5-456d-8fee-b922d5c56d27", "Boozymad89", "175.51", "REVOLUT", 1.34],
],
"NOK": [["9c3d9fb6-c74c-4a35-bd9f-b6c74c7a3504", "topmonero", "2023.02", "REVOLUT", 1.3]],
"SEK": [
["f0e840b9-29ab-4a6f-a840-b929ab7a6fde", "topmonero", "2020.45", "REVOLUT", 1.25],
["2252a3f7-6d6b-400b-92a3-f76d6bb00b50", "SwishaMonero", "2020.45", "REVOLUT", 1.25],
],
"EUR": [
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["65b452e3-a29f-4233-b452-e3a29fe23369", "topmonero", "184.28", "REVOLUT", 1.19],
["87af6467-be02-476e-af64-67be02676e9a", "topmonero", "184.28", "REVOLUT", 1.19],
["d2c6645c-6d56-4094-8664-5c6d5640941b", "topmonero", "184.28", "REVOLUT", 1.19],
],
}
cg_prices = {
"bitcoin": {
"eur": 38164,
"czk": 924828,
"gbp": 32252,
"cad": 55384,
"dkk": 284076,
"nzd": 65925,
"usd": 43640,
"nok": 384190,
"pln": 173208,
"zar": 678716,
"huf": 13509146,
"sgd": 58674,
"rub": 3301769,
"jpy": 5021747,
"thb": 1439257,
"chf": 40315,
"aud": 61394,
"try": 593510,
"hkd": 340058,
"mxn": 902166,
"sek": 399011,
},
"monero": {
"eur": 154.97,
"czk": 3755.44,
"gbp": 130.97,
"cad": 224.9,
"dkk": 1153.54,
"nzd": 267.7,
"usd": 177.21,
"nok": 1560.08,
"pln": 703.34,
"zar": 2756.05,
"huf": 54856,
"sgd": 238.26,
"rub": 13407.45,
"jpy": 20392,
"thb": 5844.37,
"chf": 163.71,
"aud": 249.3,
"try": 2410.06,
"hkd": 1380.87,
"mxn": 3663.41,
"sek": 1620.26,
},
}

View File

@ -0,0 +1,23 @@
from unittest import TestCase
from unittest.mock import MagicMock
from tests.common import fake_public_ads, currency_map, cg_prices
from agora import Agora
class TestAgora(TestCase):
def setUp(self):
self.agora = Agora()
def mock_wrap_public_ads(self, asset, currency, providers, rates):
try:
return currency_map[currency]
except KeyError:
return
def test_get_all_public_ads(self):
self.agora.cg.get_price = MagicMock()
self.agora.cg.get_price.return_value = cg_prices
self.agora.wrap_public_ads = self.mock_wrap_public_ads
public_ads = self.agora.get_all_public_ads()
self.assertDictEqual(public_ads, fake_public_ads)