going somewhere

This commit is contained in:
didier amyot committed 2020-09-09 13:10:13 -04:00
1 parent b7af65b705
commit d5c6360f88
12 files changed
+176 -41

No files matched your search

-3
View File
@@ -8,19 +8,16 @@
[skirmish.routes.core :as routes]
[clj-http.client :as http-client]))
(defn generate-amount-for-new-user [seed]
(let [random (new java.util.Random seed)
random-fn #(. random nextInt 20000)]
(* 100 (reduce + (take 100 (repeatedly random-fn))))))
(defn create-account [account-id sec-id]
#:skirmish.broker.account{:id account-id
:sec-id sec-id
:private-key (str account-id)})
(defn create-user-account [seed default-sec-id]
(let [url (routes/get-path-for :ledger/security {:sec-id (name default-sec-id)})
account-id (java.util.UUID/randomUUID)
+1 -1
View File
@@ -54,7 +54,7 @@
(concat accounts-schema)))
(defn create-connection [uri]
(let [new-db? (d/create-database uri)
(let [_ (d/create-database uri)
conn (d/connect uri)]
@(d/transact conn schema)
conn))
+6 -5
View File
@@ -36,7 +36,8 @@
exchange-apis (create-exchange-apis-for config securities-api)]
(if (= (count target-fns) 1)
((first target-fns) config securities-api exchange-apis)
(run-all-targets-in-separate-threads target-fns config securities-api exchange-apis))))
(run-all-targets-in-separate-threads target-fns config securities-api exchange-apis))
(skirmish.util/block-thread)))
(defn -main [& argv]
(let [config (-> "config.edn" clojure.java.io/resource read-config)
@@ -47,7 +48,7 @@
(if (seq target-fns)
(run-all-targets target-fns config)
(do
(timbre/info argv)
(timbre/info "target" target-fns "not found")
(timbre/info "all known targets are...")
(timbre/info (keys targets))))))
(timbre/warn argv)
(timbre/warn "target" target-fns "not found")
(timbre/warn "all known targets are...")
(timbre/warn (keys targets))))))
+8 -2
View File
@@ -82,7 +82,12 @@
:db/ident :skirmish.security/interest-rate
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "The interest-rate payed by the security as a multiple of 0.01% (100 is 1% interest rate)."}])
:db/doc "The daily interest-rate payed by the security as a multiple of 0.01% (100 is 1% interest rate)."}
{
:db/ident :skirmish.security/inflation
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db/doc "The daily inflation as a multiple of 0.01% (100 is 1%)."}])
(def ledger-schema
[
@@ -120,7 +125,8 @@
:emitter "dapm private key"
:number-of-decimal 2
:symbol "რ"
:interest-rate 10}
:interest-rate (* 100 10)
:inflation (* 100 9)}
apbm #:skirmish.security{:sec-id :apbm
:name "apache beam"
:type :equity
+82 -9
View File
@@ -12,12 +12,86 @@
[taoensso.timbre :as timbre]
[compojure.core :as compojure]
[clojure.spec.alpha :as s]
[infix.macros :refer [infix]]
[ring.middleware.cors :refer [wrap-cors]]))
;; the ledger is the central
(defn compute-simple-face-value [amount daily-interest-rate deposit-date value-date]
(let [min-in-milliseconds (infix 60 * 1000)
minute-interest-rate (infix daily-interest-rate / (24 * 60. * 10000.))
ite (quot (- (. value-date getTime) (. deposit-date getTime)) min-in-milliseconds)
base (infix 1 + minute-interest-rate)]
(infix amount * base ** ite)))
(defn- in-interval? [[_ start end] x]
(let [x-time (. x getTime)]
(and (<= (. start getTime) x-time)
(< x-time (. end getTime)))))
(defn- larger-then-interval? [[ _ start end] x]
(and (<= (. x getTime) (. end getTime))
(<= (. x getTime) (. start getTime))))
(defn- split-interest-rate-intervals [interest-rates-intervals deposit-date value-date]
(let [xf (comp (drop-while #((comp not in-interval?) % deposit-date))
(take-while #((comp not larger-then-interval?) % value-date)))
ret (into [] xf interest-rates-intervals)
ret (assoc-in ret [0 1] deposit-date)
ret (assoc-in ret [(-> ret count dec) 2] value-date)]
ret))
(defn- find-interest-rate-for [interest-rates-intervals d]
(if (in-interval? (first interest-rates-intervals) d)
(ffirst interest-rates-intervals)
(recur (rest interest-rates-intervals) d)))
(defn split-interest-rates [interest-rates deposit-date value-date]
(let [interest-rates (into [[0 (new java.util.Date 0)]] interest-rates)
interest-rates (conj interest-rates [0 (new java.util.Date Long/MAX_VALUE)])
interest-rates-intervals (partition 2 1 interest-rates)
interest-rates-intervals (map (juxt ffirst (comp second first) (comp second second)) interest-rates-intervals)]
(if (= deposit-date value-date)
[[(find-interest-rate-for interest-rates-intervals deposit-date) deposit-date value-date]]
(split-interest-rate-intervals interest-rates-intervals deposit-date value-date))))
(defn compute-face-value [amount interest-rates deposit-date value-date]
(loop [interest-rates (split-interest-rates interest-rates deposit-date value-date)
amount amount]
(let [[interest-rate start end] (first interest-rates)
amount (compute-simple-face-value amount interest-rate start end)]
(if (empty? (rest interest-rates))
(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)
interest-rates (d/q '[:find ?interest-rate ?v
:in $ ?eid
:where [?eid :skirmish.security/interest-rate ?interest-rate ?tx]
[?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
:sec-id sec-id
:account-public-key (:skirmish.ledger/owner-public-key 333)}))
;; an :account-id is the public key of a
;; an :account-secret is an :acount-id private key
;; an :account-secret is an :account-id private key
(defn get-account-body [request account sec]
(if (nil? sec)
@@ -29,7 +103,6 @@
ret {:account beautify-account :sec-url sec-url}]
(build-response request ret))))
(defn get-account [conn]
"Returns the value of an account."
(fn [{{account-id :account-id} :route-params :as request}]
@@ -74,12 +147,12 @@
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]]
volume (d/q '[:find (sum ?amount) .
:in $ ?id
:where [?e :skirmish.ledger/sec ?id]
[?e :skirmish.ledger/amount ?amount]]
db id)]
db id)]
{:sec-id sec-id
:interest-rate interest-rate
:volume (or volume 0)
@@ -157,7 +230,7 @@
create securities.
payload
[:amount :acount]"
[:amount :account]"
(fn [{{sec-id-str :sec-id} :route-params body :body :as request}]
(try+
(let [emit-request (-> body get-body (get-emit-securities-request (keyword sec-id-str)))]
@@ -171,7 +244,7 @@
(let [sec-id->url #(routes/get-path-for :ledger/account {:account-id (name %)})
to-security-description #(hash-map :account-public-key % :url (sec-id->url %))
xf (comp cat (map to-security-description))
securities-info (sequence xf accounts)];;
securities-info (sequence xf accounts)]
{:accounts securities-info}))
+1 -1
View File
@@ -1,6 +1,6 @@
(ns skirmish.util
(:require [clojure.core.async :as async])
(:import (skirmish PersistentSortedList)))
(:import [ skirmish PersistentSortedList]))
(defn sorted-list [& keys]
(PersistentSortedList/create (seq keys)))
@@ -38,6 +38,8 @@
(defn page [state]
[:div
[:section.section
[:div.container
[:img {:src "/images/amsterdam.svg"}]
[:div.columns
[:div.container
[:img {:src "/images/amsterdam.svg"}]]]
[:div.columns.is-centered
[login state]]]])