diff --git a/src/status_im/contexts/wallet/common/utils.cljs b/src/status_im/contexts/wallet/common/utils.cljs index 0bbbcfca27..60119b80a1 100644 --- a/src/status_im/contexts/wallet/common/utils.cljs +++ b/src/status_im/contexts/wallet/common/utils.cljs @@ -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 diff --git a/src/status_im/contexts/wallet/common/utils_test.cljs b/src/status_im/contexts/wallet/common/utils_test.cljs index 4bbb654f3c..e33aa0a15e 100644 --- a/src/status_im/contexts/wallet/common/utils_test.cljs +++ b/src/status_im/contexts/wallet/common/utils_test.cljs @@ -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)))) + diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 6669499401..a9384fce66 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -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