From 30d732f708a19fa196e93de15e1e3d5341146817 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 10 Sep 2020 17:58:36 -0400 Subject: [PATCH] going somewhere. --- src/clj/skirmish/ledger/data_layer.clj | 56 ++++++++++++++++++--- test/clj/skirmish/ledger/test_core.clj | 67 ++++++++++++++++++-------- 2 files changed, 94 insertions(+), 29 deletions(-) diff --git a/src/clj/skirmish/ledger/data_layer.clj b/src/clj/skirmish/ledger/data_layer.clj index 7bd542f..73f4e71 100644 --- a/src/clj/skirmish/ledger/data_layer.clj +++ b/src/clj/skirmish/ledger/data_layer.clj @@ -57,8 +57,8 @@ interest-rates (d/q '[:find ?interest-rate ?v :in $ ?eid :where [?e :skirmish.security/interest-rate ?interest-rate ?tx] - [?e :skirmish.security/sec-id ?sec-id] - [?tx :db/txInstant ?v]] + [?e :skirmish.security/sec-id ?sec-id] + [?tx :db/txInstant ?v]] history sec-id)] (sort-by second interest-rates))) @@ -72,11 +72,12 @@ (let [[amount sec-id pk date] (d/q '[:find [?amount ?sec-id ?pk ?v] :in $ ?account-id :where [?e :skirmish.ledger/owner-public-key ?account-id] - [?e :skirmish.ledger/amount ?amount ?tx] - [?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)] + [?e :skirmish.ledger/amount ?amount ?tx] + [?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)] {:amount (when (some? sec-id) (compute-face-value-from-db db amount sec-id date)) :sec-id sec-id :account-public-key pk})) @@ -85,4 +86,43 @@ (d/q '[:find ?s :where [?e :skirmish.security/sec-id ?s]] db)) (defn create-security [conn sec-id interest-rate] - (d/transact conn [[:skirmish.ledger/create-security sec-id interest-rate]])) \ No newline at end of file + (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))))) \ No newline at end of file diff --git a/test/clj/skirmish/ledger/test_core.clj b/test/clj/skirmish/ledger/test_core.clj index d91d276..119f9ed 100644 --- a/test/clj/skirmish/ledger/test_core.clj +++ b/test/clj/skirmish/ledger/test_core.clj @@ -226,7 +226,7 @@ d+2d #inst"2020-09-07T00: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+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]]] (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]] @@ -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+2½d d+2½d) => [[20 d+2½d d+2½d]])) (fact "compute-face-value" - (let [d-1d #inst"2020-09-04T00: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+½d #inst"2020-09-05T12: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+2d #inst"2020-09-07T00: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+3½d #inst"2020-09-08T12:00:00.000-00:00" - amount 10000000 - 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-1d d-½d) => amount - (data-layer/compute-face-value amount interest-rates d-1d d+3d) => 10045101)) + (let [d-1d #inst"2020-09-04T00: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+½d #inst"2020-09-05T12: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+2d #inst"2020-09-07T00: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+3½d #inst"2020-09-08T12:00:00.000-00:00" + amount 10000000 + 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-1d d-½d) => amount + (data-layer/compute-face-value amount interest-rates d-1d d+3d) => 10045101)) (fact "we had bugs... we need to fix them" - (let [deposit-date #inst"2020-09-09T18:49:47.205-00:00" - value-date #inst"2020-09-09T18:52:56.760-00:00" - amount 105402100 - 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))) + (let [deposit-date #inst"2020-09-09T18:49:47.205-00:00" + value-date #inst"2020-09-09T18:52:56.760-00:00" + amount 105402100 + 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))) ;; 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)))