diff --git a/src/clj/skirmish/ledger/core.clj b/src/clj/skirmish/ledger/core.clj index 9b0de05..a51641f 100644 --- a/src/clj/skirmish/ledger/core.clj +++ b/src/clj/skirmish/ledger/core.clj @@ -68,38 +68,45 @@ (long amount) (recur (rest interest-rates) amount))))) - -(defn get-amount-in-account [db account-id] - (let [[amount sec-id date] (d/q '[:find [?amount ?sec ?v] - :in $ ?account-id - :where [?e :skirmish.ledger/owner-public-key ?account-id] - [?e :skirmish.ledger/amount ?amount ?tx] - [?e :skirmish.ledger/sec ?sec] - [?tx :db/txInstant ?v]] db account-id) - history (d/history db) +(defn get-interest-rate-history [db sec-id] + (let [history (d/history db) interest-rates (d/q '[:find ?interest-rate ?v :in $ ?eid - :where [?eid :skirmish.security/interest-rate ?interest-rate ?tx] + :where [?e :skirmish.security/interest-rate ?interest-rate ?tx] + [?e :skirmish.security/sec-id ?sec-id] [?tx :db/txInstant ?v]] - history sec-id) - face-value (compute-face-value amount - date - (sort-by second interest-rates) - (new java.util.Date))] - {:value face-value + history sec-id)] + (sort-by second interest-rates))) + +(defn compute-face-value-from-db [db amount sec-id date] + (compute-face-value amount + (get-interest-rate-history db sec-id) + date + (new java.util.Date))) + +(defn get-amount-in-account [db account-id] + (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)] + {:amount (when (some? sec-id) (compute-face-value-from-db db amount sec-id date)) :sec-id sec-id - :account-public-key (:skirmish.ledger/owner-public-key 333)})) + :account-public-key pk})) ;; an :account-id is the public key of a ;; an :account-secret is an :account-id private key -(defn get-account-body [request account sec] - (if (nil? sec) +(defn get-account-body [request {sec-id :sec-id amount :amount account-public-key :account-public-key}] + (if (nil? sec-id) (build-response request {:status 404}) - (let [sec-url (str "/api/1/ledger/securities/" (-> sec :skirmish.security/sec-id name)) - beautify-account { :amount (:skirmish.ledger/amount account) - :sec-id (:skirmish.security/sec-id sec) - :account-public-key (:skirmish.ledger/owner-public-key account)} + (let [sec-url (str "/api/1/ledger/securities/" (name sec-id)) + beautify-account { :amount amount + :sec-id sec-id + :account-public-key account-public-key} ret {:account beautify-account :sec-url sec-url}] (build-response request ret)))) @@ -107,11 +114,8 @@ "Returns the value of an account." (fn [{{account-id :account-id} :route-params :as request}] (let [db (d/db conn) - account (d/pull db '[*] [:skirmish.ledger/owner-public-key account-id]) - sec-eid (-> account :skirmish.ledger/sec :db/id) - sec (d/entity db sec-eid)] - (get-account-body request account sec)))) - + account-info (get-amount-in-account db account-id)] + (get-account-body request account-info)))) (s/def ::transaction (s/cat :type #{:create} :sec-id keyword? @@ -147,16 +151,20 @@ name :skirmish.security/name number-of-decimal :skirmish.security/number-of-decimal type_ :skirmish.security/type} e - volume (d/q '[:find (sum ?amount) . - :in $ ?id - :where [?e :skirmish.ledger/sec ?id] - [?e :skirmish.ledger/amount ?amount]] - - db id)] + interest-rate-history (get-interest-rate-history db id) + volumes (d/q '[:find ?amount ?date + :in $ ?id + :where [?e :skirmish.ledger/sec ?id] + [?e :skirmish.ledger/amount ?amount ?tx] + [?tx :db/txInstant ?date]] + db id) + now (new java.util.Date) + volume (reduce + 0 (map #(compute-face-value (first %) interest-rate-history (second %) now) volumes))] {:sec-id sec-id :interest-rate interest-rate :volume (or volume 0) :name name + :interest-rate-history interest-rate-history :symbol symbol :number-of-decimal number-of-decimal :type type_ diff --git a/test/clj/skirmish/ledger/test_core.clj b/test/clj/skirmish/ledger/test_core.clj index 5ed67c0..780166f 100644 --- a/test/clj/skirmish/ledger/test_core.clj +++ b/test/clj/skirmish/ledger/test_core.clj @@ -244,13 +244,13 @@ interest-rates [[10 d] [15 d+1d] [20 d+2d]]] (compute-face-value amount interest-rates d+½d d+½d) => amount (compute-face-value amount interest-rates d-1d d-½d) => amount - (compute-face-value amount interest-rates d-1d d+3d) => 10045101))) - - ;(split-interest-rates interest-rates d+3d d+½d) => [[10 d d+½d]]))) - - -;; TOD - + (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"])] + (compute-face-value amount interest-rates deposit-date value-date) => 105424060))) ;; 4. transfer sec from one account to the other.