going somewhere.

This commit is contained in:
didier amyot committed 2020-09-10 17:58:36 -04:00
1 parent cfb63cb473
commit 30d732f708
2 files changed
+94 -29

No files matched your search

+48 -8
View File
@@ -57,8 +57,8 @@
interest-rates (d/q '[:find ?interest-rate ?v interest-rates (d/q '[:find ?interest-rate ?v
:in $ ?eid :in $ ?eid
:where [?e :skirmish.security/interest-rate ?interest-rate ?tx] :where [?e :skirmish.security/interest-rate ?interest-rate ?tx]
[?e :skirmish.security/sec-id ?sec-id] [?e :skirmish.security/sec-id ?sec-id]
[?tx :db/txInstant ?v]] [?tx :db/txInstant ?v]]
history sec-id)] history sec-id)]
(sort-by second interest-rates))) (sort-by second interest-rates)))
@@ -72,11 +72,12 @@
(let [[amount sec-id pk date] (d/q '[:find [?amount ?sec-id ?pk ?v] (let [[amount sec-id pk date] (d/q '[:find [?amount ?sec-id ?pk ?v]
:in $ ?account-id :in $ ?account-id
:where [?e :skirmish.ledger/owner-public-key ?account-id] :where [?e :skirmish.ledger/owner-public-key ?account-id]
[?e :skirmish.ledger/amount ?amount ?tx] [?e :skirmish.ledger/amount ?amount ?tx]
[?e :skirmish.ledger/sec ?sec] [?e :skirmish.ledger/sec ?sec]
[?sec :skirmish.security/sec-id ?sec-id] [?sec :skirmish.security/sec-id ?sec-id]
[?e :skirmish.ledger/owner-public-key ?pk] [?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)) {:amount (when (some? sec-id) (compute-face-value-from-db db amount sec-id date))
:sec-id sec-id :sec-id sec-id
:account-public-key pk})) :account-public-key pk}))
@@ -85,4 +86,43 @@
(d/q '[:find ?s :where [?e :skirmish.security/sec-id ?s]] db)) (d/q '[:find ?s :where [?e :skirmish.security/sec-id ?s]] db))
(defn create-security [conn sec-id interest-rate] (defn create-security [conn sec-id interest-rate]
(d/transact conn [[:skirmish.ledger/create-security 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)))))
+46 -21
View File
@@ -226,7 +226,7 @@
d+2d #inst"2020-09-07T00:00:00.000-00:00" d+2d #inst"2020-09-07T00:00:00.000-00:00"
d+2½d #inst"2020-09-07T12:00:00.000-00:00" d+2½d #inst"2020-09-07T12:00:00.000-00:00"
d+3d #inst"2020-09-08T00:00:00.000-00:00" d+3d #inst"2020-09-08T00:00:00.000-00:00"
d+3½d #inst"2020-09-08T12:00:00.000-00:00";; d+3½d #inst"2020-09-08T12:00:00.000-00:00" ;;
interest-rates [[10 d] [15 d+1d] [20 d+2d]]] interest-rates [[10 d] [15 d+1d] [20 d+2d]]]
(data-layer/split-interest-rates interest-rates d+½d d+3d) => [[10 d+½d d+1d] [15 d+1d d+2d] [20 d+2d d+3d]] (data-layer/split-interest-rates interest-rates d+½d d+3d) => [[10 d+½d d+1d] [15 d+1d d+2d] [20 d+2d d+3d]]
(data-layer/split-interest-rates interest-rates d+3d d+3½d) => [[20 d+3d d+3½d]] (data-layer/split-interest-rates interest-rates d+3d d+3½d) => [[20 d+3d d+3½d]]
@@ -234,28 +234,53 @@
(data-layer/split-interest-rates interest-rates d d+½d) => [[10 d d+½d]] (data-layer/split-interest-rates interest-rates d d+½d) => [[10 d d+½d]]
(data-layer/split-interest-rates interest-rates d+2½d d+2½d) => [[20 d+2½d d+2½d]])) (data-layer/split-interest-rates interest-rates d+2½d d+2½d) => [[20 d+2½d d+2½d]]))
(fact "compute-face-value" (fact "compute-face-value"
(let [d-1d #inst"2020-09-04T00:00:00.000-00:00" (let [d-1d #inst"2020-09-04T00:00:00.000-00:00"
d-½d #inst"2020-09-04T12:00:00.000-00:00" d-½d #inst"2020-09-04T12:00:00.000-00:00"
d #inst"2020-09-05T00:00:00.000-00:00" d #inst"2020-09-05T00:00:00.000-00:00"
d+½d #inst"2020-09-05T12:00:00.000-00:00" d+½d #inst"2020-09-05T12:00:00.000-00:00"
d+1d #inst"2020-09-06T00:00:00.000-00:00" d+1d #inst"2020-09-06T00:00:00.000-00:00"
d+1½d #inst"2020-09-06T12:00:00.000-00:00" d+1½d #inst"2020-09-06T12:00:00.000-00:00"
d+2d #inst"2020-09-07T00:00:00.000-00:00" d+2d #inst"2020-09-07T00:00:00.000-00:00"
d+2½d #inst"2020-09-07T12:00:00.000-00:00" d+2½d #inst"2020-09-07T12:00:00.000-00:00"
d+3d #inst"2020-09-08T00:00:00.000-00:00" d+3d #inst"2020-09-08T00:00:00.000-00:00"
d+3½d #inst"2020-09-08T12:00:00.000-00:00" d+3½d #inst"2020-09-08T12:00:00.000-00:00"
amount 10000000 amount 10000000
interest-rates [[10 d] [15 d+1d] [20 d+2d]]] interest-rates [[10 d] [15 d+1d] [20 d+2d]]]
(data-layer/compute-face-value amount interest-rates d+½d d+½d) => amount (data-layer/compute-face-value amount interest-rates d+½d d+½d) => amount
(data-layer/compute-face-value amount interest-rates d-1d d-½d) => amount (data-layer/compute-face-value amount interest-rates d-1d d-½d) => amount
(data-layer/compute-face-value amount interest-rates d-1d d+3d) => 10045101)) (data-layer/compute-face-value amount interest-rates d-1d d+3d) => 10045101))
(fact "we had bugs... we need to fix them" (fact "we had bugs... we need to fix them"
(let [deposit-date #inst"2020-09-09T18:49:47.205-00:00" (let [deposit-date #inst"2020-09-09T18:49:47.205-00:00"
value-date #inst"2020-09-09T18:52:56.760-00:00" value-date #inst"2020-09-09T18:52:56.760-00:00"
amount 105402100 amount 105402100
interest-rates (list [1000 #inst "2020-09-09T18:47:34.152-00:00"])] interest-rates (list [1000 #inst "2020-09-09T18:47:34.152-00:00"])]
(data-layer/compute-face-value amount interest-rates deposit-date value-date) => 105424060))) (data-layer/compute-face-value amount interest-rates deposit-date value-date) => 105424060)))
;; 4. transfer sec from one account to the other. ;; 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)))