Use timestamps for time measurement

See commit 5ed054babb228c80fa1 in Ercoin.
parent 8234a772
Pipeline #36050942 passed with stages
in 1 minute and 49 seconds
......@@ -10,6 +10,7 @@
[ercoin-wallet.binary :as binary]
[ercoin-wallet.converter :as converter]
[ercoin-wallet.tendermint :as tendermint]
[ercoin-wallet.timestamp :as timestamp]
[ercoin-wallet.tx :as tx]
[ercoin-wallet.uint :as uint]
[ercoin-wallet.units :as units]))
......@@ -102,8 +103,8 @@
(when private-key
[hidden-key private-key])]
[:td (unit-encode balance)]
[:td valid-until]
[:td locked-until]
[:td (timestamp/format valid-until)]
[:td (timestamp/format locked-until)]
[:td
[:code (binary-encode validator-address)]]
[:td
......@@ -199,11 +200,6 @@
:on-change #(reset! amount-atom (-> % .-target .-value (unit-decode)))}]
" " symbol])))
(defn auto-valid-since [valid-since-atom]
(go
(let [current-height (<! (tendermint/height @rpc-endpoint))]
(reset! valid-since-atom (+ current-height 1)))))
(defn broadcast-tx [tx]
(go
(let [result (<! (tendermint/request
......@@ -212,15 +208,46 @@
{:tx (str "0x" (hex/encode (clj->js tx)))}))]
(js/alert result))))
(defn valid-since-input [valid-since-atom]
[:p
[:label "Valid since"]
[:input {:type :number
:value @valid-since-atom
:on-change #(reset! valid-since-atom (-> % .-target .-value))}]
[:input {:type :button
:value "Auto"
:on-click #(auto-valid-since valid-since-atom)}]])
(defn timestamp-field
([timestamp-atom] (timestamp-field timestamp-atom "Timestamp"))
([timestamp-atom label] (timestamp-field
timestamp-atom
label
[["Now" timestamp/now]]))
([timestamp-atom label predefined-values]
(let [change-atom (r/atom 1)
interval-atom (r/atom (* 24 3600))]
(fn []
[:p
[:label label]
" "
[:span (timestamp/format @timestamp-atom)]
(for [[value-label value-func] predefined-values]
^{:key value-label}
[:input {:type :button
:value value-label
:on-click #(reset! timestamp-atom (value-func))}])
[:input {:type :number
:value @change-atom
:on-change #(reset! change-atom (-> % .-target .-value))}]
[:select {:defaultValue @interval-atom
:on-change #(reset! interval-atom (-> % .-target .-value js/parseInt))}
(for [[name interval] [["second" 1]
["minute" 60]
["hour" 3600]
["day" (* 24 3600)]
["30 days" (* 30 24 3600)]
["365 days" (* 365 24 3600)]]]
^{:key interval}
[:option {:value interval}
name])]
[:input {:type :button
:value "+"
:on-click (fn [e] (swap! timestamp-atom #(+ % (* @change-atom @interval-atom))))}]
[:input {:type :button
:value "−"
:on-click (fn [e] (swap! timestamp-atom #(- % (* @change-atom @interval-atom))))}]
]))))
(defn generated-tx [tx-atom]
(when @tx-atom
......@@ -238,7 +265,7 @@
to (r/atom nil)
amount (r/atom nil)
message (r/atom "")
valid-since (r/atom nil)
timestamp (r/atom nil)
tx (r/atom nil)]
(fn []
[:div
......@@ -250,10 +277,10 @@
[:input {:type :text
:value @message
:on-change #(reset! message (-> % .-target .-value))}]]
[valid-since-input valid-since]
[timestamp-field timestamp]
[:p
[:input {:type :button
:on-click #(reset! tx (tx/transfer-tx @valid-since @from @to @amount @message))
:on-click #(reset! tx (tx/transfer-tx @timestamp @from @to @amount @message))
:value "Generate"}]]
[generated-tx tx]
])))
......@@ -265,11 +292,10 @@
tx (r/atom nil)]
(fn []
[:div
[:p
[:label "Valid until"]
[:input {:type :number
:value @valid-until
:on-change #(reset! valid-until (-> % .-target .-value js/parseInt))}]]
[timestamp-field valid-until "Valid until" [["Current" (fn []
(or (::a/valid-until @from)
(timestamp/now)))]
["Now" timestamp/now]]]
(account-select "From" from)
(account-select "To" to)
[:p
......@@ -285,11 +311,10 @@
tx (r/atom nil)]
(fn []
[:div
[:p
[:label "Locked until"]
[:input {:type :number
:value @locked-until
:on-change #(reset! locked-until (-> % .-target .-value js/parseInt))}]]
[timestamp-field locked-until "Locked until" [["Current" (fn []
(or (::a/locked-until @account)
(timestamp/now)))]
["Now" timestamp/now]]]
(account-select "Account" account)
(account-select "Validator" validator)
[:p
......@@ -300,7 +325,7 @@
(defn vote-tx-form []
(let [validator (r/atom nil)
valid-since (r/atom nil)
timestamp (r/atom nil)
per-tx (r/atom nil)
per-256b (r/atom nil)
per-account-day (r/atom nil)
......@@ -316,11 +341,11 @@
[:label "Protocol"]
[:input {:type :number
:value @protocol
:readonly true}]]
[valid-since-input valid-since]
:readOnly true}]]
[timestamp-field timestamp]
[:p
[:input {:type :button
:on-click #(reset! tx (tx/vote-tx @valid-since @validator @per-tx @per-256b @per-account-day @protocol))
:on-click #(reset! tx (tx/vote-tx @timestamp @validator @per-tx @per-256b @per-account-day @protocol))
:value "Generate"}]]
[generated-tx tx]
])))
......@@ -328,7 +353,7 @@
(defn genesis-tx-form []
(let [from (r/atom nil)
locked (r/atom false)
locked-until (r/atom nil)
locked-for (r/atom nil)
validator (r/atom nil)
tx (r/atom nil)]
(fn []
......@@ -339,19 +364,19 @@
[:input {:type :checkbox
:on-change #(do
(reset! locked (-> % .-target .-checked))
(reset! locked-until nil))}]]
(reset! locked-for nil))}]]
(when @locked
[:div
[:p
[:label "Locked until"]
[:label "Locked for"]
[:input {:type :number
:min 1
:max (- (js/Math.pow 2 24) 1)
:on-change #(reset! locked-until (-> % .-target .-value js/parseInt))}]]
:on-change #(reset! locked-for (-> % .-target .-value js/parseInt))}]]
(account-select "Validator" validator)])
[:p
[:input {:type :button
:on-click #(reset! tx (tx/genesis-tx @from @locked-until @validator))
:on-click #(reset! tx (tx/genesis-tx @from @locked-for @validator))
:value "Generate"}]]
(when @tx
[:div
......
(ns ercoin-wallet.timestamp)
(defn now []
(quot (js/Date.now) 1000))
(defn format [timestamp]
(if timestamp
(.toISOString (js/Date. (* timestamp 1000)))
"none"))
......@@ -15,11 +15,11 @@
(vec (concat binary
(sign-detached binary account))))
(defn transfer-tx [valid-since from to amount message]
(defn transfer-tx [timestamp from to amount message]
(let [message-bytes (uint/str->utf8 message)
to-sign (concat
[0]
(uint/uint->vec valid-since 4)
(uint/uint->vec timestamp 4)
(::a/address from)
(::a/address to)
(uint/uint->vec amount 8)
......@@ -43,10 +43,10 @@
(::a/address validator))]
(sign to-sign account)))
(defn vote-tx [valid-since validator per-tx per-256b per-account-day protocol]
(defn vote-tx [timestamp validator per-tx per-256b per-account-day protocol]
(-> (concat
[3]
(uint/uint->vec valid-since 4)
(uint/uint->vec timestamp 4)
(::a/address validator)
(uint/uint->vec per-tx 8)
(uint/uint->vec per-256b 8)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment