From f7c930964cb678c0fadd4926aa736978a12a5ad0 Mon Sep 17 00:00:00 2001 From: Lungu Cristian Date: Tue, 3 Dec 2024 11:21:45 +0200 Subject: [PATCH] Show Swaps fee accurately as <0.01 when very small (#21726) * fix: use full fiat value when converting from crypto * test: fixed tests --- .../contexts/wallet/common/utils.cljs | 28 +++++++++---------- .../contexts/wallet/common/utils_test.cljs | 10 +++---- src/utils/money.cljs | 3 +- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/status_im/contexts/wallet/common/utils.cljs b/src/status_im/contexts/wallet/common/utils.cljs index 2397729bb9..669e65a50f 100644 --- a/src/status_im/contexts/wallet/common/utils.cljs +++ b/src/status_im/contexts/wallet/common/utils.cljs @@ -178,20 +178,6 @@ (let [price (get-market-value currency token prices-per-token)] (money/crypto->fiat balance price))) -(defn formatted-token-fiat-value - "Converts a token balance into its equivalent fiat value, formatted with a currency symbol. - If the fiat value is below $0.01, it returns a <$0.01 string" - [{:keys [currency currency-symbol balance token prices-per-token]}] - (let [price (or (get-market-value currency token prices-per-token) 0) - price-zero? (zero? price) - balance (or balance 0) - balance-positive? (pos? balance) - fiat-value (money/crypto->fiat balance price) - fiat-value-zero? (money/equal-to fiat-value (money/bignumber 0))] - (if (and fiat-value-zero? (not price-zero?) balance-positive?) - (number/small-number-threshold 2 currency-symbol) - (str currency-symbol fiat-value)))) - (defn sanitized-token-amount-to-display "Formats a token amount to a specified number of decimals. Returns a threshold value if the formatted amount is less than the minimum value represented by the decimals." @@ -279,6 +265,20 @@ money/absolute-value (money/to-fixed 2))) +(defn formatted-token-fiat-value + "Converts a token balance into its equivalent fiat value, formatted with a currency symbol. + If the fiat value is below $0.01, it returns a <$0.01 string" + [{:keys [currency currency-symbol balance token prices-per-token]}] + (let [price (or (get-market-value currency token prices-per-token) 0) + price-zero? (zero? price) + balance (or balance 0) + balance-positive? (pos? balance) + fiat-value (money/crypto->fiat balance price) + fiat-value-zero? (money/equal-to fiat-value (money/bignumber 0))] + (if (and fiat-value-zero? (not price-zero?) balance-positive?) + (number/small-number-threshold 2 currency-symbol) + (fiat-formatted-for-ui currency-symbol fiat-value)))) + (defn calculate-token-value "This function returns token values in the props of token-value (quo) component" [{:keys [token color currency currency-symbol prices-per-token market-values-per-token]}] diff --git a/src/status_im/contexts/wallet/common/utils_test.cljs b/src/status_im/contexts/wallet/common/utils_test.cljs index 247348ca65..360f211b23 100644 --- a/src/status_im/contexts/wallet/common/utils_test.cljs +++ b/src/status_im/contexts/wallet/common/utils_test.cljs @@ -185,16 +185,16 @@ :balance 0.5 :token {:symbol "ETH"} :prices-per-token {:ETH {:usd 2000}}}] - (is (= (utils/formatted-token-fiat-value default-params) "$1000")) - (is (= (utils/formatted-token-fiat-value (assoc default-params :balance 0)) "$0")) + (is (= (utils/formatted-token-fiat-value default-params) "$1000.00")) + (is (= (utils/formatted-token-fiat-value (assoc default-params :balance 0)) "$0.00")) (is (= (utils/formatted-token-fiat-value (assoc default-params :balance 0.000001)) "<$0.01")) - (is (= (utils/formatted-token-fiat-value (assoc default-params :balance nil)) "$0")) + (is (= (utils/formatted-token-fiat-value (assoc default-params :balance nil)) "$0.00")) (is (= (utils/formatted-token-fiat-value (assoc default-params :balance 1 :prices-per-token {:ETH {:usd nil}})) - "$0")) + "$0.00")) (is (= (utils/formatted-token-fiat-value (assoc default-params :balance 1 :prices-per-token {:ETH {:usd 0}})) - "$0"))))) + "$0.00"))))) (deftest sanitized-token-amount-to-display-test (testing "sanitized-token-amount-to-display function" diff --git a/src/utils/money.cljs b/src/utils/money.cljs index cb523bedb4..70b2c0e930 100644 --- a/src/utils/money.cljs +++ b/src/utils/money.cljs @@ -242,8 +242,7 @@ ^js fiat-price-bn (bignumber fiat-price)] (when (and crypto-bn fiat-price-bn) (-> crypto-bn - (.times fiat-price-bn) - (with-precision 2))))) + (.times fiat-price-bn))))) (defn above-zero? [^js balance]