From 793c63927c72013bbc63d734a160ec88cdb84c20 Mon Sep 17 00:00:00 2001 From: Per Magnus Petersen Date: Sun, 8 Mar 2026 15:48:41 +0100 Subject: [PATCH] Add wallet_type column to distinguish exchanges from wallets Exchanges (can receive bank deposits) vs wallets (cold storage only). Supports ?type= filter on GET /api/wallets. Co-Authored-By: Claude Opus 4.6 --- .../20260308100000-add-wallet-type.down.sql | 1 + .../migrations/20260308100000-add-wallet-type.up.sql | 4 ++++ resources/queries.sql | 10 +++++++--- .../pmagnus/btcdata/web/controllers/transactions.clj | 11 +++++++---- 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 resources/migrations/20260308100000-add-wallet-type.down.sql create mode 100644 resources/migrations/20260308100000-add-wallet-type.up.sql diff --git a/resources/migrations/20260308100000-add-wallet-type.down.sql b/resources/migrations/20260308100000-add-wallet-type.down.sql new file mode 100644 index 0000000..8462daf --- /dev/null +++ b/resources/migrations/20260308100000-add-wallet-type.down.sql @@ -0,0 +1 @@ +ALTER TABLE wallets DROP COLUMN wallet_type; diff --git a/resources/migrations/20260308100000-add-wallet-type.up.sql b/resources/migrations/20260308100000-add-wallet-type.up.sql new file mode 100644 index 0000000..8391d26 --- /dev/null +++ b/resources/migrations/20260308100000-add-wallet-type.up.sql @@ -0,0 +1,4 @@ +ALTER TABLE wallets ADD COLUMN wallet_type TEXT NOT NULL DEFAULT 'exchange'; +--;; +UPDATE wallets SET wallet_type = 'wallet' +WHERE name IN ('Cold', 'Coldcard', 'Nunchuk Multi Sig'); diff --git a/resources/queries.sql b/resources/queries.sql index ebf8346..cdad95a 100644 --- a/resources/queries.sql +++ b/resources/queries.sql @@ -70,15 +70,19 @@ SELECT rate_date, eur, dkk, eur_dkk, updated_at FROM currencies WHERE rate_date -- :name insert-wallet! :! :n -- :doc Insert a new wallet -INSERT INTO wallets (name) VALUES (:name) +INSERT INTO wallets (name, wallet_type) VALUES (:name, :wallet-type) -- :name get-all-wallets :? :* -- :doc Get all wallets -SELECT id, name, created_at FROM wallets ORDER BY name +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, created_at FROM wallets WHERE id = :id +SELECT id, name, wallet_type, created_at FROM wallets WHERE id = :id -- Events -------------------------------------------------------------------- diff --git a/src/clj/pmagnus/btcdata/web/controllers/transactions.clj b/src/clj/pmagnus/btcdata/web/controllers/transactions.clj index 500c769..4f41d66 100644 --- a/src/clj/pmagnus/btcdata/web/controllers/transactions.clj +++ b/src/clj/pmagnus/btcdata/web/controllers/transactions.clj @@ -8,14 +8,17 @@ [java.time Instant LocalDate ZoneOffset] [java.util UUID])) -(defn list-wallets [{:keys [query-fn]} _req] - (response/ok (query-fn :get-all-wallets {}))) +(defn list-wallets [{:keys [query-fn]} req] + (let [wallet-type (get-in req [:query-params "type"])] + (if wallet-type + (response/ok (query-fn :get-wallets-by-type {:wallet-type wallet-type})) + (response/ok (query-fn :get-all-wallets {}))))) (defn create-wallet! [{:keys [query-fn]} req] - (let [{:keys [name]} (:body-params req)] + (let [{:keys [name wallet_type]} (:body-params req)] (if (str/blank? name) (response/bad-request {:error "name is required"}) - (do (query-fn :insert-wallet! {:name name}) + (do (query-fn :insert-wallet! {:name name :wallet-type (or wallet_type "exchange")}) (response/created "/api/wallets" {:name name}))))) (defn get-wallet-balances [{:keys [query-fn]} _req]