From f2b73f56009c746555d79bbed443a1584fc17ae4 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Fri, 22 Aug 2025 15:18:46 -0300 Subject: [PATCH] fix reserved_balance/total_balance logic --- routstr/auth.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routstr/auth.py b/routstr/auth.py index bb51959f..b1f44080 100644 --- a/routstr/auth.py +++ b/routstr/auth.py @@ -288,14 +288,15 @@ async def pay_for_request(key: ApiKey, session: AsyncSession, body: dict) -> int }, ) - if key.balance < cost_per_request: + if key.total_balance < cost_per_request: logger.warning( "Insufficient balance for request", extra={ "key_hash": key.hashed_key[:8] + "...", "balance": key.balance, + "reserved_balance": key.reserved_balance, "required": cost_per_request, - "shortfall": cost_per_request - key.balance, + "shortfall": cost_per_request - key.total_balance, "model": model, }, ) @@ -379,8 +380,7 @@ async def revert_pay_for_request( update(ApiKey) .where(col(ApiKey.hashed_key) == key.hashed_key) .values( - balance=col(ApiKey.balance) + cost_per_request, - total_spent=col(ApiKey.total_spent) - cost_per_request, + reserved_balance=col(ApiKey.reserved_balance) - cost_per_request, total_requests=col(ApiKey.total_requests) - 1, ) )