going somewhere.
This commit is contained in:
1 parent
cfb63cb473
commit
30d732f708
2 files changed
+66
-1
No files matched your search
@@ -76,7 +76,8 @@
|
||||
[?e :skirmish.ledger/sec ?sec]
|
||||
[?sec :skirmish.security/sec-id ?sec-id]
|
||||
[?e :skirmish.ledger/owner-public-key ?pk]
|
||||
[?tx :db/txInstant ?v]] db account-id)]
|
||||
[?tx :db/txInstant ?v]]
|
||||
db account-id)]
|
||||
{:amount (when (some? sec-id) (compute-face-value-from-db db amount sec-id date))
|
||||
:sec-id sec-id
|
||||
:account-public-key pk}))
|
||||
@@ -86,3 +87,42 @@
|
||||
|
||||
(defn create-security [conn sec-id interest-rate]
|
||||
(d/transact conn [[:skirmish.ledger/create-security sec-id interest-rate]]))
|
||||
|
||||
(defn get-account [db account-id]
|
||||
(d/pull db '[*] [:skirmish.ledger/owner-public-key account-id]))
|
||||
|
||||
(defn assoc-accounts [db transaction]
|
||||
(let [from-account (get-account db (:from-account-id transaction))
|
||||
to-account (get-account db (:to-account-id transaction))
|
||||
ret (assoc transaction :from-account from-account)
|
||||
ret (assoc ret :to-account to-account)]
|
||||
ret))
|
||||
|
||||
(defn get-new-from-account-value [{amount :amount from-account :from-account} old-change]
|
||||
(if (some? old-change)
|
||||
(assoc old-change 3 (- (get old-change 3) amount))
|
||||
[:db/add
|
||||
(:db/id from-account)
|
||||
:skirmish.ledger/amount
|
||||
(- (:skirmish.ledger/amount from-account) amount)]))
|
||||
|
||||
(defn get-new-to-account-value [{amount :amount to-account :to-account} old-change]
|
||||
(if (some? old-change)
|
||||
(assoc old-change 3 (+ (get old-change 3) amount))
|
||||
[:db/add
|
||||
(:db/id to-account)
|
||||
:skirmish.ledger/amount
|
||||
(+ (:skirmish.ledger/amount to-account) amount)]))
|
||||
|
||||
(defn compute-transactions [res transaction]
|
||||
(let [res-from-account (get res (:from-account-id transaction))
|
||||
from-account (get-new-from-account-value transaction res-from-account)
|
||||
res (assoc res (:from-account-id transaction) from-account)
|
||||
res-to-account (get res (:to-account-id transaction))
|
||||
to-account (get-new-to-account-value transaction res-to-account)
|
||||
res (assoc res (:to-account-id transaction) to-account)]
|
||||
res))
|
||||
|
||||
(defn create-transactions [db transactions]
|
||||
(let [transactions (map #(assoc-accounts db %) transactions)]
|
||||
(into #{} (vals (reduce compute-transactions {} transactions)))))
|
||||
@@ -259,3 +259,28 @@
|
||||
|
||||
;; 4. transfer sec from one account to the other.
|
||||
|
||||
(defn get-account-eids [db & owner-public-keys]
|
||||
(let [q (map #(vector :skirmish.ledger/owner-public-key %) owner-public-keys)
|
||||
ret (d/pull-many db '[:db/id] q)]
|
||||
(map :db/id ret)))
|
||||
|
||||
(facts "about transactions"
|
||||
(fact "about transferring an amount from one wallet to another"
|
||||
(let [conn (create-some-securities-and-accounts (create-conn))
|
||||
transaction {:from-account-id "1-sec-1"
|
||||
:to-account-id "2-sec-1"
|
||||
:amount 50}
|
||||
db (d/db conn)
|
||||
[eid1 eid2] (get-account-eids db "1-sec-1" "2-sec-1")
|
||||
expt #{[:db/add eid1 :skirmish.ledger/amount 50]
|
||||
[:db/add eid2 :skirmish.ledger/amount 250]}]
|
||||
(data-layer/create-transactions db [transaction]) => expt)) ;expt))
|
||||
(fact "about transferring an amount from one wallet to another"
|
||||
(let [conn (create-some-securities-and-accounts (create-conn))
|
||||
transaction {:from-account-id "1-sec-1"
|
||||
:to-account-id "1-sec-1"
|
||||
:amount 45}
|
||||
db (d/db conn)
|
||||
[eid1] (get-account-eids db "1-sec-1")
|
||||
expt #{[:db/add eid1 :skirmish.ledger/amount 100]}]
|
||||
(data-layer/create-transactions db [transaction]) => expt)))
|
||||
Reference in new issue
Block a user