Move rates and margin functions into Money library

This commit is contained in:
Mark Veidemanis 2022-02-22 20:10:47 +00:00
parent 6e8903833c
commit f2dcb977b3
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 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
def create_ad(self, asset, countrycode, currency, provider, edit=False, ad_id=None):
"""
@ -502,7 +471,7 @@ class Agora(object):
# Substitute the asset
ad = ad.replace("$ASSET$", asset)
rates = self.get_rates_all()
rates = self.money.get_rates_all()
if asset == "XMR":
min_usd = float(settings.Agora.MinUSDXMR)
max_usd = float(settings.Agora.MaxUSDXMR)
@ -721,5 +690,5 @@ class Agora(object):
if currency == "USD":
return float(amount)
else:
rates = self.get_rates_all()
rates = self.money.get_rates_all()
return float(amount) / rates[currency]

View File

@ -5,6 +5,10 @@ from twisted.logger import Logger
from pycoingecko import CoinGeckoAPI
# Project imports
from settings import settings
class Money(object):
"""
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)
ad.append(rate)
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
def get_total_usd(self):
rates = self.agora.get_rates_all()
rates = self.money.get_rates_all()
accounts = self.accounts()
if not accounts:
return False

View File

@ -194,7 +194,7 @@ class Transactions(object):
if currency == "USD":
amount_usd = amount
else:
rates = self.agora.get_rates_all()
rates = self.money.get_rates_all()
amount_usd = amount / rates[currency]
# 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):
@ -230,7 +230,7 @@ class Transactions(object):
if looked_up_without_reference:
return
# 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(
"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}",
min_amount=min_amount,
@ -510,7 +510,7 @@ class Transactions(object):
total_usd = total_usd_agora + total_usd_revolut
# 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_usd = total_usd
price_gbp = rates["GBP"] * total_usd
@ -599,7 +599,7 @@ class Transactions(object):
if dash is False:
return False
rates = self.agora.get_rates_all()
rates = self.money.get_rates_all()
cumul_usd = 0
for contact_id, contact in dash.items():
# We need created at in order to look up the historical prices