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])))
(re-frame/reg-sub :asset-value
(fn [[_ fsym tsym]]
(fn [[_ fsym decimals tsym]]
[(re-frame/subscribe [:balance])
(re-frame/subscribe [:price fsym tsym])
(re-frame/subscribe [:wallet/currency])])
(fn [[balance price currency] [_ fsym tsym]]
(fn [[balance price currency] [_ fsym decimals tsym]]
(when (and balance price)
(-> (money/wei->ether (get balance fsym))
(money/eth->fiat price)
(-> (money/internal->formatted (get balance fsym) fsym decimals)
(money/crypto->fiat price)
(money/with-precision 2)
str
(i18n/format-currency (:code currency))))))
(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)))
(defn- get-balance-total-value [balance prices currency token->decimals]
(reduce-kv (fn [acc symbol value]
(if-let [price (get-in prices [symbol currency :price])]
(+ acc (-> (money/internal->formatted value symbol (token->decimals symbol))
(money/crypto->fiat price)
.toNumber))
acc)) 0 balance))
(re-frame/reg-sub :portfolio-value
:<- [:balance]
:<- [:prices]
:<- [:wallet/currency]
(fn [[balance prices currency] [_ currency-code]]
:<- [:network]
(fn [[balance prices currency network] [_ currency-code]]
(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
prices
(or currency-code
(-> currency :code keyword)))]
(-> currency :code keyword))
token->decimals)]
(-> balance-total-value
(money/with-precision 2)
str

View File

@ -61,7 +61,7 @@
(defn- render-asset [currency]
(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}
[list/item
[list/item-image icon]

View File

@ -119,8 +119,8 @@
(defn fee-value [gas gas-price]
(.times (bignumber gas) (bignumber gas-price)))
(defn eth->fiat [eth fiat-price]
(when-let [bn (bignumber eth)]
(defn crypto->fiat [crypto fiat-price]
(when-let [bn (bignumber crypto)]
(.times bn (bignumber fiat-price))))
(defn percent-change [from to]

View File

@ -5,8 +5,13 @@
(deftest test-balance-total-value
(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}}
:SNT {:USD {:from "SNT", :to "USD", :price 0.1562, :last-day 0.15}}}
:USD)
693.53)))
:SNT {:USD {:from "SNT", :to "USD", :price 0.1562, :last-day 0.15}}
:AST {:USD {:from "AST", :to "USD", :price 4, :last-day 3}}}
:USD
{:ETH 18
:SNT 18
:AST 4})
697.53)))