bug #4496 - taking decimals into account when calculating fiat value of assets

This commit is contained in:
Goran Jovic 2018-05-29 12:50:59 +02:00 committed by Roman Volosovskyi
parent 40e6f361c7
commit 6f8647e7d7
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
4 changed files with 30 additions and 22 deletions

View File

@ -30,38 +30,41 @@
(get-in prices [fsym tsym :last-day]))) (get-in prices [fsym tsym :last-day])))
(re-frame/reg-sub :asset-value (re-frame/reg-sub :asset-value
(fn [[_ fsym tsym]] (fn [[_ fsym decimals tsym]]
[(re-frame/subscribe [:balance]) [(re-frame/subscribe [:balance])
(re-frame/subscribe [:price fsym tsym]) (re-frame/subscribe [:price fsym tsym])
(re-frame/subscribe [:wallet/currency])]) (re-frame/subscribe [:wallet/currency])])
(fn [[balance price currency] [_ fsym tsym]] (fn [[balance price currency] [_ fsym decimals tsym]]
(when (and balance price) (when (and balance price)
(-> (money/wei->ether (get balance fsym)) (-> (money/internal->formatted (get balance fsym) fsym decimals)
(money/eth->fiat price) (money/crypto->fiat price)
(money/with-precision 2) (money/with-precision 2)
str str
(i18n/format-currency (:code currency)))))) (i18n/format-currency (:code currency))))))
(defn- get-balance-total-value [balance prices currency] (defn- get-balance-total-value [balance prices currency token->decimals]
(->> balance (reduce-kv (fn [acc symbol value]
(reduce-kv (fn [acc symbol value] (if-let [price (get-in prices [symbol currency :price])]
(if-let [price (get-in prices [symbol currency :price])] (+ acc (-> (money/internal->formatted value symbol (token->decimals symbol))
(+ acc (-> (money/wei->ether value) (money/crypto->fiat price)
(money/eth->fiat price) .toNumber))
.toNumber)) acc)) 0 balance))
acc)) 0)))
(re-frame/reg-sub :portfolio-value (re-frame/reg-sub :portfolio-value
:<- [:balance] :<- [:balance]
:<- [:prices] :<- [:prices]
:<- [:wallet/currency] :<- [:wallet/currency]
(fn [[balance prices currency] [_ currency-code]] :<- [:network]
(fn [[balance prices currency network] [_ currency-code]]
(if (and balance prices) (if (and balance prices)
(let [balance-total-value (let [assets (tokens/tokens-for (ethereum/network->chain-keyword network))
token->decimals (into {} (map #(vector (:symbol %) (:decimals %)) assets))
balance-total-value
(get-balance-total-value balance (get-balance-total-value balance
prices prices
(or currency-code (or currency-code
(-> currency :code keyword)))] (-> currency :code keyword))
token->decimals)]
(-> balance-total-value (-> balance-total-value
(money/with-precision 2) (money/with-precision 2)
str str

View File

@ -61,7 +61,7 @@
(defn- render-asset [currency] (defn- render-asset [currency]
(fn [{:keys [symbol icon decimals amount]}] (fn [{:keys [symbol icon decimals amount]}]
(let [asset-value (re-frame/subscribe [:asset-value symbol (-> currency :code keyword)])] (let [asset-value (re-frame/subscribe [:asset-value symbol decimals (-> currency :code keyword)])]
[react/view {:style styles/asset-item-container} [react/view {:style styles/asset-item-container}
[list/item [list/item
[list/item-image icon] [list/item-image icon]

View File

@ -119,8 +119,8 @@
(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->fiat [eth fiat-price] (defn crypto->fiat [crypto fiat-price]
(when-let [bn (bignumber eth)] (when-let [bn (bignumber crypto)]
(.times bn (bignumber fiat-price)))) (.times bn (bignumber fiat-price))))
(defn percent-change [from to] (defn percent-change [from to]

View File

@ -5,8 +5,13 @@
(deftest test-balance-total-value (deftest test-balance-total-value
(is (= (s/get-balance-total-value {:ETH (money/bignumber 1000000000000000000) (is (= (s/get-balance-total-value {:ETH (money/bignumber 1000000000000000000)
:SNT (money/bignumber 100000000000000000000)} :SNT (money/bignumber 100000000000000000000)
:AST (money/bignumber 10000)}
{:ETH {:USD {:from "ETH", :to "USD", :price 677.91, :last-day 658.688}} {: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}}} :SNT {:USD {:from "SNT", :to "USD", :price 0.1562, :last-day 0.15}}
:USD) :AST {:USD {:from "AST", :to "USD", :price 4, :last-day 3}}}
693.53))) :USD
{:ETH 18
:SNT 18
:AST 4})
697.53)))