Move lookup rates into new Money library

This commit is contained in:
Mark Veidemanis 2022-02-22 20:06:56 +00:00
parent c15492f161
commit 74522342f7
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 50 additions and 25 deletions

View File

@ -311,26 +311,6 @@ class Agora(object):
to_return.append(to_append) to_return.append(to_append)
return to_return return to_return
# TODO: move to money library
def lookup_rates(self, ads, rates=None):
"""
Lookup the rates for a list of public ads.
"""
if not rates:
rates = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=self.markets.get_all_currencies())
# Set the price based on the asset
for ad in ads:
if ad[4] == "XMR":
coin = "monero"
elif ad[4] == "BTC":
coin = "bitcoin" # No s here
currency = ad[5]
base_currency_price = rates[coin][currency.lower()]
price = float(ad[2])
rate = round(price / base_currency_price, 2)
ad.append(rate)
return sorted(ads, key=lambda x: x[2])
def run_cheat_in_thread(self, assets=None): def run_cheat_in_thread(self, assets=None):
""" """
Update prices in another thread. Update prices in another thread.
@ -395,7 +375,7 @@ class Agora(object):
ads_list = self.enum_public_ads(asset, currency, providers) ads_list = self.enum_public_ads(asset, currency, providers)
if not ads_list: if not ads_list:
continue continue
ads = self.lookup_rates(ads_list, rates=rates) ads = self.money.lookup_rates(ads_list, rates=rates)
if not ads: if not ads:
continue continue
if currency in public_ads: if currency in public_ads:

View File

@ -18,6 +18,7 @@ from transactions import Transactions
from irc import bot from irc import bot
from notify import Notify from notify import Notify
from markets import Markets from markets import Markets
from money import Money
init_map = None init_map = None
@ -82,6 +83,7 @@ if __name__ == "__main__":
"revolut": Revolut(), "revolut": Revolut(),
"tx": Transactions(), "tx": Transactions(),
"webapp": WebApp(), "webapp": WebApp(),
"money": Money(),
} }
for classname, object_instance in init_map.items(): for classname, object_instance in init_map.items():
# notify, Notify # notify, Notify

39
handler/money.py Normal file
View File

@ -0,0 +1,39 @@
# Twisted/Klein imports
from twisted.logger import Logger
# Other library imports
from pycoingecko import CoinGeckoAPI
class Money(object):
"""
Generic class for handling money-related matters that aren't Revolut or Agora.
"""
def __init__(self):
"""
Initialise the Money object.
Set the logger.
Initialise the CoinGecko API.
"""
self.log = Logger("money")
self.cg = CoinGeckoAPI()
def lookup_rates(self, ads, rates=None):
"""
Lookup the rates for a list of public ads.
"""
if not rates:
rates = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=self.markets.get_all_currencies())
# Set the price based on the asset
for ad in ads:
if ad[4] == "XMR":
coin = "monero"
elif ad[4] == "BTC":
coin = "bitcoin" # No s here
currency = ad[5]
base_currency_price = rates[coin][currency.lower()]
price = float(ad[2])
rate = round(price / base_currency_price, 2)
ad.append(rate)
return sorted(ads, key=lambda x: x[2])

View File

@ -6,6 +6,7 @@ from copy import deepcopy
from tests.common import fake_public_ads, cg_prices, expected_to_update from tests.common import fake_public_ads, cg_prices, expected_to_update
from agora import Agora from agora import Agora
from markets import Markets from markets import Markets
from money import Money
import util import util
@ -22,7 +23,10 @@ class TestAgora(TestCase):
def setUp(self): def setUp(self):
self.markets = Markets() self.markets = Markets()
self.agora = Agora() self.agora = Agora()
self.money = Money()
setattr(self.agora, "markets", self.markets) setattr(self.agora, "markets", self.markets)
setattr(self.money, "markets", self.markets)
setattr(self.agora, "money", self.money)
self.all_providers = [ self.all_providers = [
"XOOM", "XOOM",
@ -177,8 +181,8 @@ class TestAgora(TestCase):
util.last_online_recent.return_value = True util.last_online_recent.return_value = True
# Override get_price # Override get_price
self.agora.cg.get_price = MagicMock() self.money.cg.get_price = MagicMock()
self.agora.cg.get_price.return_value = cg_prices self.money.cg.get_price.return_value = cg_prices
enum_ads_return = self.agora.enum_public_ads("XMR", "USD", self.all_providers) enum_ads_return = self.agora.enum_public_ads("XMR", "USD", self.all_providers)
@ -191,7 +195,7 @@ class TestAgora(TestCase):
ad.append(margin) ad.append(margin)
expected_return.append(ad) expected_return.append(ad)
lookup_rates_return = self.agora.lookup_rates(enum_ads_return) lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return) # TODO: do this properly
self.assertCountEqual(lookup_rates_return, expected_return) self.assertCountEqual(lookup_rates_return, expected_return)
def test_lookup_rates_not_usd(self): def test_lookup_rates_not_usd(self):
@ -219,5 +223,5 @@ class TestAgora(TestCase):
ad.append(margin) ad.append(margin)
expected_return.append(ad) expected_return.append(ad)
# Test specifying rates= # Test specifying rates=
lookup_rates_return = self.agora.lookup_rates(enum_ads_return, rates=cg_prices) lookup_rates_return = self.agora.money.lookup_rates(enum_ads_return, rates=cg_prices)
self.assertCountEqual(lookup_rates_return, expected_return) self.assertCountEqual(lookup_rates_return, expected_return)