mirror of
https://github.com/Zproger/Aurum.git
synced 2026-10-13 02:56:39 +07:00
feat: full CoinMarketCap-style crypto portfolio (buy/sell log, 1h/24h/7d%, P&L)
Replaces the plain quantity field with a CryptoTransaction log (same
"record events, derive the total" shape as Goal/GoalContribution): quantity
and average buy price are now computed via weighted-average cost — a buy
blends into the running average, a sell reduces quantity but leaves the
average cost of what's still held unchanged.
Switches price fetching from /simple/price to CoinGecko's /coins/markets
(still one batched call for every coin), which also returns 1h/24h/7d %
change in the same request. Old PATCH /crypto/holdings/{id} is replaced by
POST .../transactions (buy/sell, rejects selling more than held), GET
.../transactions (history), and DELETE /crypto/transactions/{id}.
Frontend: CryptoHoldingsTable replaces the simple list with a real table
(Name/Price/1h%/24h%/7d%/Holdings/Avg Buy Price/Profit-Loss/Actions),
horizontally scrollable on mobile rather than dropping columns. New
CryptoTransactionModal (buy/sell) and CryptoTransactionHistoryModal.
Backend: 79/79 tests passing (13 in test_crypto.py, up from 9). Also
verified live end-to-end against the real CoinGecko API with the user's own
key — create, buy, sell, oversell rejection, history, manual refresh, all
against real BTC/ETH prices in EUR — test data cleaned up after.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01855PiW5Wd8n4aRW4n9kByY
This commit is contained in:
@@ -1,39 +1,74 @@
|
||||
from datetime import date as date_
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from app.models.enums import CryptoTransactionType
|
||||
|
||||
|
||||
class CryptoTransactionCreate(BaseModel):
|
||||
type: CryptoTransactionType
|
||||
quantity: Decimal = Field(gt=0, max_digits=38, decimal_places=18)
|
||||
# Price paid (buy) / received (sell) per unit, in the app's display
|
||||
# currency — not fetched from CoinGecko, this is what the user actually
|
||||
# paid, which the market price today has nothing to do with.
|
||||
price_per_unit: Decimal = Field(gt=0, max_digits=38, decimal_places=18)
|
||||
date: date_
|
||||
note: str | None = Field(default=None, max_length=500)
|
||||
|
||||
|
||||
class CryptoTransactionRead(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
asset_id: int
|
||||
type: CryptoTransactionType
|
||||
quantity: Decimal
|
||||
price_per_unit: Decimal
|
||||
date: date_
|
||||
note: str | None
|
||||
|
||||
|
||||
class CryptoHoldingCreate(BaseModel):
|
||||
coingecko_id: str = Field(min_length=1, max_length=100)
|
||||
symbol: str = Field(min_length=1, max_length=20)
|
||||
name: str = Field(min_length=1, max_length=150)
|
||||
thumb_url: str | None = Field(default=None, max_length=500)
|
||||
# 18 decimal places covers wei-level token amounts; still gt=0 since a
|
||||
# zero/negative holding isn't a holding.
|
||||
quantity: Decimal = Field(gt=0, max_digits=38, decimal_places=18)
|
||||
|
||||
|
||||
class CryptoHoldingUpdate(BaseModel):
|
||||
# A holding always starts with a buy — there's nothing to "start
|
||||
# tracking" with a sell.
|
||||
quantity: Decimal = Field(gt=0, max_digits=38, decimal_places=18)
|
||||
price_per_unit: Decimal = Field(gt=0, max_digits=38, decimal_places=18)
|
||||
date: date_ = Field(default_factory=date_.today)
|
||||
note: str | None = Field(default=None, max_length=500)
|
||||
|
||||
|
||||
class CryptoHoldingRead(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
asset_id: int
|
||||
coingecko_id: str
|
||||
symbol: str
|
||||
name: str
|
||||
thumb_url: str | None
|
||||
|
||||
# Quantity and avg_buy_price are derived from the transaction log (see
|
||||
# services/crypto_service.py's _compute_position) — never stored.
|
||||
quantity: Decimal
|
||||
# Both derived from the linked Asset's latest AssetValuation — null
|
||||
# value/unit_price means "added but never priced yet" (CoinGecko was
|
||||
# unreachable at creation time), not zero.
|
||||
avg_buy_price: Decimal | None
|
||||
|
||||
# Cached from the last successful CoinGecko sync — null until the
|
||||
# first one completes.
|
||||
current_price: Decimal | None
|
||||
price_change_1h: Decimal | None
|
||||
price_change_24h: Decimal | None
|
||||
price_change_7d: Decimal | None
|
||||
|
||||
# All derived from the above: value = current_price * quantity,
|
||||
# cost_basis = avg_buy_price * quantity, profit_loss = value - cost_basis.
|
||||
value: Decimal | None
|
||||
unit_price: Decimal | None
|
||||
as_of_date: str | None
|
||||
cost_basis: Decimal | None
|
||||
profit_loss: Decimal | None
|
||||
profit_loss_percent: float | None
|
||||
|
||||
|
||||
class CryptoSyncResult(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user