Skip to content

Commit

Permalink
Wallet: crypto max decimals (#18267)
Browse files Browse the repository at this point in the history
* wallet: crypto max decimals
  • Loading branch information
OmarBasem authored Jan 3, 2024
1 parent 006b637 commit f2c69ab
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/status_im/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@
(map (comp :raw-balance val))
(reduce money/add)))

(defn extract-exponent
[s]
(if-let [index (string/index-of s "e")]
(subs s (+ index 2))
nil))

(defn calc-max-crypto-decimals
[value]
(let [str-representation (str value)
decimal-part (second (clojure.string/split str-representation #"\."))
exponent (extract-exponent str-representation)
zeroes-count (count (take-while #(= \0 %) decimal-part))
max-decimals (or exponent zeroes-count)
first-non-zero-digit (first (filter #(not (= \0 %)) decimal-part))]
(if (= \1 first-non-zero-digit)
(inc max-decimals)
max-decimals)))

(defn get-standard-crypto-format
"For full details: https://github.com/status-im/status-mobile/issues/18225"
[{:keys [market-values-per-currency]} token-units]
(let [price (get-in market-values-per-currency [:usd :price])
one-cent-value (if (pos? price) (/ 0.01 price) 0)
decimals-count (calc-max-crypto-decimals one-cent-value)]
(if (< token-units one-cent-value)
(str "<" (.toFixed one-cent-value decimals-count))
(.toFixed token-units decimals-count))))

(defn total-token-units-in-all-chains
[{:keys [balances-per-chain decimals] :as _token}]
(-> balances-per-chain
Expand Down
15 changes: 15 additions & 0 deletions src/status_im/contexts/wallet/common/utils_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@

(is (= (utils/get-wallet-qr wallet-singlechain)
"x000")))))

(deftest test-extract-exponent
(testing "extract-exponent function"
(is (= (utils/extract-exponent "123.456") nil))
(is (= (utils/extract-exponent "2.5e-2") "2"))
(is (= (utils/extract-exponent "4.567e-10") "10"))))

(deftest test-calc-max-crypto-decimals
(testing "calc-max-crypto-decimals function"
(is (= (utils/calc-max-crypto-decimals 0.00323) 2))
(is (= (utils/calc-max-crypto-decimals 0.00123) 3))
(is (= (utils/calc-max-crypto-decimals 0.00000423) 5))
(is (= (utils/calc-max-crypto-decimals 2.23e-6) 5))
(is (= (utils/calc-max-crypto-decimals 1.13e-6) 6))))

10 changes: 7 additions & 3 deletions src/status_im/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@
currency
(get market-values-per-currency
constants/profile-default-currency))
{:keys [change-pct-24hour]} market-values]
{:keys [change-pct-24hour]} market-values
crypto-value (utils/get-standard-crypto-format token token-units)
fiat-value (if (string/includes? crypto-value "<")
"<$0.01"
(utils/prettify-balance currency-symbol fiat-value))]
{:token (:symbol token)
:token-name (:name token)
:state :default
Expand All @@ -180,8 +184,8 @@
(neg? change-pct-24hour) :negative
:else :empty)
:customization-color color
:values {:crypto-value token-units
:fiat-value (utils/prettify-balance currency-symbol fiat-value)}}))
:values {:crypto-value crypto-value
:fiat-value fiat-value}}))

(rf/reg-sub
:wallet/account-token-values
Expand Down

0 comments on commit f2c69ab

Please sign in to comment.