swap Money for MoneyAmount, fix a couple of things along the way

This commit is contained in:
Pablo Martin 2024-06-27 17:18:43 +02:00
parent ab2ac1ec6a
commit 0947b34ebf
6 changed files with 26 additions and 27 deletions

View file

@ -1,6 +1,5 @@
import pathlib
from dataclasses import dataclass
from decimal import Decimal
from money.currency import Currency
@ -8,8 +7,6 @@ from xexe.rate_fetching import MockRateFetcher, XERateFetcher
DEFAULT_CURRENCIES = {Currency("EUR"), Currency("GBP"), Currency("USD")}
DEFAULT_MAX_DECIMALS = Decimal("0.00000001")
RATES_SOURCES = {"mock": MockRateFetcher, "xe": XERateFetcher}
DWH_SCHEMA = "sync_xedotcom_currency_rates"

View file

@ -3,8 +3,9 @@ from decimal import Decimal
from numbers import Number
from typing import Iterable, Set, Union
from money.currency import Currency, CurrencyHelper
from money.money import Money
from money.currency import Currency
from xexe.money_amount import DEFAULT_MONEY_PRECISION_POSITIONS, MoneyAmount
class ExchangeRate:
@ -13,13 +14,13 @@ class ExchangeRate:
self,
from_currency: Currency,
to_currency: Currency,
rate: Union[Money, Number, str],
rate: Union[MoneyAmount, Number, str],
rate_date: datetime.date,
) -> None:
self.from_currency = from_currency
self.to_currency = to_currency
if not isinstance(rate, Money):
rate = Money(rate, to_currency)
if not isinstance(rate, MoneyAmount):
rate = MoneyAmount(amount=rate, currency=to_currency)
self.rate = rate
self.rate_date = rate_date
@ -100,7 +101,7 @@ def add_equal_rates(rates: ExchangeRates, overwrite: bool = False) -> ExchangeRa
new_rate = ExchangeRate(
from_currency=currency,
to_currency=currency,
rate=Money(1, currency),
rate=MoneyAmount(1, currency),
rate_date=date,
)
if new_rate in rates and not overwrite:
@ -122,7 +123,7 @@ def add_inverse_rates(rates: ExchangeRates) -> ExchangeRates:
from_currency=rate.to_currency,
to_currency=rate.from_currency,
rate_date=rate.rate_date,
rate=f"{1 / rate.amount:.{CurrencyHelper.decimal_precision_for_currency(rate.from_currency)}f}",
rate=f"{1 / rate.amount:.{DEFAULT_MONEY_PRECISION_POSITIONS}f}",
)
rates.add_rate(inverse_rate)

View file

@ -3,9 +3,12 @@ from typing import Union
from money.currency import Currency
from xexe.constants import DEFAULT_MAX_DECIMALS
DEFAULT_MONEY_PRECISION = DEFAULT_MAX_DECIMALS
DEFAULT_MONEY_PRECISION_POSITIONS = 8
DEFAULT_MONEY_PRECISION = Decimal(
"0." + ("0" * (DEFAULT_MONEY_PRECISION_POSITIONS - 1)) + "1"
)
# If we have X decimal positions, we want 7 zeros and 1 one
# i.e. 0. 000 000 01
class MoneyAmount:

View file

@ -1,13 +1,12 @@
import datetime
import os
from abc import ABC, abstractmethod
from decimal import Decimal
from money.currency import Currency, CurrencyHelper
from money.money import Money
from money.currency import Currency
from xecd_rates_client import XecdClient
from xexe.exchange_rates import ExchangeRate
from xexe.money_amount import DEFAULT_MONEY_PRECISION_POSITIONS, MoneyAmount
class RateFetcher(ABC):
@ -35,7 +34,7 @@ class MockRateFetcher(RateFetcher):
return ExchangeRate(
from_currency=from_currency,
to_currency=to_currency,
rate=Money(42, to_currency),
rate=MoneyAmount(42, to_currency),
rate_date=rate_date,
)
@ -71,7 +70,7 @@ class XERateFetcher(RateFetcher):
"Z", "+00:00"
) # Funny replace is necessary because of API response format
).date()
rate = f"""{response["to"][0]["mid"]:.{CurrencyHelper.decimal_precision_for_currency(to_currency)}f}"""
rate = f"""{response["to"][0]["mid"]:.{DEFAULT_MONEY_PRECISION_POSITIONS}f}"""
return ExchangeRate(
from_currency=from_currency,