going somewhere.
This commit is contained in:
1 parent
30d732f708
commit
75a91f7f7b
2 files changed
+84
-28
No files matched your search
@@ -1,6 +1,7 @@
|
||||
(ns skirmish.ledger.data-layer
|
||||
(:require [datomic.api :as d]
|
||||
[infix.macros :refer [infix]]))
|
||||
[infix.macros :refer [infix]]
|
||||
[slingshot.slingshot :refer [throw+]]))
|
||||
|
||||
(defn get-all-account-public-key [db]
|
||||
(d/q '[:find ?pk :where [?e :skirmish.ledger/owner-public-key ?pk]] db))
|
||||
@@ -123,6 +124,31 @@
|
||||
res (assoc res (:to-account-id transaction) to-account)]
|
||||
res))
|
||||
|
||||
(defn validate-transactions [transactions]
|
||||
(loop [transactions transactions]
|
||||
(when (seq transactions)
|
||||
(let [transaction (first transactions)]
|
||||
(when (not= (-> transaction :from-account :skirmish.ledger/sec)
|
||||
(-> transaction :to-account :skirmish.ledger/sec))
|
||||
(throw+ {:error-type :4XX
|
||||
:status 400
|
||||
:body {:error "Part of the transaction includes a transaction between two wallets of different underlying securities."}}))
|
||||
(recur (rest transactions)))))
|
||||
transactions)
|
||||
|
||||
(defn validate-resulting-transactions [transactions]
|
||||
(loop [transactions transactions]
|
||||
(when (seq transactions)
|
||||
(let [transaction (first transactions)]
|
||||
(when (neg? (get transaction 3))
|
||||
(throw+ {:error-type :4XX
|
||||
:status 400
|
||||
:body {:error "Transaction would result in a negative amount in a wallet."}})))
|
||||
(recur (rest transactions))))
|
||||
transactions)
|
||||
|
||||
(defn create-transactions [db transactions]
|
||||
(let [transactions (map #(assoc-accounts db %) transactions)]
|
||||
(into #{} (vals (reduce compute-transactions {} transactions)))))
|
||||
(let [transactions (map #(assoc-accounts db %) transactions)
|
||||
transactions (validate-transactions transactions)
|
||||
ret (vals (reduce compute-transactions {} transactions))]
|
||||
(validate-resulting-transactions ret)))
|
||||
Reference in new issue
Block a user