Adjusts balance calculation for user perspective

Inverts the sign of Beancount balances to represent the user's perspective, where liabilities are positive and receivables are negative.

This change ensures that user balances accurately reflect the amount the castle owes the user (positive) or the amount the user owes the castle (negative). It simplifies the logic by consistently negating the Beancount balance rather than using conditional checks based on account type.
This commit is contained in:
padreug 2025-11-09 23:35:36 +01:00
parent 9350f05d74
commit 37fe34668f

View file

@ -220,15 +220,12 @@ class FavaClient:
for cost_str, amount in sats_positions.items():
amount_int = int(amount)
# Apply sign based on account type
if "Payable" in account_name:
# Liability: positive = castle owes user
total_sats += amount_int
account_balance["sats"] += amount_int
elif "Receivable" in account_name:
# Asset: positive = user owes castle (subtract from user balance)
total_sats -= amount_int
account_balance["sats"] -= amount_int
# For user balance perspective, negate Beancount balance
# - Payable (Liability): negative in Beancount → positive (castle owes user)
# - Receivable (Asset): positive in Beancount → negative (user owes castle)
adjusted_amount = -amount_int
total_sats += adjusted_amount
account_balance["sats"] += adjusted_amount
# Extract fiat amount from cost basis
# Format: "100.00 EUR" or "{100.00 EUR}"
@ -243,23 +240,22 @@ class FavaClient:
if fiat_currency not in fiat_balances:
fiat_balances[fiat_currency] = Decimal(0)
# Apply same sign logic
if "Payable" in account_name:
fiat_balances[fiat_currency] += fiat_amount
elif "Receivable" in account_name:
fiat_balances[fiat_currency] -= fiat_amount
# Apply same sign adjustment to fiat
# Cost basis is always positive, derive sign from amount
if amount_int < 0:
fiat_amount = -fiat_amount
adjusted_fiat = -fiat_amount
fiat_balances[fiat_currency] += adjusted_fiat
except (ValueError, IndexError):
logger.warning(f"Could not parse cost basis: {cost_str}")
elif isinstance(sats_positions, (int, float)):
# Simple number (no cost basis)
amount_int = int(sats_positions)
if "Payable" in account_name:
total_sats += amount_int
account_balance["sats"] += amount_int
elif "Receivable" in account_name:
total_sats -= amount_int
account_balance["sats"] -= amount_int
# Negate Beancount balance for user perspective
adjusted_amount = -amount_int
total_sats += adjusted_amount
account_balance["sats"] += adjusted_amount
accounts.append(account_balance)
@ -338,12 +334,10 @@ class FavaClient:
for cost_str, amount in sats_positions.items():
amount_int = int(amount)
if "Payable" in account_name:
user_data[user_id]["balance"] += amount_int
account_info["sats"] += amount_int
elif "Receivable" in account_name:
user_data[user_id]["balance"] -= amount_int
account_info["sats"] -= amount_int
# Negate Beancount balance for user perspective
adjusted_amount = -amount_int
user_data[user_id]["balance"] += adjusted_amount
account_info["sats"] += adjusted_amount
# Extract fiat
if cost_str and cost_str != "SATS":
@ -357,21 +351,20 @@ class FavaClient:
if fiat_currency not in user_data[user_id]["fiat_balances"]:
user_data[user_id]["fiat_balances"][fiat_currency] = Decimal(0)
if "Payable" in account_name:
user_data[user_id]["fiat_balances"][fiat_currency] += fiat_amount
elif "Receivable" in account_name:
user_data[user_id]["fiat_balances"][fiat_currency] -= fiat_amount
# Apply sign from amount to fiat
if amount_int < 0:
fiat_amount = -fiat_amount
adjusted_fiat = -fiat_amount
user_data[user_id]["fiat_balances"][fiat_currency] += adjusted_fiat
except (ValueError, IndexError):
pass
elif isinstance(sats_positions, (int, float)):
amount_int = int(sats_positions)
if "Payable" in account_name:
user_data[user_id]["balance"] += amount_int
account_info["sats"] += amount_int
elif "Receivable" in account_name:
user_data[user_id]["balance"] -= amount_int
account_info["sats"] -= amount_int
# Negate Beancount balance for user perspective
adjusted_amount = -amount_int
user_data[user_id]["balance"] += adjusted_amount
account_info["sats"] += adjusted_amount
user_data[user_id]["accounts"].append(account_info)