Show fiat value of wallet assets #4009

This commit is contained in:
psdp 2018-04-29 15:36:24 +00:00 committed by Roman Volosovskyi
parent d430d74781
commit 5d5444e3de
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
9 changed files with 115 additions and 46 deletions

View File

@ -95,6 +95,7 @@
:update-wallet :update-wallet
(fn [{{:keys [web3 account/account network network-status] :as db} :db} _] (fn [{{:keys [web3 account/account network network-status] :as db} :db} _]
(let [chain (ethereum/network->chain-keyword network) (let [chain (ethereum/network->chain-keyword network)
mainnet? (= :mainnet chain)
address (:address account) address (:address account)
symbols (get-in account [:settings :wallet :visible-tokens chain])] symbols (get-in account [:settings :wallet :visible-tokens chain])]
(when (not= network-status :offline) (when (not= network-status :offline)
@ -108,8 +109,8 @@
:chain chain :chain chain
:success-event :update-token-balance-success :success-event :update-token-balance-success
:error-event :update-token-balance-fail} :error-event :update-token-balance-fail}
:get-prices {:from "ETH" :get-prices {:from (if mainnet? (conj symbols "ETH") ["ETH"])
:to "USD" :to ["USD"]
:success-event :update-prices-success :success-event :update-prices-success
:error-event :update-prices-fail} :error-event :update-prices-fail}
:db (-> db :db (-> db

View File

@ -134,6 +134,12 @@
:margin-left 16 :margin-left 16
:color colors/gray}) :color colors/gray})
(def asset-item-container
{:flex 1
:flex-direction :row
:align-items :center
:justify-content :space-between})
(def asset-item-value-container (def asset-item-value-container
{:flex 1 {:flex 1
:flex-direction :row :flex-direction :row
@ -148,3 +154,8 @@
{:font-size 16 {:font-size 16
:color colors/gray :color colors/gray
:margin-left 6}) :margin-left 6})
(def asset-item-price
{:font-size 16
:color colors/gray
:margin-left 6})

View File

@ -13,23 +13,49 @@
(fn [wallet] (fn [wallet]
(:balance wallet))) (:balance wallet)))
(re-frame/reg-sub :price (re-frame/reg-sub :prices
(fn [db] (fn [db]
(get-in db [:prices :price]))) (:prices db)))
(re-frame/reg-sub :price
:<- [:prices]
(fn [prices [_ fsym tsym]]
(get-in prices [fsym tsym :price])))
(re-frame/reg-sub :last-day (re-frame/reg-sub :last-day
(fn [db] :<- [:prices]
(get-in db [:prices :last-day]))) (fn [prices [_ fsym tsym]]
(get-in prices [fsym tsym :last-day])))
(re-frame/reg-sub :asset-value
(fn [[_ fsym tsym]]
[(re-frame/subscribe [:balance])
(re-frame/subscribe [:price fsym tsym])])
(fn [[balance price] [_ fsym tsym]]
(when (and balance price)
(-> (money/wei->ether (get balance fsym))
(money/eth->fiat price)
(money/with-precision 2)
str))))
(defn- get-balance-total-value [balance prices currency]
(->> balance
(reduce-kv (fn [acc symbol value]
(if-let [price (get-in prices [symbol currency :price])]
(+ acc (-> (money/wei->ether value)
(money/eth->fiat price)
.toNumber))
acc)) 0)))
(re-frame/reg-sub :portfolio-value (re-frame/reg-sub :portfolio-value
:<- [:balance] :<- [:balance]
:<- [:price] :<- [:prices]
(fn [[balance price]] (fn [[balance prices] [_ currency]]
(if (and balance price) (if (and balance prices)
(-> (money/wei->ether (get balance :ETH)) ;; TODO(jeluard) Modify to consider tokens (let [balance-total-value (get-balance-total-value balance prices currency)]
(money/eth->usd price) (-> balance-total-value
(money/with-precision 2) (money/with-precision 2)
str) str))
"..."))) "...")))
(re-frame/reg-sub :prices-loading? (re-frame/reg-sub :prices-loading?

View File

@ -49,7 +49,8 @@
:action #(re-frame/dispatch [:navigate-to :transactions-history])}]) :action #(re-frame/dispatch [:navigate-to :transactions-history])}])
(defn- render-asset [{:keys [symbol icon decimals amount]}] (defn- render-asset [{:keys [symbol icon decimals amount]}]
[react/view (let [asset-value (re-frame/subscribe [:asset-value symbol :USD])]
[react/view {:style styles/asset-item-container}
[list/item [list/item
[list/item-image icon] [list/item-image icon]
[react/view {:style styles/asset-item-value-container} [react/view {:style styles/asset-item-value-container}
@ -61,9 +62,11 @@
[react/text {:style styles/asset-item-currency [react/text {:style styles/asset-item-currency
:uppercase? true :uppercase? true
:number-of-lines 1} :number-of-lines 1}
(clojure.core/name symbol)]]]]) (clojure.core/name symbol)]]
[react/text {:style styles/asset-item-price
:uppercase? true
:number-of-lines 1}
(if @asset-value (str "$" @asset-value) "...")]]]))
(defn- asset-section [assets] (defn- asset-section [assets]
[react/view styles/asset-section [react/view styles/asset-section
@ -77,7 +80,7 @@
(views/defview wallet [] (views/defview wallet []
(views/letsubs [assets [:wallet/visible-assets-with-amount] (views/letsubs [assets [:wallet/visible-assets-with-amount]
portfolio-value [:portfolio-value]] portfolio-value [:portfolio-value :USD]]
[react/view styles/main-section [react/view styles/main-section
[toolbar-view] [toolbar-view]
[react/scroll-view {:refresh-control [react/scroll-view {:refresh-control

View File

@ -93,9 +93,9 @@
(defn fee-value [gas gas-price] (defn fee-value [gas gas-price]
(.times (bignumber gas) (bignumber gas-price))) (.times (bignumber gas) (bignumber gas-price)))
(defn eth->usd [eth usd-price] (defn eth->fiat [eth fiat-price]
(when-let [bn (bignumber eth)] (when-let [bn (bignumber eth)]
(.times bn (bignumber usd-price)))) (.times bn (bignumber fiat-price))))
(defn percent-change [from to] (defn percent-change [from to]
(let [bnf (bignumber from) (let [bnf (bignumber from)

View File

@ -13,19 +13,22 @@
(def api-url "https://min-api.cryptocompare.com/data") (def api-url "https://min-api.cryptocompare.com/data")
(def status-identifier "extraParams=Status.im") (def status-identifier "extraParams=Status.im")
(defn- gen-price-url [fsyms tsyms] (defn- ->url-param-syms [syms]
(str api-url "/pricemultifull?fsyms=" fsyms "&tsyms=" tsyms "&" status-identifier)) ((comp (partial clojure.string/join ",") (partial map name)) syms))
(defn- format-price-resp [from to resp] (defn- gen-price-url [fsyms tsyms]
(let [raw (:RAW (types/json->clj resp)) (str api-url "/pricemultifull?fsyms=" (->url-param-syms fsyms) "&tsyms=" (->url-param-syms tsyms) "&" status-identifier))
entry (get-in raw [(keyword from) (keyword to)])]
{:from from (defn- format-price-resp [resp]
:to to (into {} (for [[from entries] (:RAW (types/json->clj resp))]
{from (into {} (for [[to entry] entries]
{to {:from (name from)
:to (name to)
:price (:PRICE entry) :price (:PRICE entry)
:last-day (:OPEN24HOUR entry)})) :last-day (:OPEN24HOUR entry)}}))})))
(defn get-prices [from to on-success on-error] (defn get-prices [from to on-success on-error]
(http/get (http/get
(gen-price-url from to) (gen-price-url from to)
(fn [resp] (on-success (format-price-resp from to resp))) (fn [resp] (on-success (format-price-resp resp)))
on-error)) on-error))

View File

@ -5,6 +5,7 @@
[status-im.test.contacts.subs] [status-im.test.contacts.subs]
[status-im.test.accounts.events] [status-im.test.accounts.events]
[status-im.test.data-store.realm.core] [status-im.test.data-store.realm.core]
[status-im.test.wallet.subs]
[status-im.test.wallet.transactions.subs] [status-im.test.wallet.transactions.subs]
[status-im.test.wallet.transactions.views] [status-im.test.wallet.transactions.views]
[status-im.test.profile.events] [status-im.test.profile.events]
@ -27,7 +28,8 @@
[status-im.test.utils.transducers] [status-im.test.utils.transducers]
[status-im.test.utils.async] [status-im.test.utils.async]
[status-im.test.utils.datetime] [status-im.test.utils.datetime]
[status-im.test.utils.mixpanel])) [status-im.test.utils.mixpanel]
[status-im.test.utils.prices]))
(enable-console-print!) (enable-console-print!)
@ -48,6 +50,7 @@
'status-im.test.profile.events 'status-im.test.profile.events
'status-im.test.data-store.realm.core 'status-im.test.data-store.realm.core
'status-im.test.bots.events 'status-im.test.bots.events
'status-im.test.wallet.subs
'status-im.test.wallet.transactions.subs 'status-im.test.wallet.transactions.subs
'status-im.test.wallet.transactions.views 'status-im.test.wallet.transactions.views
'status-im.test.chat.models.input 'status-im.test.chat.models.input
@ -65,4 +68,5 @@
'status-im.test.utils.signing-phrase.core 'status-im.test.utils.signing-phrase.core
'status-im.test.utils.transducers 'status-im.test.utils.transducers
'status-im.test.utils.datetime 'status-im.test.utils.datetime
'status-im.test.utils.mixpanel) 'status-im.test.utils.mixpanel
'status-im.test.utils.prices)

View File

@ -0,0 +1,9 @@
(ns status-im.test.utils.prices
(:require [cljs.test :refer-macros [deftest is]]
[status-im.utils.prices :as prices]))
(deftest test-format-price-resp
(is (= (prices/format-price-resp
"{\"RAW\":{\"ETH\":{\"USD\":{\"TYPE\":\"5\",\"MARKET\":\"CCCAGG\",\"FROMSYMBOL\":\"ETH\",\"TOSYMBOL\":\"USD\",\"FLAGS\":\"4\",\"PRICE\":677.91,\"LASTUPDATE\":1525241472,\"LASTVOLUME\":0.69661268,\"LASTVOLUMETO\":472.0874471092,\"LASTTRADEID\":\"1525241472.6714\",\"VOLUMEDAY\":109859.96594606241,\"VOLUMEDAYTO\":73924561.52673237,\"VOLUME24HOUR\":408811.69032152055,\"VOLUME24HOURTO\":270617692.0100031,\"OPENDAY\":670.81,\"HIGHDAY\":681.06,\"LOWDAY\":662.13,\"OPEN24HOUR\":658.68,\"HIGH24HOUR\":681.67,\"LOW24HOUR\":638.25,\"LASTMARKET\":\"Kraken\",\"CHANGE24HOUR\":19.230000000000018,\"CHANGEPCT24HOUR\":2.919475314264896,\"CHANGEDAY\":7.100000000000023,\"CHANGEPCTDAY\":1.0584219078427608,\"SUPPLY\":99176837.749,\"MKTCAP\":67232970078.42458,\"TOTALVOLUME24H\":1308785.4697758215,\"TOTALVOLUME24HTO\":880718916.8398683}},\"SNT\":{\"USD\":{\"TYPE\":\"5\",\"MARKET\":\"CCCAGG\",\"FROMSYMBOL\":\"SNT\",\"TOSYMBOL\":\"USD\",\"FLAGS\":\"1\",\"PRICE\":0.1562,\"LASTUPDATE\":1525241236,\"LASTVOLUME\":582.07218,\"LASTVOLUMETO\":90.919674516,\"LASTTRADEID\":\"237582608\",\"VOLUMEDAY\":259125.16382211001,\"VOLUMEDAYTO\":40450.5318867195,\"VOLUME24HOUR\":1451022.9449243098,\"VOLUME24HOURTO\":223465.71221695316,\"OPENDAY\":0.1573,\"HIGHDAY\":0.1592,\"LOWDAY\":0.1532,\"OPEN24HOUR\":0.15,\"HIGH24HOUR\":0.1657,\"LOW24HOUR\":0.1481,\"LASTMARKET\":\"Bitfinex\",\"CHANGE24HOUR\":0.006200000000000011,\"CHANGEPCT24HOUR\":4.133333333333341,\"CHANGEDAY\":-0.0010999999999999899,\"CHANGEPCTDAY\":-0.6993006993006928,\"SUPPLY\":6804870174.87817,\"MKTCAP\":1062920721.3159702,\"TOTALVOLUME24H\":239970152.87898657,\"TOTALVOLUME24HTO\":37480153.807917476}}},\"DISPLAY\":{\"ETH\":{\"USD\":{\"FROMSYMBOL\":\"Ξ\",\"TOSYMBOL\":\"$\",\"MARKET\":\"CryptoCompare Index\",\"PRICE\":\"$ 677.91\",\"LASTUPDATE\":\"Just now\",\"LASTVOLUME\":\"Ξ 0.6966\",\"LASTVOLUMETO\":\"$ 472.09\",\"LASTTRADEID\":\"1525241472.6714\",\"VOLUMEDAY\":\"Ξ 109,860.0\",\"VOLUMEDAYTO\":\"$ 73,924,561.5\",\"VOLUME24HOUR\":\"Ξ 408,811.7\",\"VOLUME24HOURTO\":\"$ 270,617,692.0\",\"OPENDAY\":\"$ 670.81\",\"HIGHDAY\":\"$ 681.06\",\"LOWDAY\":\"$ 662.13\",\"OPEN24HOUR\":\"$ 658.68\",\"HIGH24HOUR\":\"$ 681.67\",\"LOW24HOUR\":\"$ 638.25\",\"LASTMARKET\":\"Kraken\",\"CHANGE24HOUR\":\"$ 19.23\",\"CHANGEPCT24HOUR\":\"2.92\",\"CHANGEDAY\":\"$ 7.10\",\"CHANGEPCTDAY\":\"1.06\",\"SUPPLY\":\"Ξ 99,176,837.7\",\"MKTCAP\":\"$ 67.23 B\",\"TOTALVOLUME24H\":\"Ξ 1,308.79 K\",\"TOTALVOLUME24HTO\":\"$ 880.72 M\"}},\"SNT\":{\"USD\":{\"FROMSYMBOL\":\"SNT\",\"TOSYMBOL\":\"$\",\"MARKET\":\"CryptoCompare Index\",\"PRICE\":\"$ 0.1562\",\"LASTUPDATE\":\"3 min ago\",\"LASTVOLUME\":\"SNT 582.07\",\"LASTVOLUMETO\":\"$ 90.92\",\"LASTTRADEID\":\"237582608\",\"VOLUMEDAY\":\"SNT 259,125.2\",\"VOLUMEDAYTO\":\"$ 40,450.5\",\"VOLUME24HOUR\":\"SNT 1,451,022.9\",\"VOLUME24HOURTO\":\"$ 223,465.7\",\"OPENDAY\":\"$ 0.1573\",\"HIGHDAY\":\"$ 0.1592\",\"LOWDAY\":\"$ 0.1532\",\"OPEN24HOUR\":\"$ 0.1500\",\"HIGH24HOUR\":\"$ 0.1657\",\"LOW24HOUR\":\"$ 0.1481\",\"LASTMARKET\":\"Bitfinex\",\"CHANGE24HOUR\":\"$ 0.0062\",\"CHANGEPCT24HOUR\":\"4.13\",\"CHANGEDAY\":\"$ -0.0011\",\"CHANGEPCTDAY\":\"-0.70\",\"SUPPLY\":\"SNT 6,804,870,174.9\",\"MKTCAP\":\"$ 1,062.92 M\",\"TOTALVOLUME24H\":\"SNT 239.97 M\",\"TOTALVOLUME24HTO\":\"$ 37.48 M\"}}}}")
{:ETH {:USD {:from "ETH", :to "USD", :price 677.91, :last-day 658.68}}
:SNT {:USD {:from "SNT", :to "USD", :price 0.1562, :last-day 0.15}}})))

View File

@ -0,0 +1,12 @@
(ns status-im.test.wallet.subs
(:require [cljs.test :refer-macros [deftest is]]
[status-im.utils.money :as money]
[status-im.ui.screens.wallet.subs :as s]))
(deftest test-balance-total-value
(is (= (s/get-balance-total-value {:ETH (money/bignumber 1000000000000000000)
:SNT (money/bignumber 100000000000000000000)}
{:ETH {:USD {:from "ETH", :to "USD", :price 677.91, :last-day 658.688}}
:SNT {:USD {:from "SNT", :to "USD", :price 0.1562, :last-day 0.15}}}
:USD)
693.53)))