Merge pull request #16 from Routstr/codex/fix-failing-tests-in-pr-#15

Fix failing tests for price update error handling
This commit is contained in:
shroominic
2025-06-06 11:41:53 +02:00
committed by GitHub
3 changed files with 32 additions and 14 deletions
+1 -1
View File
@@ -68,5 +68,5 @@ async def update_sats_pricing() -> None:
ir = model.sats_pricing.internal_reasoning * 100
model.sats_pricing.max_cost = p + c + r + i + w + ir
except Exception as e:
print(e)
print('Error updating sats pricing: ', e)
await asyncio.sleep(10)
+28 -11
View File
@@ -1,35 +1,52 @@
import os
import httpx
import asyncio
import logging
# artifical spread to cover conversion fees
EXCHANGE_FEE = float(os.environ.get("EXCHANGE_FEE", "1.005")) # 0.5% default
async def kraken_btc_usd(client: httpx.AsyncClient) -> float:
async def kraken_btc_usd(client: httpx.AsyncClient) -> float | None:
api = "https://api.kraken.com/0/public/Ticker?pair=XBTUSD"
return float((await client.get(api)).json()["result"]["XXBTZUSD"]["c"][0])
try:
return float((await client.get(api)).json()["result"]["XXBTZUSD"]["c"][0])
except (httpx.RequestError, KeyError) as e:
logging.warning(f"Kraken API error: {e}")
return None
async def coinbase_btc_usd(client: httpx.AsyncClient) -> float:
async def coinbase_btc_usd(client: httpx.AsyncClient) -> float | None:
api = "https://api.coinbase.com/v2/prices/BTC-USD/spot"
return float((await client.get(api)).json()["data"]["amount"])
try:
return float((await client.get(api)).json()["data"]["amount"])
except (httpx.RequestError, KeyError) as e:
logging.warning(f"Coinbase API error: {e}")
return None
async def binance_btc_usdt(client: httpx.AsyncClient) -> float:
async def binance_btc_usdt(client: httpx.AsyncClient) -> float | None:
api = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"
return float((await client.get(api)).json()["price"])
try:
return float((await client.get(api)).json()["price"])
except (httpx.RequestError, KeyError) as e:
logging.warning(f"Binance API error: {e}")
return None
async def btc_usd_ask_price() -> float:
async with httpx.AsyncClient() as client:
return (
max(
await asyncio.gather(
kraken_btc_usd(client),
coinbase_btc_usd(client),
binance_btc_usdt(client),
)
[
price
for price in await asyncio.gather(
kraken_btc_usd(client),
coinbase_btc_usd(client),
binance_btc_usdt(client),
)
if price is not None
]
)
* EXCHANGE_FEE
)
+3 -2
View File
@@ -174,7 +174,8 @@ async def test_update_sats_pricing_handles_errors():
def mock_print(*args, **kwargs):
nonlocal error_printed
if args and isinstance(args[0], Exception) and str(args[0]) == "API Error":
message = " ".join(str(a) for a in args)
if "API Error" in message and "Error updating sats pricing" in message:
error_printed = True
original_print(*args, **kwargs)
@@ -218,4 +219,4 @@ def test_model_serialization(sample_model: Model):
# Test deserialization
new_model = Model(**model_dict)
assert new_model.id == sample_model.id
assert new_model.pricing.prompt == pytest.approx(sample_model.pricing.prompt)
assert new_model.pricing.prompt == pytest.approx(sample_model.pricing.prompt)