Add event-sourced wallet and transaction tracking system

Wallets table with 7 seeded wallets, events table for three transaction
types (bank_to_exchange, exchange_to_wallet, wallet_to_wallet). Balances
computed from event credits/debits in sats. REST endpoints for CRUD.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 18:30:48 +01:00
co-authored by Claude Opus 4.6
parent e61f38d4f3
commit 5874952115
8 changed files with 162 additions and 2 deletions
+71
View File
@@ -65,3 +65,74 @@ SELECT rate_date, eur, dkk, eur_dkk, updated_at FROM currencies ORDER BY rate_da
-- :name get-currency-rates-by-date :? :1
-- :doc Get currency rates for a specific date
SELECT rate_date, eur, dkk, eur_dkk, updated_at FROM currencies WHERE rate_date = :rate-date
-- Wallets -------------------------------------------------------------------
-- :name insert-wallet! :! :n
-- :doc Insert a new wallet
INSERT INTO wallets (name) VALUES (:name)
-- :name get-all-wallets :? :*
-- :doc Get all wallets
SELECT id, name, created_at FROM wallets ORDER BY name
-- :name get-wallet-by-id :? :1
-- :doc Get a wallet by ID
SELECT id, name, created_at FROM wallets WHERE id = :id
-- Events --------------------------------------------------------------------
-- :name insert-event! :! :n
-- :doc Insert a new event
INSERT INTO events (event_type, occurred_at, sats, fee_sats, fiat_amount, fiat_currency, from_wallet_id, to_wallet_id, note)
VALUES (:event-type, :occurred-at, :sats, :fee-sats, :fiat-amount, :fiat-currency, :from-wallet-id, :to-wallet-id, :note)
-- :name get-all-events :? :*
-- :doc Get all events with wallet names
SELECT e.id, e.event_type, e.occurred_at, e.recorded_at,
e.sats, e.fee_sats, e.fiat_amount, e.fiat_currency,
e.from_wallet_id, fw.name AS from_wallet_name,
e.to_wallet_id, tw.name AS to_wallet_name,
e.note
FROM events e
LEFT JOIN wallets fw ON fw.id = e.from_wallet_id
LEFT JOIN wallets tw ON tw.id = e.to_wallet_id
ORDER BY e.occurred_at DESC
-- :name get-events-by-type :? :*
-- :doc Get events filtered by type
SELECT e.id, e.event_type, e.occurred_at, e.recorded_at,
e.sats, e.fee_sats, e.fiat_amount, e.fiat_currency,
e.from_wallet_id, fw.name AS from_wallet_name,
e.to_wallet_id, tw.name AS to_wallet_name,
e.note
FROM events e
LEFT JOIN wallets fw ON fw.id = e.from_wallet_id
LEFT JOIN wallets tw ON tw.id = e.to_wallet_id
WHERE e.event_type = :event-type
ORDER BY e.occurred_at DESC
-- :name get-events-by-wallet :? :*
-- :doc Get events involving a specific wallet
SELECT e.id, e.event_type, e.occurred_at, e.recorded_at,
e.sats, e.fee_sats, e.fiat_amount, e.fiat_currency,
e.from_wallet_id, fw.name AS from_wallet_name,
e.to_wallet_id, tw.name AS to_wallet_name,
e.note
FROM events e
LEFT JOIN wallets fw ON fw.id = e.from_wallet_id
LEFT JOIN wallets tw ON tw.id = e.to_wallet_id
WHERE e.from_wallet_id = :wallet-id OR e.to_wallet_id = :wallet-id
ORDER BY e.occurred_at DESC
-- :name get-wallet-balances :? :*
-- :doc Compute sats balance per wallet from events
SELECT w.id, w.name,
COALESCE(SUM(CASE WHEN e.to_wallet_id = w.id THEN e.sats ELSE 0 END), 0)
- COALESCE(SUM(CASE WHEN e.from_wallet_id = w.id THEN e.sats ELSE 0 END), 0)
- COALESCE(SUM(CASE WHEN e.from_wallet_id = w.id THEN e.fee_sats ELSE 0 END), 0)
AS balance_sats
FROM wallets w
LEFT JOIN events e ON e.from_wallet_id = w.id OR e.to_wallet_id = w.id
GROUP BY w.id, w.name
ORDER BY w.name