started to implement transactions.

This commit is contained in:
didier amyot committed 2020-09-10 12:17:42 -04:00
1 parent 1fa115db79
commit c5d4e83d44
5 files changed
+52 -23

No files matched your search

+12 -1
View File
@@ -35,6 +35,16 @@
[:db/add entity :skirmish.ledger/owner-public-key account-public-key] [:db/add entity :skirmish.ledger/owner-public-key account-public-key]
[:db/add entity :skirmish.ledger/sec (:db/id security)]])})}]) [:db/add entity :skirmish.ledger/sec (:db/id security)]])})}])
(def create-transactions
[{:db/ident :skirmish.ledger/create-transactions
:db/doc "Move money from one account to the other."
:db/fn (d/function {:lang "clojure"
:requires ['[slingshot.slingshot]]
:params '[db transactions]
:code '(let [from-account-id (map :from-account-id transactions)
from-accounts (d/pull-many db '[*] (for [] [:skirmish.ledger/owner-public-key "2-sec-2"]))]
[])})}])
;; there are different types of securities... ;; there are different types of securities...
;; 1. currencies, which pays interest rates in their one sec-id. ;; 1. currencies, which pays interest rates in their one sec-id.
;; 2. equities, which do not pay interest rates. ;; 2. equities, which do not pay interest rates.
@@ -114,7 +124,8 @@
(concat schema-securities) (concat schema-securities)
(concat ledger-schema) (concat ledger-schema)
(concat create-securities) (concat create-securities)
(concat print-securities))) (concat print-securities)
(concat create-transactions)))
(defn add-default-currencies [conn] (defn add-default-currencies [conn]
+23 -7
View File
@@ -117,15 +117,16 @@
account-info (get-amount-in-account db account-id)] account-info (get-amount-in-account db account-id)]
(get-account-body request account-info)))) (get-account-body request account-info))))
(s/def ::transaction (s/cat :type #{:create} (s/def ::transaction (s/cat :type #{:transfer-immediately}
:sec-id keyword? :from-account-id string?
:amount int? :to-account-id string?
:account-id string?)) :amount int?))
(s/def ::transactions (s/coll-of ::transaction))
(defn get-transaction [raw-transact] (defn extract-transactions [raw-transactions]
(if (s/valid? ::transaction raw-transact) (if (s/valid? ::transactions raw-transactions)
(s/conform ::transaction raw-transact) (s/conform ::transactions raw-transactions)
(throw+ {:error-type :4XX :status 400 :body {:error "Invalid transaction format."}}))) (throw+ {:error-type :4XX :status 400 :body {:error "Invalid transaction format."}})))
@@ -267,6 +268,19 @@
{:url "ledger/securities" :name "securities"}]] {:url "ledger/securities" :name "securities"}]]
(build-response request body))) (build-response request body)))
(defn get-transactions [conn]
(fn [request]
(let [all-pending-transactions []]
(build-response request all-pending-transactions))))
(defn create-transactions [conn]
(fn [{body :body :as request}]
(try+
(let [transactions (-> body get-body extract-transactions)
_ @(d/transact conn [[:skirmish.ledger/create-transactions transactions]])])
(catch [:error-type :4XX] e (build-response request e))
(catch [:error-type :5XX] e (build-response request e)))))
(defn create-routes-for-ledger [conn] (defn create-routes-for-ledger [conn]
(compojure/routes (compojure/routes
@@ -274,6 +288,8 @@
(routes/GET :ledger/home [] get-api-description) (routes/GET :ledger/home [] get-api-description)
(routes/GET :ledger/accounts [] (get-available-accounts conn)) (routes/GET :ledger/accounts [] (get-available-accounts conn))
(routes/GET :ledger/account [] (get-account conn)) (routes/GET :ledger/account [] (get-account conn))
(routes/GET :ledger/transactions [] (get-transactions conn))
(routes/POST :ledger/transactions [] (create-transactions conn))
(routes/GET :ledger/securities [] (get-available-securities conn)) ;; done (routes/GET :ledger/securities [] (get-available-securities conn)) ;; done
(routes/POST :ledger/securities [] (create-security conn)) ;; done (routes/POST :ledger/securities [] (create-security conn)) ;; done
(routes/GET :ledger/security [] (get-security conn)) ;; done (routes/GET :ledger/security [] (get-security conn)) ;; done
@@ -22,12 +22,13 @@
[:td "Security ID"] [:td (:sec-id sec-info)]] [:td "Security ID"] [:td (:sec-id sec-info)]]
[:tr [:tr
[:td "Type"] [:td (:type sec-info)]] [:td "Type"] [:td (:type sec-info)]]
(when-let [symbol (:symbol sec-info)]
[:tr
[:td "Symbol"] [:td symbol]])
[:tr [:tr
[:td "Symbol"] [:td (:symbol sec-info)]] [:td "Interest Rate"] [:td (-> sec-info :interest-rate (pp-int 2) (str " %"))]]
[:tr [:tr
[:td "Interest Rate"] [:td (:interest-rate sec-info)]] [:td "Total Volume"] [:td (-> sec-info :volume (pp-int (:number-of-decimal sec-info)) (str " " (:symbol sec-info)))]]]]])
[:tr
[:td "Total Volume"] [:td (-> sec-info :volume (pp-int (:number-of-decimal sec-info)))]]]]])
(defn ledger-table [sec-id] (defn ledger-table [sec-id]
@@ -47,8 +47,8 @@
[:li "move 'routes' in 'rest'"] [:li "move 'routes' in 'rest'"]
[:li "add a little bit of professionalism on top of the broker api"] [:li "add a little bit of professionalism on top of the broker api"]
[:li "make the api support transit and edn"] [:li "make the api support transit and edn"]
[:li on-it "implements interest rate in ledger."] [:li done "implements interest rate in ledger."]
[:li "Make sure the ledger can have accounts"] [:li done "Make sure the ledger can have accounts"]
[:li done "when a new user signs in, he should receive some sort of money"] [:li done "when a new user signs in, he should receive some sort of money"]
[:li "implement send securities to friends"] [:li "implement send securities to friends"]
[:li "implement buy/sell securities."] [:li "implement buy/sell securities."]
+10 -9
View File
@@ -98,16 +98,17 @@
create-request {:body (edn->stream security) :headers {"accept" "application/edn"}} create-request {:body (edn->stream security) :headers {"accept" "application/edn"}}
_ ((create-security conn) create-request) _ ((create-security conn) create-request)
request {:route-params {:sec-id :new-currency} :headers {"accept" "application/edn"}} request {:route-params {:sec-id :new-currency} :headers {"accept" "application/edn"}}
resp ((get-security conn) request)] resp ((get-security conn) request)
expt {:sec-id :new-currency
:interest-rate 100
:name nil
:description nil
:number-of-decimal nil
:symbol nil
:type nil
:volume 0}]
resp => (spec-check ::ok) resp => (spec-check ::ok)
(body resp) => {:sec-id :new-currency (body resp) => (contains expt)))
:interest-rate 100
:name nil
:description nil
:number-of-decimal nil
:symbol nil
:type nil
:volume 0}))
(fact "we should not be able to get-security if it does not exists" (fact "we should not be able to get-security if it does not exists"
(let [conn (create-conn) (let [conn (create-conn)