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>
This commit is contained in:
Mohamed Javid 2024-12-10 00:21:00 +05:30 committed by GitHub
parent 3c3ef20769
commit cdde36ca28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 130 additions and 7 deletions

View File

@ -711,13 +711,11 @@
(rf/reg-sub (rf/reg-sub
:wallet/zero-balance-in-all-non-watched-accounts? :wallet/zero-balance-in-all-non-watched-accounts?
:<- [:wallet/aggregated-tokens] :<- [:wallet/aggregated-tokens]
:<- [:profile/currency] (fn [aggregated-tokens]
:<- [:wallet/prices-per-token] (let [zero-balance? (->> aggregated-tokens
(fn [[aggregated-tokens currency prices-per-token]] (mapcat (comp vals :balances-per-chain))
(let [balance (utils/calculate-balance-from-tokens {:currency currency (every? #(money/equal-to (:raw-balance %) 0)))]
:tokens aggregated-tokens (and (not-empty aggregated-tokens) zero-balance?))))
:prices-per-token prices-per-token})]
(and (not-empty aggregated-tokens) (money/equal-to balance 0)))))
(rf/reg-sub (rf/reg-sub
:wallet/network-preference-details :wallet/network-preference-details

View File

@ -1008,3 +1008,128 @@
(update accounts "0x2" assoc :operable :partially))) (update accounts "0x2" assoc :operable :partially)))
(is (true? (rf/sub [sub-name]))))) (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])))))