-- queries for btcdata -- :name insert-binance-price! :! :n -- :doc Insert a Binance BTC price INSERT INTO binance_price (price) VALUES (:price) -- :name get-latest-binance-price :? :1 -- :doc Get the most recent Binance BTC price SELECT price, recorded_at FROM binance_price ORDER BY id DESC LIMIT 1 -- :name upsert-kraken-hour! :! :n -- :doc Upsert a Kraken hourly OHLC candle INSERT INTO kraken_hour (ts, open, high, low, close, vwap, volume, trade_count) VALUES (:ts, :open, :high, :low, :close, :vwap, :volume, :trade-count) ON CONFLICT (ts) DO UPDATE SET open = EXCLUDED.open, high = EXCLUDED.high, low = EXCLUDED.low, close = EXCLUDED.close, vwap = EXCLUDED.vwap, volume = EXCLUDED.volume, trade_count = EXCLUDED.trade_count -- :name get-latest-kraken-hour :? :1 -- :doc Get the most recent Kraken hourly candle by timestamp SELECT ts, open, high, low, close, vwap, volume, trade_count, created_at FROM kraken_hour ORDER BY ts DESC LIMIT 1 -- :name upsert-kraken-day! :! :n -- :doc Upsert a Kraken daily OHLC candle INSERT INTO kraken_day (ts, open, high, low, close, vwap, volume, trade_count) VALUES (:ts, :open, :high, :low, :close, :vwap, :volume, :trade-count) ON CONFLICT (ts) DO UPDATE SET open = EXCLUDED.open, high = EXCLUDED.high, low = EXCLUDED.low, close = EXCLUDED.close, vwap = EXCLUDED.vwap, volume = EXCLUDED.volume, trade_count = EXCLUDED.trade_count -- :name get-latest-kraken-day :? :1 -- :doc Get the most recent Kraken daily candle by timestamp SELECT ts FROM kraken_day ORDER BY ts DESC LIMIT 1 -- :name insert-strike-price! :! :n -- :doc Insert a Strike ticker snapshot INSERT INTO strike_price (rates, quote_sats) VALUES (:rates, :quote-sats) -- :name get-latest-strike-price :? :1 -- :doc Get the most recent Strike ticker snapshot SELECT rates, recorded_at FROM strike_price ORDER BY id DESC LIMIT 1 -- :name upsert-currency-rates! :! :n -- :doc Upsert daily currency rates from Frankfurter INSERT INTO currencies (rate_date, eur, dkk, eur_dkk, updated_at) VALUES (:rate-date, :eur, :dkk, :eur-dkk, NOW()) ON CONFLICT (rate_date) DO UPDATE SET eur = EXCLUDED.eur, dkk = EXCLUDED.dkk, eur_dkk = EXCLUDED.eur_dkk, updated_at = NOW() -- :name get-latest-currency-rates :? :1 -- :doc Get the most recent currency rates SELECT rate_date, eur, dkk, eur_dkk, updated_at FROM currencies ORDER BY rate_date DESC LIMIT 1 -- :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, wallet_type) VALUES (:name, :wallet-type) -- :name get-all-wallets :? :* -- :doc Get all wallets SELECT id, name, wallet_type, created_at FROM wallets ORDER BY name -- :name get-wallets-by-type :? :* -- :doc Get wallets filtered by type SELECT id, name, wallet_type, created_at FROM wallets WHERE wallet_type = :wallet-type ORDER BY name -- :name get-wallet-by-id :? :1 -- :doc Get a wallet by ID SELECT id, name, wallet_type, created_at FROM wallets WHERE id = :id -- Events -------------------------------------------------------------------- -- :name insert-event! :? :1 -- :doc Insert a new event, returning its id 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) RETURNING id -- :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, e.id 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, e.id 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, e.id DESC -- :name truncate-deposits! :! :n -- :doc Delete all rows from the deposits read table TRUNCATE deposits -- :name get-deposit-events :? :* -- :doc Get bank_to_exchange events with exchange name, oldest first SELECT e.id, e.occurred_at, e.fiat_amount, e.fiat_currency, e.to_wallet_id, tw.name AS exchange, e.note FROM events e LEFT JOIN wallets tw ON tw.id = e.to_wallet_id WHERE e.event_type = 'bank_to_exchange' ORDER BY e.occurred_at ASC, e.id ASC -- :name insert-deposit! :! :n -- :doc Insert a projected deposit row INSERT INTO deposits (event_id, occurred_at, exchange, fiat_amount, fiat_currency, amount_eur, amount_dkk, amount_usd, note) VALUES (:event-id, :occurred-at, :exchange, :fiat-amount, :fiat-currency, :amount-eur, :amount-dkk, :amount-usd, :note) -- :name get-all-deposits :? :* -- :doc Get all projected deposits ordered by date descending SELECT id, event_id, occurred_at, exchange, fiat_amount, fiat_currency, amount_eur, amount_dkk, amount_usd, note 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 FROM kraken_hour ORDER BY ts DESC LIMIT 24 -- :name get-kraken-minute-latest-60 :? :* -- :doc Get the 60 most recent Kraken minute candles SELECT ts, open, high, low, close, vwap, volume, trade_count, created_at FROM kraken_minute ORDER BY ts DESC LIMIT 60 -- :name upsert-kraken-minute! :! :n -- :doc Upsert a Kraken minute OHLC candle INSERT INTO kraken_minute (ts, open, high, low, close, vwap, volume, trade_count) VALUES (:ts, :open, :high, :low, :close, :vwap, :volume, :trade-count) ON CONFLICT (ts) DO UPDATE SET open = EXCLUDED.open, high = EXCLUDED.high, low = EXCLUDED.low, close = EXCLUDED.close, vwap = EXCLUDED.vwap, volume = EXCLUDED.volume, trade_count = EXCLUDED.trade_count -- :name get-latest-kraken-minute :? :1 -- :doc Get the most recent Kraken minute candle timestamp SELECT ts FROM kraken_minute ORDER BY ts DESC LIMIT 1 -- :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