Show Swaps fee accurately as <0.01 when very small (#21726)

* fix: use full fiat value when converting from crypto

* test: fixed tests
This commit is contained in:
Lungu Cristian 2024-12-03 11:21:45 +02:00 committed by GitHub
parent 0c25b77e54
commit f7c930964c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 21 deletions

View File

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

View File

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

View File

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