Implemented interest rates

This commit is contained in:
didier amyot committed 2020-09-10 10:33:57 -04:00
1 parent d5c6360f88
commit 1fa115db79
2 files changed
+49 -41

No files matched your search

+42 -34
View File
@@ -68,38 +68,45 @@
(long amount) (long amount)
(recur (rest interest-rates) amount))))) (recur (rest interest-rates) amount)))))
(defn get-interest-rate-history [db sec-id]
(defn get-amount-in-account [db account-id] (let [history (d/history db)
(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)
interest-rates (d/q '[:find ?interest-rate ?v interest-rates (d/q '[:find ?interest-rate ?v
:in $ ?eid :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]] [?tx :db/txInstant ?v]]
history sec-id) history sec-id)]
face-value (compute-face-value amount (sort-by second interest-rates)))
date
(sort-by second interest-rates) (defn compute-face-value-from-db [db amount sec-id date]
(new java.util.Date))] (compute-face-value amount
{:value face-value (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 :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-id is the public key of a
;; an :account-secret is an :account-id private key ;; an :account-secret is an :account-id private key
(defn get-account-body [request account sec] (defn get-account-body [request {sec-id :sec-id amount :amount account-public-key :account-public-key}]
(if (nil? sec) (if (nil? sec-id)
(build-response request {:status 404}) (build-response request {:status 404})
(let [sec-url (str "/api/1/ledger/securities/" (-> sec :skirmish.security/sec-id name)) (let [sec-url (str "/api/1/ledger/securities/" (name sec-id))
beautify-account { :amount (:skirmish.ledger/amount account) beautify-account { :amount amount
:sec-id (:skirmish.security/sec-id sec) :sec-id sec-id
:account-public-key (:skirmish.ledger/owner-public-key account)} :account-public-key account-public-key}
ret {:account beautify-account :sec-url sec-url}] ret {:account beautify-account :sec-url sec-url}]
(build-response request ret)))) (build-response request ret))))
@@ -107,11 +114,8 @@
"Returns the value of an account." "Returns the value of an account."
(fn [{{account-id :account-id} :route-params :as request}] (fn [{{account-id :account-id} :route-params :as request}]
(let [db (d/db conn) (let [db (d/db conn)
account (d/pull db '[*] [:skirmish.ledger/owner-public-key account-id]) account-info (get-amount-in-account db account-id)]
sec-eid (-> account :skirmish.ledger/sec :db/id) (get-account-body request account-info))))
sec (d/entity db sec-eid)]
(get-account-body request account sec))))
(s/def ::transaction (s/cat :type #{:create} (s/def ::transaction (s/cat :type #{:create}
:sec-id keyword? :sec-id keyword?
@@ -147,16 +151,20 @@
name :skirmish.security/name name :skirmish.security/name
number-of-decimal :skirmish.security/number-of-decimal number-of-decimal :skirmish.security/number-of-decimal
type_ :skirmish.security/type} e type_ :skirmish.security/type} e
volume (d/q '[:find (sum ?amount) . interest-rate-history (get-interest-rate-history db id)
:in $ ?id volumes (d/q '[:find ?amount ?date
:where [?e :skirmish.ledger/sec ?id] :in $ ?id
[?e :skirmish.ledger/amount ?amount]] :where [?e :skirmish.ledger/sec ?id]
[?e :skirmish.ledger/amount ?amount ?tx]
db id)] [?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 {:sec-id sec-id
:interest-rate interest-rate :interest-rate interest-rate
:volume (or volume 0) :volume (or volume 0)
:name name :name name
:interest-rate-history interest-rate-history
:symbol symbol :symbol symbol
:number-of-decimal number-of-decimal :number-of-decimal number-of-decimal
:type type_ :type type_
+7 -7
View File
@@ -244,13 +244,13 @@
interest-rates [[10 d] [15 d+1d] [20 d+2d]]] 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+½d d+½d) => amount
(compute-face-value amount interest-rates d-1d 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))) (compute-face-value amount interest-rates d-1d d+3d) => 10045101))
(fact "we had bugs... we need to fix them"
;(split-interest-rates interest-rates d+3d d+½d) => [[10 d d+½d]]))) (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
;; TOD 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. ;; 4. transfer sec from one account to the other.