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:
ZProger
2026-08-30 04:25:03 +03:00
parent 7e4508d1de
commit e8cd536584
24 changed files with 1154 additions and 374 deletions

View File

@@ -1,5 +1,12 @@
import { api } from "@/api/client";
import type { CryptoHolding, CryptoHoldingInput, CryptoSearchResult, CryptoSyncResult } from "@/types";
import type {
CryptoHolding,
CryptoHoldingCreateInput,
CryptoSearchResult,
CryptoSyncResult,
CryptoTransaction,
CryptoTransactionInput,
} from "@/types";
// Also triggers the lazy once-a-day auto-refresh server-side — see
// services/crypto_service.py.
@@ -11,16 +18,26 @@ export function refreshCryptoPrices() {
return api.post<CryptoSyncResult>("/crypto/refresh", {});
}
export function createCryptoHolding(input: CryptoHoldingInput) {
export function createCryptoHolding(input: CryptoHoldingCreateInput) {
return api.post<CryptoHolding>("/crypto/holdings", input);
}
export function updateCryptoHoldingQuantity(assetId: number, quantity: string) {
return api.patch<CryptoHolding>(`/crypto/holdings/${assetId}`, { quantity });
// Buy more of, or sell some of, a coin already being tracked.
export function addCryptoTransaction(assetId: number, input: CryptoTransactionInput) {
return api.post<CryptoHolding>(`/crypto/holdings/${assetId}/transactions`, input);
}
export function fetchCryptoTransactions(assetId: number) {
return api.get<CryptoTransaction[]>(`/crypto/holdings/${assetId}/transactions`);
}
export function deleteCryptoTransaction(transactionId: number) {
return api.delete<void>(`/crypto/transactions/${transactionId}`);
}
// Reuses the existing asset-delete endpoint — deleting the Asset cascades
// to the linked crypto_holdings row server-side, no dedicated endpoint.
// to the linked crypto_holdings row (and its whole transaction log)
// server-side, no dedicated endpoint.
export function deleteCryptoHolding(assetId: number) {
return api.delete<void>(`/assets/${assetId}`);
}