From 0d6e95bbabf2dff62dfc2bdd9b7d7b2dff6b52e0 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Wed, 19 Nov 2025 21:13:26 +0100 Subject: [PATCH] fix: Read TOTAL_WITHDRAWN from file instead of cached process.env Bug: Second withdrawal showed $5.92 cumulative instead of $12.50 Root cause: process.env caches values at container startup, doesn't update when .env file changes. Fix: Parse TOTAL_WITHDRAWN directly from .env file content instead of using cached process.env value. Added: Debug logging showing calculation Manual fix: Updated .env to $12.50 (sum of $6.58 + $5.92) Result: Withdrawal stats now correctly show cumulative total. --- .env | 2 +- app/api/withdrawals/execute/route.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 7c024b8..f3ecf27 100644 --- a/.env +++ b/.env @@ -419,4 +419,4 @@ WITHDRAWAL_PROFIT_PERCENT=10 MIN_WITHDRAWAL_AMOUNT=5 MIN_ACCOUNT_BALANCE=500 LAST_WITHDRAWAL_TIME=2025-11-19T19:34:47.185Z -TOTAL_WITHDRAWN=6.58 \ No newline at end of file +TOTAL_WITHDRAWN=12.50 \ No newline at end of file diff --git a/app/api/withdrawals/execute/route.ts b/app/api/withdrawals/execute/route.ts index 00c2afd..c6cc9d1 100644 --- a/app/api/withdrawals/execute/route.ts +++ b/app/api/withdrawals/execute/route.ts @@ -42,11 +42,16 @@ export async function POST(request: NextRequest) { } // Update LAST_WITHDRAWAL_TIME and TOTAL_WITHDRAWN in .env - const currentTotal = parseFloat(process.env.TOTAL_WITHDRAWN || '0') + // IMPORTANT: Read from file, not process.env (which is cached at startup) + let envContent = fs.readFileSync(ENV_PATH, 'utf-8') + + // Parse current total from file, not from cached process.env + const totalMatch = envContent.match(/^TOTAL_WITHDRAWN=(.*)$/m) + const currentTotal = totalMatch ? parseFloat(totalMatch[1]) : 0 const newTotal = currentTotal + calculation.withdrawalAmount const now = new Date().toISOString() - let envContent = fs.readFileSync(ENV_PATH, 'utf-8') + console.log(`💾 Updating withdrawal tracking: $${currentTotal.toFixed(2)} + $${calculation.withdrawalAmount.toFixed(2)} = $${newTotal.toFixed(2)}`) // Update LAST_WITHDRAWAL_TIME const timeRegex = /^LAST_WITHDRAWAL_TIME=.*$/m