From cdde36ca28be36511f474e54b7fc5d0b5f24385e Mon Sep 17 00:00:00 2001 From: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com> Date: Tue, 10 Dec 2024 00:21:00 +0530 Subject: [PATCH] fix(wallet)_: logic for calculating zero balance (#21777) This commit fixes logic to calculate whether the user has zero balance in all non-watched accounts. Signed-off-by: Mohamed Javid <19339952+smohamedjavid@users.noreply.github.com> --- src/status_im/subs/wallet/wallet.cljs | 12 +- src/status_im/subs/wallet/wallet_test.cljs | 125 +++++++++++++++++++++ 2 files changed, 130 insertions(+), 7 deletions(-) diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 71e3219c5d..63d510646f 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -711,13 +711,11 @@ (rf/reg-sub :wallet/zero-balance-in-all-non-watched-accounts? :<- [:wallet/aggregated-tokens] - :<- [:profile/currency] - :<- [:wallet/prices-per-token] - (fn [[aggregated-tokens currency prices-per-token]] - (let [balance (utils/calculate-balance-from-tokens {:currency currency - :tokens aggregated-tokens - :prices-per-token prices-per-token})] - (and (not-empty aggregated-tokens) (money/equal-to balance 0))))) + (fn [aggregated-tokens] + (let [zero-balance? (->> aggregated-tokens + (mapcat (comp vals :balances-per-chain)) + (every? #(money/equal-to (:raw-balance %) 0)))] + (and (not-empty aggregated-tokens) zero-balance?)))) (rf/reg-sub :wallet/network-preference-details diff --git a/src/status_im/subs/wallet/wallet_test.cljs b/src/status_im/subs/wallet/wallet_test.cljs index 97b34ea9d0..b5854ee842 100644 --- a/src/status_im/subs/wallet/wallet_test.cljs +++ b/src/status_im/subs/wallet/wallet_test.cljs @@ -1008,3 +1008,128 @@ (update accounts "0x2" assoc :operable :partially))) (is (true? (rf/sub [sub-name]))))) +(h/deftest-sub :wallet/zero-balance-in-all-non-watched-accounts? + [sub-name] + (testing "returns true if the balance is zero in all non-watched accounts" + (swap! rf-db/app-db + #(assoc-in % + [:wallet :accounts] + {"0x1" {:address "0x1" + :watch-only? false + :tokens + [{:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}} + {:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}}]} + "0x2" {:address "0x2" + :watch-only? false + :tokens + [{:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}} + {:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}}]} + + "0x3" + {:address "0x3" + :watch-only? true + :tokens [{:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance + (money/bignumber + "2") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance + (money/bignumber + "1") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance + (money/bignumber + "0") + :has-error false}}} + {:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance + (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance + (money/bignumber + "2") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance + (money/bignumber + "0") + :has-error + false}}}]}})) + (is (true? (rf/sub [sub-name])))) + (testing "returns false if the balance is not zero in all non-watched accounts" + (swap! rf-db/app-db + #(assoc-in % + [:wallet :accounts] + {"0x1" {:address "0x1" + :watch-only? false + :tokens + [{:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "2") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "1") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}} + {:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "2") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}}]} + "0x2" {:address "0x2" + :watch-only? true + :tokens + [{:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "2") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "1") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}} + {:balances-per-chain {constants/ethereum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false} + constants/optimism-mainnet-chain-id {:raw-balance (money/bignumber + "2") + :has-error false} + constants/arbitrum-mainnet-chain-id {:raw-balance (money/bignumber + "0") + :has-error false}}}]}})) + (is (false? (rf/sub [sub-name])))))