Wallet: crypto max decimals (#18267)

* wallet: crypto max decimals
This commit is contained in:
Omar Basem 2024-01-03 15:48:04 +04:00 committed by GitHub
parent 006b637b1b
commit f2c69ab9a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 3 deletions

View File

@ -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

View File

@ -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))))

View File

@ -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
@ -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