Wallet: Utility Tests (#18371)

* Wallet: utility tests
This commit is contained in:
Omar Basem 2024-01-05 16:29:26 +04:00 committed by GitHub
parent be2d0feda3
commit c6a7808049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 17 deletions

View File

@ -31,7 +31,7 @@
(let [path (get-derivation-path number-of-accounts)] (let [path (get-derivation-path number-of-accounts)]
(format-derivation-path path))) (format-derivation-path path)))
(defn- total-raw-balance-in-all-chains (defn total-raw-balance-in-all-chains
[balances-per-chain] [balances-per-chain]
(->> balances-per-chain (->> balances-per-chain
(map (comp :raw-balance val)) (map (comp :raw-balance val))
@ -55,15 +55,27 @@
(inc max-decimals) (inc max-decimals)
max-decimals))) max-decimals)))
(defn remove-trailing-zeroes
[num]
(let [parts (clojure.string/split (str num) #"\.")]
(str (first parts)
(if-let [decimals (second parts)]
(if (seq (clojure.string/replace decimals #"0+$" ""))
(str "." (clojure.string/replace decimals #"0+$" ""))
"")
""))))
(defn get-standard-crypto-format (defn get-standard-crypto-format
"For full details: https://github.com/status-im/status-mobile/issues/18225" "For full details: https://github.com/status-im/status-mobile/issues/18225"
[{:keys [market-values-per-currency]} token-units] [{:keys [market-values-per-currency]} token-units]
(let [price (get-in market-values-per-currency [:usd :price]) (let [price (get-in market-values-per-currency [:usd :price])
one-cent-value (if (pos? price) (/ 0.01 price) 0) one-cent-value (if (pos? price) (/ 0.01 price) 0)
decimals-count (calc-max-crypto-decimals one-cent-value)] decimals-count (calc-max-crypto-decimals one-cent-value)]
(if (money/equal-to token-units 0)
"0"
(if (< token-units one-cent-value) (if (< token-units one-cent-value)
(str "<" (.toFixed one-cent-value decimals-count)) (str "<" (remove-trailing-zeroes (.toFixed one-cent-value decimals-count)))
(.toFixed token-units decimals-count)))) (remove-trailing-zeroes (.toFixed token-units decimals-count))))))
(defn total-token-units-in-all-chains (defn total-token-units-in-all-chains
[{:keys [balances-per-chain decimals] :as _token}] [{:keys [balances-per-chain decimals] :as _token}]

View File

@ -1,21 +1,48 @@
(ns status-im.contexts.wallet.common.utils-test (ns status-im.contexts.wallet.common.utils-test
(:require [cljs.test :refer [deftest is testing]] (:require [cljs.test :refer [deftest is testing]]
[status-im.contexts.wallet.common.utils :as utils])) [status-im.contexts.wallet.common.utils :as utils]
[utils.money :as money]))
(deftest test-get-wallet-qr
(testing "Test get-wallet-qr function"
(let [wallet-multichain {:wallet-type :wallet-multichain
:selected-networks [:ethereum :optimism]
:address "x000"}
wallet-singlechain {:wallet-type :wallet-singlechain
:selected-networks [:ethereum :optimism]
:address "x000"}]
(is (= (utils/get-wallet-qr wallet-multichain) (deftest test-get-first-name
"eth:opt:x000")) (testing "get-first-name function"
(is (= (utils/get-first-name "John Doe") "John"))
(is (= (utils/get-first-name "Jane Smith xyz") "Jane"))))
(is (= (utils/get-wallet-qr wallet-singlechain) (deftest test-prettify-balance
"x000"))))) (testing "prettify-balance function"
(is (= (utils/prettify-balance "$" 100) "$100.00"))
(is (= (utils/prettify-balance "$" 0.5) "$0.50"))
(is (= (utils/prettify-balance "$" 0) "$0.00"))
(is (= (utils/prettify-balance "$" nil) "$0.00"))
(is (= (utils/prettify-balance "$" "invalid input") "$0.00"))))
(deftest test-get-derivation-path
(testing "get-derivation-path function"
(is (= (utils/get-derivation-path 5) "m/44'/60'/0'/0/5"))
(is (= (utils/get-derivation-path 0) "m/44'/60'/0'/0/0"))
(is (= (utils/get-derivation-path 123) "m/44'/60'/0'/0/123"))))
(deftest test-format-derivation-path
(testing "format-derivation-path function"
(is (= (utils/format-derivation-path "m/44'/60'/0'/0/5") "m / 44' / 60' / 0' / 0 / 5"))
(is (= (utils/format-derivation-path "m/44'/60'/0'/0/0") "m / 44' / 60' / 0' / 0 / 0"))
(is (= (utils/format-derivation-path "m/44'/60'/0'/0/123") "m / 44' / 60' / 0' / 0 / 123"))))
(deftest test-get-formatted-derivation-path
(testing "get-formatted-derivation-path function"
(is (= (utils/get-formatted-derivation-path 5) "m / 44' / 60' / 0' / 0 / 5"))
(is (= (utils/get-formatted-derivation-path 0) "m / 44' / 60' / 0' / 0 / 0"))
(is (= (utils/get-formatted-derivation-path 123) "m / 44' / 60' / 0' / 0 / 123"))))
(deftest test-total-raw-balance-in-all-chains
(testing "total-raw-balance-in-all-chains function"
(let [balances-per-chain {1 {:raw-balance (money/bignumber 100)}
10 {:raw-balance (money/bignumber 200)}
42161 {:raw-balance (money/bignumber 300)}}]
(is (money/equal-to (utils/total-raw-balance-in-all-chains balances-per-chain)
(money/bignumber 600))))))
(deftest test-extract-exponent (deftest test-extract-exponent
(testing "extract-exponent function" (testing "extract-exponent function"
@ -31,3 +58,68 @@
(is (= (utils/calc-max-crypto-decimals 2.23e-6) 5)) (is (= (utils/calc-max-crypto-decimals 2.23e-6) 5))
(is (= (utils/calc-max-crypto-decimals 1.13e-6) 6)))) (is (= (utils/calc-max-crypto-decimals 1.13e-6) 6))))
(deftest test-get-standard-crypto-format
(testing "get-standard-crypto-format function"
(let [market-values-per-currency {:usd {:price 100}}
token-units (money/bignumber 0.005)]
(is (= (utils/get-standard-crypto-format {:market-values-per-currency market-values-per-currency}
token-units)
"0.005")))
(let [market-values-per-currency {:usd {:price 0.005}}
token-units (money/bignumber 0.01)]
(is (= (utils/get-standard-crypto-format {:market-values-per-currency market-values-per-currency}
token-units)
"<2")))))
(deftest test-total-token-units-in-all-chains
(testing "total-token-units-in-all-chains function"
(let [token {:balances-per-chain {1 {:raw-balance (money/bignumber 100)}
10 {:raw-balance (money/bignumber 200)}
42161 {:raw-balance (money/bignumber 300)}}
:decimals 2}]
(is (money/equal-to (utils/total-token-units-in-all-chains token) 6.0)))))
(deftest test-get-account-by-address
(testing "get-account-by-address function"
(let [accounts [{:address "0x123"}
{:address "0x456"}
{:address "0x789"}]
address-to-find "0x456"]
(is (= (utils/get-account-by-address accounts address-to-find) {:address "0x456"})))
(let [accounts [{:address "0x123"}
{:address "0x456"}
{:address "0x789"}]
address-to-find "0x999"]
(is (= (utils/get-account-by-address accounts address-to-find) nil)))))
(deftest test-calculate-raw-balance
(testing "calculate-raw-balance function"
(is (= (utils/calculate-raw-balance "100000000" "8") 1.0))
(is (= (utils/calculate-raw-balance "50000000" "8") 0.5))
(is (= (utils/calculate-raw-balance "123456789" "2") 1234567.89))
(is (= (utils/calculate-raw-balance "0" "4") 0.0))))
(deftest test-token-value-in-chain
(testing "token-value-in-chain function"
(let [token {:balances-per-chain {1 {:raw-balance (money/bignumber 100000000)}
2 {:raw-balance (money/bignumber 50000000)}
3 {:raw-balance (money/bignumber 123456789)}}
:decimals 8}]
(is (= (utils/token-value-in-chain token 1) 1.0)))))
(deftest test-get-wallet-qr
(testing "Test get-wallet-qr function"
(let [wallet-multichain {:wallet-type :wallet-multichain
:selected-networks [:ethereum :optimism]
:address "x000"}
wallet-singlechain {:wallet-type :wallet-singlechain
:selected-networks [:ethereum :optimism]
:address "x000"}]
(is (= (utils/get-wallet-qr wallet-multichain)
"eth:opt:x000"))
(is (= (utils/get-wallet-qr wallet-singlechain)
"x000")))))