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,20 +1,22 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
addCryptoTransaction,
createCryptoHolding,
deleteCryptoHolding,
deleteCryptoTransaction,
fetchCryptoHoldings,
fetchCryptoTransactions,
refreshCryptoPrices,
searchCryptoCoins,
updateCryptoHoldingQuantity,
} from "@/api/crypto";
import type { CryptoHoldingInput } from "@/types";
import type { CryptoHoldingCreateInput, CryptoTransactionInput } from "@/types";
function useInvalidateCrypto() {
const queryClient = useQueryClient();
return () => {
queryClient.invalidateQueries({ queryKey: ["crypto-holdings"] });
queryClient.invalidateQueries({ queryKey: ["crypto-transactions"] });
// A crypto holding is a normal Asset under the hood — Net Worth's own
// numbers change the moment one is added/edited/repriced/removed.
// numbers change the moment one is added/traded/repriced/removed.
queryClient.invalidateQueries({ queryKey: ["net-worth-summary"] });
};
}
@@ -23,6 +25,14 @@ export function useCryptoHoldings() {
return useQuery({ queryKey: ["crypto-holdings"], queryFn: fetchCryptoHoldings });
}
export function useCryptoTransactions(assetId: number | null) {
return useQuery({
queryKey: ["crypto-transactions", assetId],
queryFn: () => fetchCryptoTransactions(assetId!),
enabled: assetId !== null,
});
}
export function useRefreshCryptoPrices() {
const invalidate = useInvalidateCrypto();
return useMutation({ mutationFn: refreshCryptoPrices, onSuccess: invalidate });
@@ -31,16 +41,24 @@ export function useRefreshCryptoPrices() {
export function useCreateCryptoHolding() {
const invalidate = useInvalidateCrypto();
return useMutation({
mutationFn: (input: CryptoHoldingInput) => createCryptoHolding(input),
mutationFn: (input: CryptoHoldingCreateInput) => createCryptoHolding(input),
onSuccess: invalidate,
});
}
export function useUpdateCryptoHoldingQuantity() {
export function useAddCryptoTransaction() {
const invalidate = useInvalidateCrypto();
return useMutation({
mutationFn: ({ assetId, quantity }: { assetId: number; quantity: string }) =>
updateCryptoHoldingQuantity(assetId, quantity),
mutationFn: ({ assetId, input }: { assetId: number; input: CryptoTransactionInput }) =>
addCryptoTransaction(assetId, input),
onSuccess: invalidate,
});
}
export function useDeleteCryptoTransaction() {
const invalidate = useInvalidateCrypto();
return useMutation({
mutationFn: (transactionId: number) => deleteCryptoTransaction(transactionId),
onSuccess: invalidate,
});
}
@@ -52,8 +70,3 @@ export function useDeleteCryptoHolding() {
onSuccess: invalidate,
});
}
// Not a useQuery — this drives a search-as-you-type picker inside the "add
// holding" modal, called imperatively (debounced) rather than kept in sync
// with a query key.
export { searchCryptoCoins };