Step 5: Update exchange price endpoint to use new pricing config

- Update ExchangeConfigResponse schema with direction-specific fields
- Remove premium_percentage from PriceResponse (now in config)
- Update price endpoint to load pricing config from database
- Update frontend to use direction-specific min/max and calculate premium
- Update tests to seed pricing config
- Add logic to clamp amount when direction changes
This commit is contained in:
counterweight 2025-12-26 20:20:23 +01:00
parent d838d1be96
commit d317939ad0
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
5 changed files with 145 additions and 44 deletions

View file

@ -20,6 +20,7 @@ from models import (
from price_fetcher import PAIR_BTC_EUR, SOURCE_BITFINEX, fetch_btc_eur_price
from repositories.exchange import ExchangeRepository
from repositories.price import PriceRepository
from repositories.pricing import PricingRepository
from schemas import (
AdminExchangeResponse,
AvailableSlotsResponse,
@ -31,12 +32,7 @@ from schemas import (
UserSearchResult,
)
from services.exchange import ExchangeService
from shared_constants import (
EUR_TRADE_INCREMENT,
EUR_TRADE_MAX,
EUR_TRADE_MIN,
PREMIUM_PERCENTAGE,
)
from shared_constants import EUR_TRADE_INCREMENT
from utils.enum_validation import validate_enum
router = APIRouter(prefix="/api/exchange", tags=["exchange"])
@ -65,20 +61,33 @@ async def get_exchange_price(
The response includes:
- market_price: The raw price from the exchange
- premium_percentage: The premium to apply to trades
- is_stale: Whether the price is older than 5 minutes
- config: Trading configuration (min/max EUR, increment)
- config: Trading configuration (min/max EUR per direction, premiums, increment)
"""
config = ExchangeConfigResponse(
eur_min=EUR_TRADE_MIN,
eur_max=EUR_TRADE_MAX,
eur_increment=EUR_TRADE_INCREMENT,
premium_percentage=PREMIUM_PERCENTAGE,
)
price_repo = PriceRepository(db)
pricing_repo = PricingRepository(db)
service = ExchangeService(db)
# Load pricing config from database
pricing_config = await pricing_repo.get_current()
if pricing_config is None:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Pricing configuration not available",
)
config = ExchangeConfigResponse(
eur_min_buy=pricing_config.eur_min_buy,
eur_max_buy=pricing_config.eur_max_buy,
eur_min_sell=pricing_config.eur_min_sell,
eur_max_sell=pricing_config.eur_max_sell,
eur_increment=EUR_TRADE_INCREMENT,
premium_buy=pricing_config.premium_buy,
premium_sell=pricing_config.premium_sell,
small_trade_threshold_eur=pricing_config.small_trade_threshold_eur,
small_trade_extra_premium=pricing_config.small_trade_extra_premium,
)
# Try to get the latest cached price
cached_price = await price_repo.get_latest()
@ -101,7 +110,6 @@ async def get_exchange_price(
return ExchangePriceResponse(
price=PriceResponse(
market_price=price_value,
premium_percentage=PREMIUM_PERCENTAGE,
timestamp=timestamp,
is_stale=False,
),
@ -113,7 +121,6 @@ async def get_exchange_price(
return ExchangePriceResponse(
price=PriceResponse(
market_price=cached_price.price,
premium_percentage=PREMIUM_PERCENTAGE,
timestamp=cached_price.timestamp,
is_stale=True,
),
@ -131,7 +138,6 @@ async def get_exchange_price(
return ExchangePriceResponse(
price=PriceResponse(
market_price=cached_price.price,
premium_percentage=PREMIUM_PERCENTAGE,
timestamp=cached_price.timestamp,
is_stale=service.is_price_stale(cached_price.timestamp),
),