Add buys query table from exchange_to_wallet events
Denormalized read table with id, event_id, occurred_at, wallet, sats, fee_sats, and amount_eur. Projected on event creation and rebuildable via POST /api/buys/rebuild. Fiat amounts converted to EUR using Frankfurter rates. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS buys;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE buys (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
event_id BIGINT NOT NULL REFERENCES events(id) UNIQUE,
|
||||
occurred_at DATE NOT NULL,
|
||||
wallet TEXT NOT NULL,
|
||||
amount_eur NUMERIC(18,2) NOT NULL
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE buys DROP COLUMN fee_sats;
|
||||
--;;
|
||||
ALTER TABLE buys DROP COLUMN sats;
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE buys ADD COLUMN sats BIGINT NOT NULL DEFAULT 0;
|
||||
--;;
|
||||
ALTER TABLE buys ADD COLUMN fee_sats BIGINT;
|
||||
@@ -155,6 +155,30 @@ SELECT id, event_id, occurred_at, exchange, fiat_amount, fiat_currency,
|
||||
FROM deposits
|
||||
ORDER BY occurred_at DESC, id DESC
|
||||
|
||||
-- :name truncate-buys! :! :n
|
||||
-- :doc Delete all rows from the buys read table
|
||||
TRUNCATE buys
|
||||
|
||||
-- :name get-buy-events :? :*
|
||||
-- :doc Get exchange_to_wallet events with wallet name, oldest first
|
||||
SELECT e.id, e.occurred_at, e.sats, e.fee_sats, e.fiat_amount, e.fiat_currency,
|
||||
e.to_wallet_id, tw.name AS wallet
|
||||
FROM events e
|
||||
LEFT JOIN wallets tw ON tw.id = e.to_wallet_id
|
||||
WHERE e.event_type = 'exchange_to_wallet'
|
||||
ORDER BY e.occurred_at ASC, e.id ASC
|
||||
|
||||
-- :name insert-buy! :! :n
|
||||
-- :doc Insert a projected buy row
|
||||
INSERT INTO buys (event_id, occurred_at, wallet, sats, fee_sats, amount_eur)
|
||||
VALUES (:event-id, :occurred-at, :wallet, :sats, :fee-sats, :amount-eur)
|
||||
|
||||
-- :name get-all-buys :? :*
|
||||
-- :doc Get all projected buys ordered by date descending
|
||||
SELECT id, event_id, occurred_at, wallet, sats, fee_sats, amount_eur
|
||||
FROM buys
|
||||
ORDER BY occurred_at DESC, id DESC
|
||||
|
||||
-- :name get-kraken-hour-latest-24 :? :*
|
||||
-- :doc Get the 24 most recent Kraken hourly candles
|
||||
SELECT ts, open, high, low, close, vwap, volume, trade_count, created_at
|
||||
|
||||
Reference in New Issue
Block a user