From db22bb33bf93f9ea81c90d1cdceddae3864c2870 Mon Sep 17 00:00:00 2001 From: Routstr Dev <213178690+routstrdev@users.noreply.github.com> Date: Tue, 17 Jun 2025 21:18:45 +0530 Subject: [PATCH] Fixed the bug where balance was being credited twice. --- router/auth.py | 1 + router/cashu.py | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/router/auth.py b/router/auth.py index 67ee1041..e59051c3 100644 --- a/router/auth.py +++ b/router/auth.py @@ -70,6 +70,7 @@ async def validate_bearer_key( refund_address=refund_address, key_expiry_time=key_expiry_time, ) + session.add(new_key) msats = await credit_balance(bearer_key, new_key, session) if msats <= 0: raise Exception("Token redemption failed") diff --git a/router/cashu.py b/router/cashu.py index 35ab6be3..541ed0c8 100644 --- a/router/cashu.py +++ b/router/cashu.py @@ -90,10 +90,6 @@ async def credit_balance(cashu_token: str, key: ApiKey, session: AsyncSession) - return 0 amount_msats = amount_sats * 1000 - key.balance += amount_msats - - session.add(key) - await session.flush() # Apply the balance change atomically to avoid race conditions when topping # up the same key concurrently. @@ -104,6 +100,7 @@ async def credit_balance(cashu_token: str, key: ApiKey, session: AsyncSession) - ) await session.exec(stmt) # type: ignore[call-overload] await session.commit() + await session.refresh(key) return amount_msats