mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-10-05 20:28:23 +00:00
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:
+1
-1
@@ -68,5 +68,5 @@ async def update_sats_pricing() -> None:
|
|||||||
ir = model.sats_pricing.internal_reasoning * 100
|
ir = model.sats_pricing.internal_reasoning * 100
|
||||||
model.sats_pricing.max_cost = p + c + r + i + w + ir
|
model.sats_pricing.max_cost = p + c + r + i + w + ir
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print('Error updating sats pricing: ', e)
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
|
|||||||
+28
-11
@@ -1,35 +1,52 @@
|
|||||||
import os
|
import os
|
||||||
import httpx
|
import httpx
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
# artifical spread to cover conversion fees
|
# artifical spread to cover conversion fees
|
||||||
EXCHANGE_FEE = float(os.environ.get("EXCHANGE_FEE", "1.005")) # 0.5% default
|
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"
|
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"
|
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"
|
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 def btc_usd_ask_price() -> float:
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
return (
|
return (
|
||||||
max(
|
max(
|
||||||
await asyncio.gather(
|
[
|
||||||
kraken_btc_usd(client),
|
price
|
||||||
coinbase_btc_usd(client),
|
for price in await asyncio.gather(
|
||||||
binance_btc_usdt(client),
|
kraken_btc_usd(client),
|
||||||
)
|
coinbase_btc_usd(client),
|
||||||
|
binance_btc_usdt(client),
|
||||||
|
)
|
||||||
|
if price is not None
|
||||||
|
]
|
||||||
)
|
)
|
||||||
* EXCHANGE_FEE
|
* EXCHANGE_FEE
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -174,7 +174,8 @@ async def test_update_sats_pricing_handles_errors():
|
|||||||
|
|
||||||
def mock_print(*args, **kwargs):
|
def mock_print(*args, **kwargs):
|
||||||
nonlocal error_printed
|
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
|
error_printed = True
|
||||||
original_print(*args, **kwargs)
|
original_print(*args, **kwargs)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user