Open-source personal finance manager. Self-hosted, privacy-first.
fix(assets): keep price precision when converting asset values to the primary currency (#1251)
Tássio Noronha4ff6d0e8d058
diff --git a/backend/app/api/assets.py b/backend/app/api/assets.pyindex 90b4e0d3..fca83045 100644--- a/backend/app/api/assets.py+++ b/backend/app/api/assets.py@@ -14,6 +14,7 @@ from app.core.workspace_context import (current_workspace,current_writable_workspace,)+from app.models.asset import ASSET_PRICE_DECIMALSfrom app.models.user import Userfrom app.providers.market_price import (MarketPriceRateLimitedError,@@ -161,11 +162,13 @@ async def refresh_asset_price(if refreshed.currency != primary_currency and refreshed.current_value is not None:converted, _ = await convert(session, Decimal(str(refreshed.current_value)), refreshed.currency, primary_currency,+ decimals=ASSET_PRICE_DECIMALS,)refreshed.current_value_primary = float(converted)if refreshed.gain_loss is not None:gl_converted, _ = await convert(session, Decimal(str(refreshed.gain_loss)), refreshed.currency, primary_currency,+ decimals=ASSET_PRICE_DECIMALS,)refreshed.gain_loss_primary = float(gl_converted)return refreshed@@ -183,11 +186,13 @@ async def list_assets(if asset.currency != primary_currency and asset.current_value is not None:converted, _ = await convert(session, Decimal(str(asset.current_value)), asset.currency, primary_currency,+ decimals=ASSET_PRICE_DECIMALS,)asset.current_value_primary = float(converted)if asset.gain_loss is not None:gl_converted, _ = await convert(session, Decimal(str(asset.gain_loss)), asset.currency, primary_currency,+ decimals=ASSET_PRICE_DECIMALS,)asset.gain_loss_primary = float(gl_converted)return assetsdiff --git a/backend/app/models/asset.py b/backend/app/models/asset.pyindex 3f2f203d..6bb0d1f9 100644--- a/backend/app/models/asset.py+++ b/backend/app/models/asset.py@@ -19,6 +19,12 @@ if TYPE_CHECKING:# Asset types a loan can be secured against (a mortgage, a car financing).LOAN_COLLATERAL_TYPES = frozenset({"real_estate", "vehicle"})+# Decimal precision of the asset price fields (last_price, average_price,+# Numeric(18,6)). FX conversions of asset values keep this many decimals so a+# sub-cent market price survives the conversion instead of rounding to 0.00+# (issue #622). Monetary amounts keep two.+ASSET_PRICE_DECIMALS = 6+class Asset(Base):__tablename__ = "assets"__table_args__ = (diff --git a/backend/app/services/asset_group_service.py b/backend/app/services/asset_group_service.pyindex fd2cad0b..bdcfbb08 100644--- a/backend/app/services/asset_group_service.py+++ b/backend/app/services/asset_group_service.py@@ -8,7 +8,7 @@ from sqlalchemy.exc import IntegrityErrorfrom sqlalchemy.ext.asyncio import AsyncSessionfrom app.core.config import get_settings-from app.models.asset import Asset+from app.models.asset import ASSET_PRICE_DECIMALS, Assetfrom app.models.asset_group import AssetGroupfrom app.models.asset_value import AssetValuefrom app.models.bank_connection import BankConnection@@ -107,7 +107,8 @@ async def _rollup(current_value_primary += latestelse:converted, _ = await convert(- session, latest, asset.currency, primary_currency+ session, latest, asset.currency, primary_currency,+ decimals=ASSET_PRICE_DECIMALS,)current_value_primary += convertedreturn asset_count, current_value, current_value_primarydiff --git a/backend/app/services/asset_service.py b/backend/app/services/asset_service.pyindex e7e5664d..3163cbd4 100644--- a/backend/app/services/asset_service.py+++ b/backend/app/services/asset_service.py@@ -11,7 +11,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom app.core.app_clock import app_todayfrom app.models.account import Account-from app.models.asset import LOAN_COLLATERAL_TYPES, Asset+from app.models.asset import ASSET_PRICE_DECIMALS, LOAN_COLLATERAL_TYPES, Assetfrom app.models.asset_transaction import AssetTransactionfrom app.models.asset_value import AssetValuefrom app.models.user import User@@ -947,7 +947,7 @@ async def get_portfolio_trend(for d in sorted_dates:row: dict[str, object] = {"date": d.isoformat()}- date_total = 0.0+ date_total = Decimal("0")for aid in [a["id"] for a in asset_meta]:if d in value_lookup[aid]:last_known[aid] = value_lookup[aid][d]@@ -966,15 +966,18 @@ async def get_portfolio_trend(currency = asset_currency[aid]if native != 0.0 and currency != primary_currency:converted, _ = await convert(- session, Decimal(str(native)), currency, primary_currency, d+ session, Decimal(str(native)), currency, primary_currency, d,+ decimals=ASSET_PRICE_DECIMALS,)- val = round(float(converted), 2)+ val = convertedelse:- val = round(native, 2)+ val = Decimal(str(native))- row[aid] = val+ # Each series point is shown in monetary decimals, but the total+ # sums the unrounded values so sub-cent holdings still add up.+ row[aid] = round(float(val), 2)date_total += val- row["_total"] = round(date_total, 2)+ row["_total"] = float(date_total.quantize(Decimal("0.01")))trend.append(row)# The header total matches the last row's _total — both use the same@@ -1042,7 +1045,8 @@ async def get_asset_values_at(if primary_currency is not None:converted, _ = await convert(- session, Decimal(str(amount)), asset.currency, primary_currency, as_of_date+ session, Decimal(str(amount)), asset.currency, primary_currency, as_of_date,+ decimals=ASSET_PRICE_DECIMALS,)primary_total += float(converted)diff --git a/backend/app/services/fx_rate_service.py b/backend/app/services/fx_rate_service.pyindex 3aa9d975..f21c1f1d 100644--- a/backend/app/services/fx_rate_service.py+++ b/backend/app/services/fx_rate_service.py@@ -205,12 +205,18 @@ async def convert(target_date: Optional[date] = None,*,allow_fetch: bool = True,+ decimals: int = 2,) -> tuple[Decimal, Decimal]:"""Convert an amount from one currency to another.Returns (converted_amount, rate_used). Uses the 1:1 fallback from:func:`get_rate` when no rate is available, so this is for live reads, notfor persisting a stored conversion.++ ``decimals`` controls the precision of the converted amount. Monetary+ amounts keep the two-decimal default; asset values pass the price-field+ precision (``ASSET_PRICE_DECIMALS``) so a sub-cent market price survives+ the conversion instead of rounding to 0.00 (issue #622)."""if from_currency == to_currency:return amount, Decimal("1")@@ -219,7 +225,7 @@ async def convert(session, from_currency, to_currency, target_date, allow_fetch=allow_fetch)converted = amount * rate- return converted.quantize(Decimal("0.01")), rate+ return converted.quantize(Decimal(1).scaleb(-decimals)), rateasync def stamp_primary_amount(diff --git a/backend/app/services/report_service.py b/backend/app/services/report_service.pyindex 5e608ebb..536c171d 100644--- a/backend/app/services/report_service.py+++ b/backend/app/services/report_service.py@@ -10,7 +10,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom app.core.app_clock import app_todayfrom app.core.config import get_settingsfrom app.models.account import Account-from app.models.asset import Asset+from app.models.asset import ASSET_PRICE_DECIMALS, Assetfrom app.models.asset_value import AssetValuefrom app.models.transaction import Transactionfrom app.models.category import Category@@ -123,7 +123,8 @@ async def _asset_value_at(amount = 0.0if amount > 0:converted, _ = await convert(- session, Decimal(str(amount)), asset.currency, primary_currency, cutoff+ session, Decimal(str(amount)), asset.currency, primary_currency, cutoff,+ decimals=ASSET_PRICE_DECIMALS,)total += float(converted)return total@@ -209,7 +210,9 @@ async def _net_worth_at(if filtered:asset_stmt = asset_stmt.where(Asset.group_id.in_(asset_group_ids or []))asset_result = await session.execute(asset_stmt)- assets_total = 0.0+ # Decimal until the one final rounding: sub-cent holdings must add up+ # exactly the way the wallet rollup does.+ assets_sum = Decimal("0")for asset in asset_result.scalars().all():val_result = await session.execute(select(AssetValue.amount)@@ -228,10 +231,11 @@ async def _net_worth_at(amount = 0.0if amount > 0:converted, _ = await convert(- session, Decimal(str(amount)), asset.currency, primary_currency, cutoff+ session, Decimal(str(amount)), asset.currency, primary_currency, cutoff,+ decimals=ASSET_PRICE_DECIMALS,)converted_val = round(float(converted), 2)- assets_total += converted_val+ assets_sum += convertedcomposition.append(ReportCompositionItem(key=str(asset.id),label=asset.name,@@ -240,6 +244,7 @@ async def _net_worth_at(group="assets",))+ assets_total = float(assets_sum.quantize(Decimal("0.01")))net_worth = accounts_total + assets_total - liabilities_totalreturn ReportDataPoint(diff --git a/backend/tests/test_asset_group_service.py b/backend/tests/test_asset_group_service.pyindex 0001a9e4..c1ed4749 100644--- a/backend/tests/test_asset_group_service.py+++ b/backend/tests/test_asset_group_service.py@@ -1,12 +1,16 @@import uuid-from datetime import datetime, timezone+from datetime import date, datetime, timezone+from decimal import Decimalimport pytestfrom sqlalchemy.ext.asyncio import AsyncSession+from app.models.asset import Assetfrom app.models.asset_group import AssetGroup+from app.models.asset_value import AssetValuefrom app.models.bank_connection import BankConnection-from app.services.asset_group_service import get_groups+from app.models.fx_rate import FxRate+from app.services.asset_group_service import get_group, get_groups@pytest.mark.asyncio@@ -54,3 +58,73 @@ async def test_get_groups_hides_empty_synced_wallets(assert "Manual Wallet" in namesassert "Connected Wallet" not in namesassert "MeuPluggy" not in names+++async def test_rollup_keeps_sub_cent_market_prices_in_primary_currency(+ session: AsyncSession, test_user, test_workspace+):+ """issue #622: a wallet holding low-priced foreign-currency assets must+ not roll up to 0.0 in the user's primary currency. Each converted amount+ keeps six decimals inside the sum; the read boundary still displays the+ total rounded to two. So 30 coins at 0.002087 USD x 0.92 (0.0576 EUR)+ must surface as 0.06, not the 0.0 the per-asset two-decimal quantization+ produced."""+ test_user.preferences = {**(test_user.preferences or {}), "currency_display": "EUR"}+ session.add(+ FxRate(+ base_currency="USD",+ quote_currency="EUR",+ date=date.today(),+ rate=Decimal("0.9200000000"),+ source="test",+ )+ )+ wallet = AssetGroup(+ id=uuid.uuid4(),+ user_id=test_user.id,+ workspace_id=test_workspace.id,+ name="Coin Wallet",+ )+ session.add(wallet)+ await session.commit()++ assets = [+ Asset(+ id=uuid.uuid4(),+ user_id=test_user.id,+ workspace_id=test_workspace.id,+ group_id=wallet.id,+ name=f"Coin {i}",+ type="investment",+ currency="USD",+ units=Decimal("1"),+ valuation_method="market_price",+ last_price=Decimal("0.002087"),+ ticker="COIN-USD",+ position=i,+ )+ for i in range(30)+ ]+ session.add_all(assets)+ await session.commit()+ # The rollup reads the latest AssetValue; the quote sync writes one per price.+ session.add_all(+ AssetValue(+ id=uuid.uuid4(),+ asset_id=asset.id,+ workspace_id=test_workspace.id,+ amount=Decimal("0.002087"),+ date=date.today(),+ source="sync",+ )+ for asset in assets+ )+ await session.commit()++ read = await get_group(session, wallet.id, test_workspace.id, test_user.id)++ assert read is not None+ assert read.asset_count == 30+ assert read.current_value == pytest.approx(0.06)+ assert read.current_value_primary == pytest.approx(0.06)diff --git a/backend/tests/test_asset_group_service_coverage.py b/backend/tests/test_asset_group_service_coverage.pyindex 4aa8a6a2..e7e459d1 100644--- a/backend/tests/test_asset_group_service_coverage.py+++ b/backend/tests/test_asset_group_service_coverage.py@@ -113,7 +113,7 @@ async def test_get_groups_converts_foreign_currency(session: AsyncSession, test_currency="USD", value_amount=Decimal("10"))await session.commit()- async def _fake_convert(session, amount, frm, to):+ async def _fake_convert(session, amount, frm, to, *args, **kwargs):return Decimal(amount) * Decimal("5"), Decimal("5")monkeypatch.setattr(svc, "convert", _fake_convert)diff --git a/backend/tests/test_asset_service.py b/backend/tests/test_asset_service.pyindex cd4632ca..d8d2ec11 100644--- a/backend/tests/test_asset_service.py+++ b/backend/tests/test_asset_service.py@@ -1021,3 +1021,38 @@ async def test_get_assets_excludes_archived_by_default(session: AsyncSession, tenames = [a.name for a in assets]assert "Visible" in namesassert "Archived" not in names+++async def test_portfolio_trend_total_keeps_sub_cent_foreign_assets(+ session: AsyncSession, test_user: User, test_workspace,+):+ """Low-priced foreign-currency holdings must add up in the trend total.++ Three coins at 0.001 USD convert to 0.005 BRL each. Rounding every+ holding to two decimals before summing lost them; summing first in+ Decimal gives 0.015, shown as 0.02. A float sum would round it to 0.01.+ """+ today = date.today()+ session.add(FxRate(+ id=uuid.uuid4(), base_currency="USD", quote_currency="BRL",+ date=today, rate=Decimal("5.0"), source="test",+ ))+ for i in range(3):+ coin = Asset(+ id=uuid.uuid4(), user_id=test_user.id, name=f"Coin {i}",+ type="investment", currency="USD", valuation_method="market_price",+ units=Decimal("1"), last_price=Decimal("0.001"), position=i,+ )+ session.add(coin)+ await session.flush()+ session.add(AssetValue(+ id=uuid.uuid4(), asset_id=coin.id,+ amount=Decimal("0.001"), date=today, source="sync",+ ))+ await session.commit()++ result = await get_portfolio_trend(session, test_workspace.id, test_user.id)++ assert result["trend"][-1]["_total"] == pytest.approx(0.02)+ assert result["total"] == pytest.approx(0.02)diff --git a/backend/tests/test_assets_api.py b/backend/tests/test_assets_api.pyindex e376a045..bedcbcf9 100644--- a/backend/tests/test_assets_api.py+++ b/backend/tests/test_assets_api.py@@ -10,6 +10,7 @@ from sqlalchemy.ext.asyncio import AsyncSessionfrom app.models.asset import Assetfrom app.models.asset_value import AssetValue+from app.models.fx_rate import FxRatefrom app.models.user import Userfrom app.models.workspace import Workspace, WorkspaceMemberfrom app.schemas.asset import MarketSymbolQuote@@ -309,3 +310,120 @@ async def test_get_value_trend(client: AsyncClient, auth_headers: dict, test_assasync def test_unauthenticated_access(client: AsyncClient):response = await client.get("/api/assets")assert response.status_code == 401+++class TestAssetValuePrecision:+ """FX conversion of asset values keeps price-field precision (issue #622).++ A low-priced market asset held by a user in another currency must not+ vanish from the primary-currency total: the converted value is rounded at+ the asset price precision (six decimals), not the monetary two.+ """++ @pytest_asyncio.fixture+ async def eur_rate(self, session: AsyncSession, clean_db) -> FxRate:+ """USD -> EUR 0.92, same value the fx_rates fixture uses."""+ fx = FxRate(+ base_currency="USD",+ quote_currency="EUR",+ date=date.today(),+ rate=Decimal("0.9200000000"),+ source="test",+ )+ session.add(fx)+ await session.commit()+ await session.refresh(fx)+ return fx++ @pytest_asyncio.fixture+ async def eur_user(+ self, session: AsyncSession, test_user: User+ ) -> User:+ """The test user reads the portfolio in EUR, not BRL."""+ test_user.preferences = {**(test_user.preferences or {}), "currency_display": "EUR"}+ await session.commit()+ return test_user++ @pytest.mark.asyncio+ async def test_list_keeps_sub_cent_value_in_primary_currency(+ self,+ client: AsyncClient,+ auth_headers: dict,+ session: AsyncSession,+ eur_user: User,+ test_workspace: Workspace,+ eur_rate: FxRate,+ ):+ # One unit of a low-priced coin, worth a fraction of a cent.+ asset = Asset(+ id=uuid.uuid4(),+ user_id=eur_user.id,+ workspace_id=test_workspace.id,+ name="Coin A",+ type="investment",+ currency="USD",+ units=Decimal("1"),+ valuation_method="market_price",+ purchase_price=Decimal("0.00"),+ last_price=Decimal("0.002087"),+ ticker="COIN-USD",+ position=0,+ )+ session.add(asset)+ await session.commit()++ headers = {**auth_headers, "X-Workspace-Id": str(test_workspace.id)}+ response = await client.get("/api/assets", headers=headers)++ assert response.status_code == 200+ entry = next(a for a in response.json() if a["id"] == str(asset.id))+ # 0.002087 USD * 0.92 = 0.001920 EUR at price precision. The+ # two-decimal quantization the issue reports returned 0.0 here.+ assert entry["current_value"] == pytest.approx(0.002087)+ assert entry["current_value_primary"] == pytest.approx(0.00192)+ assert entry["gain_loss"] == pytest.approx(0.002087)+ assert entry["gain_loss_primary"] == pytest.approx(0.00192)++ @pytest.mark.asyncio+ async def test_values_at_total_keeps_sub_cent_assets(+ self,+ session: AsyncSession,+ eur_user: User,+ test_workspace: Workspace,+ eur_rate: FxRate,+ ):+ """get_asset_values_at (the rollup behind the dashboard total and the+ trend chart) must not drop low-priced holdings from the primary+ total: each converted amount keeps six decimals, so the sum of+ sub-cent values is correct. 2 x 0.002087 USD = 0.004174, and+ 0.002087 x 0.92 = 0.001920 per coin. Before the fix each asset+ converted to 0.0 separately and the primary total was 0.0."""+ from app.services.asset_service import get_asset_values_at++ for i in range(2):+ session.add(+ Asset(+ id=uuid.uuid4(),+ user_id=eur_user.id,+ workspace_id=test_workspace.id,+ name=f"Coin {i}",+ type="investment",+ currency="USD",+ units=Decimal("1"),+ valuation_method="market_price",+ last_price=Decimal("0.002087"),+ ticker="COIN-USD",+ position=i,+ )+ )+ await session.commit()++ totals, primary_total = await get_asset_values_at(+ session,+ test_workspace.id,+ primary_currency="EUR",+ by_workspace=True,+ )++ assert totals["USD"] == pytest.approx(0.004174)+ assert primary_total == pytest.approx(0.00384)diff --git a/backend/tests/test_fx_rates.py b/backend/tests/test_fx_rates.pyindex ca3e7e31..36714a5f 100644--- a/backend/tests/test_fx_rates.py+++ b/backend/tests/test_fx_rates.py@@ -267,6 +267,22 @@ class TestConvert:# 100 * (5.0 / 0.92) = 543.478... → rounded to 543.48assert converted == Decimal("543.48")+ @pytest.mark.asyncio+ async def test_convert_price_precision_preserves_sub_cent_market_price(+ self, session: AsyncSession, fx_rates+ ):+ """The issue #622 case: a low market price (a sub-cent coin at+ 0.00224 USD) converted at six decimals keeps its value instead of+ rounding to 0.00."""+ from app.services.fx_rate_service import convert++ converted, rate = await convert(+ session, Decimal("0.002240"), "USD", "EUR", decimals=6+ )+ assert rate == Decimal("0.9200000000")+ # 0.00224 * 0.92 = 0.0020608 → 0.002061 at price precision+ assert converted == Decimal("0.002061")+class TestStampPrimaryAmount:"""Tests for fx_rate_service.stamp_primary_amount()."""diff --git a/backend/tests/test_report_service.py b/backend/tests/test_report_service.pyindex 2d4a4c9c..963a117c 100644--- a/backend/tests/test_report_service.py+++ b/backend/tests/test_report_service.py@@ -2962,3 +2962,42 @@ def test_custom_range_guard_follows_resolved_timezone():with pytest.raises(HTTPException) as exc:_resolve_custom_range(zone_today, host_today)assert exc.value.detail == "end_date must be on or before today"+++async def test_net_worth_keeps_sub_cent_foreign_assets(+ session: AsyncSession, test_user, test_workspace+):+ """Low-priced foreign-currency holdings must add up in the net worth.++ Three coins at 0.001 USD convert to 0.005 BRL each. A two-decimal+ conversion made each one 0.00 (or 0.01, depending on rounding); converting+ at price precision and summing in Decimal gives 0.015, shown as 0.02. A+ float sum would round 0.015 down to 0.01.+ """+ from app.models.fx_rate import FxRate++ today = date.today()+ session.add(FxRate(+ id=uuid.uuid4(), base_currency="USD", quote_currency="BRL",+ date=today, rate=Decimal("5.0"), source="test",+ ))+ for i in range(3):+ coin = Asset(+ id=uuid.uuid4(), user_id=test_user.id, name=f"Coin {i}",+ type="investment", currency="USD", valuation_method="market_price",+ units=Decimal("1"), last_price=Decimal("0.001"), position=i,+ )+ session.add(coin)+ await session.flush()+ session.add(AssetValue(+ id=uuid.uuid4(), asset_id=coin.id,+ amount=Decimal("0.001"), date=today, source="sync",+ ))+ await session.commit()++ total = await _asset_value_at(session, test_workspace.id, today, "BRL")+ assert total == pytest.approx(0.015)++ point = await _net_worth_at(session, test_workspace.id, today, "BRL")+ assert point.breakdowns["assets"] == pytest.approx(0.02)