[#21557] fix: check and count account with token balance

This commit is contained in:
Mohsen 2024-11-20 21:34:33 +03:00
parent e093d42c94
commit 9143d15960
No known key found for this signature in database
GPG Key ID: 20BACCB8426033CE
3 changed files with 94 additions and 2 deletions

View File

@ -548,3 +548,27 @@
(:less-than-three-minutes constants/wallet-transaction-estimation) "1-3"
(:less-than-five-minutes constants/wallet-transaction-estimation) "3-5"
">5"))
(defn get-accounts-with-token-balance
[accounts token]
(let [operable-account (filter :operable? (vals accounts))
positive-balance-in-any-chain? (fn [{:keys [balances-per-chain]}]
(->> balances-per-chain
(map (comp :raw-balance val))
(some pos?)))
addresses-tokens-with-positive-balance (as-> operable-account $
(group-by :address $)
(update-vals $
#(filter positive-balance-in-any-chain?
(:tokens (first %)))))]
(if-let [asset-symbol (:symbol token)]
(let [addresses-with-asset (as-> addresses-tokens-with-positive-balance $
(update-vals $ #(set (map :symbol %)))
(keep (fn [[address token-symbols]]
(when (token-symbols asset-symbol) address))
$)
(set $))]
(filter #(addresses-with-asset (:address %)) operable-account))
(filter (fn [{:keys [tokens]}]
(some positive-balance-in-any-chain? tokens))
operable-account))))

View File

@ -269,3 +269,68 @@
expected "0"]
(is (= (utils/token-balance-display-for-network token chain-id rounding-decimals)
expected)))))
(def mock-accounts
{:0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5
{:address "0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5"
:operable? true
:tokens [{:symbol "ETH"
:balances-per-chain {1 {:raw-balance 1000000
:balance 1.0}}}
{:symbol "USDC"
:balances-per-chain {1 {:raw-balance 0
:balance 0}}}]}
:0xbce36f66a8cd99f1d6489cb9585146e3f3b893be
{:address "0xbce36f66a8cd99f1d6489cb9585146e3f3b893be"
:operable? true
:tokens [{:symbol "ETH"
:balances-per-chain {1 {:raw-balance 0
:balance 0}}}]}})
(deftest get-accounts-with-token-balance-test
(testing "Positive token balance for a specific token"
(let [accounts mock-accounts
token {:symbol "ETH"}
expected [{:address "0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5"
:operable? true
:tokens [{:symbol "ETH"
:balances-per-chain {1 {:raw-balance 1000000
:balance 1.0}}}
{:symbol "USDC"
:balances-per-chain {1 {:raw-balance 0
:balance 0}}}]}]]
(is (= (utils/get-accounts-with-token-balance accounts token)
expected))))
(testing "No token symbol provided, return all tokens with positive balance"
(let [accounts mock-accounts
token {}
expected [{:address "0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5"
:operable? true
:tokens [{:symbol "ETH"
:balances-per-chain {1 {:raw-balance 1000000
:balance 1.0}}}
{:symbol "USDC"
:balances-per-chain {1 {:raw-balance 0
:balance 0}}}]}]]
(is (= (utils/get-accounts-with-token-balance accounts token)
expected))))
(testing "No matching token found for a specific token"
(let [accounts mock-accounts
token {:symbol "DAI"}
expected []]
(is (= (utils/get-accounts-with-token-balance accounts token)
expected))))
(testing "No operable accounts"
(let [accounts {:0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5
{:address "0xfc6327a092f6232e158a0dd1d6d967a2e1c65cd5"
:operable? false
:tokens [{:symbol "ETH"
:balances-per-chain {1 {:raw-balance 1000000
:balance 1.0}}}]}}
token {}
expected []]
(is (= (utils/get-accounts-with-token-balance accounts token)
expected)))))

View File

@ -26,7 +26,10 @@
:account account
:test-networks-enabled? test-networks-enabled?
:token-symbol (get-in data [:asset-to-pay :symbol])}))
multi-account? (> (count (:accounts wallet)) 1)
multi-account-balance? (-> (utils/get-accounts-with-token-balance (:accounts wallet)
asset-to-pay)
(count)
(> 1))
network' (or network
(swap-utils/select-network asset-to-pay))
start-point (if open-new-screen? :action-menu :swap-button)]
@ -36,7 +39,7 @@
(assoc-in [:wallet :ui :swap :network] network')
(assoc-in [:wallet :ui :swap :launch-screen] view-id)
(assoc-in [:wallet :ui :swap :start-point] start-point))
:fx (if (and multi-account? (= view-id :wallet-stack) (not from-account))
:fx (if (and multi-account-balance? (= view-id :wallet-stack) (not from-account))
[[:dispatch [:open-modal :screen/wallet.swap-select-account]]]
(if network'
[[:dispatch [:wallet/switch-current-viewing-account (:address account)]]