Move rates and margin functions into Money library

This commit is contained in:
Mark Veidemanis 2022-02-22 20:10:47 +00:00
parent 74522342f7
commit 859c1d20ef
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 40 additions and 38 deletions

View File

@ -442,37 +442,6 @@ class Agora(object):
return_ids.append(rtrn["success"]) return_ids.append(rtrn["success"])
return all(return_ids) return all(return_ids)
# TODO: move to money
def get_rates_all(self):
"""
Get all rates that pair with USD.
:return: dictionary of USD/XXX rates
:rtype: dict
"""
rates = self.cr.get_rates("USD")
return rates
# TODO: move to money
def get_acceptable_margins(self, currency, amount):
"""
Get the minimum and maximum amounts we would accept a trade for.
:param currency: currency code
:param amount: amount
:return: (min, max)
:rtype: tuple
"""
rates = self.get_rates_all()
if currency == "USD":
min_amount = amount - float(settings.Agora.AcceptableUSDMargin)
max_amount = amount + float(settings.Agora.AcceptableUSDMargin)
return (min_amount, max_amount)
amount_usd = amount / rates[currency]
min_usd = amount_usd - float(settings.Agora.AcceptableUSDMargin)
max_usd = amount_usd + float(settings.Agora.AcceptableUSDMargin)
min_local = min_usd * rates[currency]
max_local = max_usd * rates[currency]
return (min_local, max_local)
@util.handle_exceptions @util.handle_exceptions
def create_ad(self, asset, countrycode, currency, provider, edit=False, ad_id=None): def create_ad(self, asset, countrycode, currency, provider, edit=False, ad_id=None):
""" """
@ -502,7 +471,7 @@ class Agora(object):
# Substitute the asset # Substitute the asset
ad = ad.replace("$ASSET$", asset) ad = ad.replace("$ASSET$", asset)
rates = self.get_rates_all() rates = self.money.get_rates_all()
if asset == "XMR": if asset == "XMR":
min_usd = float(settings.Agora.MinUSDXMR) min_usd = float(settings.Agora.MinUSDXMR)
max_usd = float(settings.Agora.MaxUSDXMR) max_usd = float(settings.Agora.MaxUSDXMR)
@ -721,5 +690,5 @@ class Agora(object):
if currency == "USD": if currency == "USD":
return float(amount) return float(amount)
else: else:
rates = self.get_rates_all() rates = self.money.get_rates_all()
return float(amount) / rates[currency] return float(amount) / rates[currency]

View File

@ -5,6 +5,10 @@ from twisted.logger import Logger
from pycoingecko import CoinGeckoAPI from pycoingecko import CoinGeckoAPI
# Project imports
from settings import settings
class Money(object): class Money(object):
""" """
Generic class for handling money-related matters that aren't Revolut or Agora. Generic class for handling money-related matters that aren't Revolut or Agora.
@ -37,3 +41,32 @@ class Money(object):
rate = round(price / base_currency_price, 2) rate = round(price / base_currency_price, 2)
ad.append(rate) ad.append(rate)
return sorted(ads, key=lambda x: x[2]) return sorted(ads, key=lambda x: x[2])
def get_rates_all(self):
"""
Get all rates that pair with USD.
:return: dictionary of USD/XXX rates
:rtype: dict
"""
rates = self.cr.get_rates("USD")
return rates
def get_acceptable_margins(self, currency, amount):
"""
Get the minimum and maximum amounts we would accept a trade for.
:param currency: currency code
:param amount: amount
:return: (min, max)
:rtype: tuple
"""
rates = self.get_rates_all()
if currency == "USD":
min_amount = amount - float(settings.Agora.AcceptableUSDMargin)
max_amount = amount + float(settings.Agora.AcceptableUSDMargin)
return (min_amount, max_amount)
amount_usd = amount / rates[currency]
min_usd = amount_usd - float(settings.Agora.AcceptableUSDMargin)
max_usd = amount_usd + float(settings.Agora.AcceptableUSDMargin)
min_local = min_usd * rates[currency]
max_local = max_usd * rates[currency]
return (min_local, max_local)

View File

@ -184,7 +184,7 @@ class Revolut(object):
return False return False
def get_total_usd(self): def get_total_usd(self):
rates = self.agora.get_rates_all() rates = self.money.get_rates_all()
accounts = self.accounts() accounts = self.accounts()
if not accounts: if not accounts:
return False return False

View File

@ -194,7 +194,7 @@ class Transactions(object):
if currency == "USD": if currency == "USD":
amount_usd = amount amount_usd = amount
else: else:
rates = self.agora.get_rates_all() rates = self.money.get_rates_all()
amount_usd = amount / rates[currency] amount_usd = amount / rates[currency]
# Amount is reliable here as it is checked by find_trade, so no need for stored_trade["amount"] # Amount is reliable here as it is checked by find_trade, so no need for stored_trade["amount"]
if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD): if float(amount_usd) > float(settings.Agora.AcceptableAltLookupUSD):
@ -230,7 +230,7 @@ class Transactions(object):
if looked_up_without_reference: if looked_up_without_reference:
return return
# If the amount does not match exactly, get the min and max values for our given acceptable margins for trades # If the amount does not match exactly, get the min and max values for our given acceptable margins for trades
min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount) min_amount, max_amount = self.money.get_acceptable_margins(currency, amount)
self.log.info( self.log.info(
"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}", "Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}",
min_amount=min_amount, min_amount=min_amount,
@ -510,7 +510,7 @@ class Transactions(object):
total_usd = total_usd_agora + total_usd_revolut total_usd = total_usd_agora + total_usd_revolut
# Convert the total USD price to GBP and SEK # Convert the total USD price to GBP and SEK
rates = self.agora.get_rates_all() rates = self.money.get_rates_all()
price_sek = rates["SEK"] * total_usd price_sek = rates["SEK"] * total_usd
price_usd = total_usd price_usd = total_usd
price_gbp = rates["GBP"] * total_usd price_gbp = rates["GBP"] * total_usd
@ -599,7 +599,7 @@ class Transactions(object):
if dash is False: if dash is False:
return False return False
rates = self.agora.get_rates_all() rates = self.money.get_rates_all()
cumul_usd = 0 cumul_usd = 0
for contact_id, contact in dash.items(): for contact_id, contact in dash.items():
# We need created at in order to look up the historical prices # We need created at in order to look up the historical prices