Prices handling in commands impl

This commit is contained in:
janherich 2018-09-10 10:52:48 +02:00
parent f240a6bef9
commit 988a1ca2e2
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
4 changed files with 21 additions and 51 deletions

View File

@ -243,19 +243,19 @@
:gas (ethereum/estimate-gas symbol)
:from-chat? true)))
(defn- inject-network-price-info [{:keys [amount asset] :as parameters} {:keys [db]}]
(let [{:keys [chain prices]} db
currency (-> db
currency-settings.subs/get-currency
name
string/upper-case)]
(defn- inject-network-info [parameters {:keys [db]}]
(assoc parameters :network (:chain db)))
(defn- inject-price-info [{:keys [amount asset] :as parameters} {:keys [db]}]
(let [currency (-> db
currency-settings.subs/get-currency
name
string/upper-case)]
(assoc parameters
:network chain
;; TODO(janherich) - shouldn't this be rather computed on the receiver side ?
:fiat-amount (money/fiat-amount-value amount
:fiat-amount (money/fiat-amount-value (string/replace amount #"," ".")
(keyword asset)
(keyword currency)
prices)
(:prices db))
:currency currency)))
(defn- params-unchanged? [send-message request-message]
@ -316,8 +316,10 @@
:edit? false}}))
protocol/EnhancedParameters
(enhance-send-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx))
(enhance-receive-parameters [_ _ _]))
(-> (inject-network-info parameters cofx)
(inject-price-info cofx)))
(enhance-receive-parameters [_ parameters cofx]
(inject-price-info parameters cofx)))
;; `/request` command
@ -445,5 +447,7 @@
(request-preview command-message))
protocol/EnhancedParameters
(enhance-send-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx))
(enhance-receive-parameters [_ _ _]))
(-> (inject-network-info parameters cofx)
(inject-price-info cofx)))
(enhance-receive-parameters [_ parameters cofx]
(inject-price-info parameters cofx)))

View File

@ -29,9 +29,9 @@
If the message is not of the command type, or command doesn't implement the
`EnhancedParameters` protocol, returns unaltered message, otherwise updates
its parameters."
[message {:keys [db] :as cofx}]
[{:keys [content] :as message} {:keys [db] :as cofx}]
(let [id->command (:id->command db)
{:keys [type content]} (lookup-command-by-ref message id->command)]
{:keys [type]} (lookup-command-by-ref message id->command)]
(if-let [new-params (and (satisfies? protocol/EnhancedParameters type)
(protocol/enhance-receive-parameters type (:params content) cofx))]
(assoc-in message [:content :params] new-params)

View File

@ -97,28 +97,11 @@
(defn add-outgoing-status [{:keys [from] :as message} {:keys [db]}]
(assoc message :outgoing (= from (:current-public-key db))))
(defn set-fiat-amount [{:keys [content-type content] :as message} cofx]
(if (= constants/content-type-command content-type)
(let [account-currency (-> cofx
(get-in [:db :account/account :settings :wallet :currency] :usd)
name
string/upper-case)
{:keys [amount asset]} (:params content)
prices (get-in cofx [:db :prices])]
(-> message
(assoc-in [:content :params :fiat-amount] (money/fiat-amount-value amount
(keyword asset)
(keyword account-currency)
prices))
(assoc-in [:content :params :currency] account-currency)))
message))
(defn- add-message
[batch? {:keys [chat-id message-id clock-value content] :as message} current-chat? {:keys [db] :as cofx}]
(let [prepared-message (-> message
(prepare-message chat-id current-chat?)
(add-outgoing-status cofx)
(set-fiat-amount cofx))]
(add-outgoing-status cofx))]
(let [fx {:db (cond->
(-> db
(update-in [:chats chat-id :messages] assoc message-id prepared-message)

View File

@ -169,20 +169,3 @@
(get-in fx2 [:db :chats "chat-id" :messages])))
(is (= {}
(get-in fx2 [:db :chats "chat-id" :message-groups]))))))
(deftest set-fiat-amount
(let [message {:content-type "text/plain"}
command-message {:content-type "command"
:content {:params {:amount "2"
:asset "ETH"}}}
cofx {:db {:prices {:ETH {:EUR {:from "ETH"
:to "EUR"
:price 300
:last-day 299}}}
:account/account {:settings {:wallet {:currency :eur}}}}}]
(testing "Not a command message"
(is (= message (message/set-fiat-amount message cofx))))
(testing "Command message"
(is (= (-> command-message
(assoc-in [:content :params :fiat-amount] "600")
(assoc-in [:content :params :currency] "EUR")) (message/set-fiat-amount command-message cofx))))))