def create_order( user_id: int, direction: Direction, amount: int, base_currency: Currencies, quote_currency: Currencies, ) -> Order: expiration_time = datetime.utcnow() + timedelta(minutes=10) conversion_rate = get_rate( base_currency=Currency(base_currency), quote_currency=Currency(quote_currency), ) request_amount = Amount().deserialize(amount) exchange_amount = request_amount * conversion_rate order_type = OrderType.Trade if CurrencyPair.is_diem_to_diem( CurrencyPair(Currency(base_currency), Currency(quote_currency))): order_type = OrderType.DirectConvert return storage.create_order( user_id=user_id, amount=request_amount.serialize(), direction=direction, base_currency=base_currency.value, quote_currency=quote_currency.value, expiration_time=expiration_time, exchange_amount=exchange_amount.serialize(), order_type=order_type.value, )
def _get_rates() -> Dict[str, Amount]: rates = {} for base_currency in DiemCurrency.__members__: for fiat_currency in FiatCurrency.__members__: try: rate = get_rate( base_currency=Currency(base_currency), quote_currency=Currency(fiat_currency), ) rates[f"{base_currency}_{fiat_currency}"] = rate except LookupError: pass return rates
def test_get_rate_direct_conversion_odd_currencies(): rate = get_rate(Currency.EUR, Currency.Coin1).serialize() assert rate == 1080000
def test_get_rate_non_exist_conversion(): with pytest.raises(LookupError): get_rate(Currency.CHF, Currency.NZD).serialize()
def test_get_rate_direct_conversion_matching_currencies(): rate = get_rate(Currency.Coin1, Currency.USD).serialize() assert rate == 1000000