Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#21323] fix: wrong networks when asset not available in default account #21689

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/status_im/contexts/wallet/swap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
view-id (:view-id db)
root-screen? (or (= view-id :wallet-stack) (nil? view-id))
account (or from-account (swap-utils/wallet-account wallet))
asset-to-pay (if (get-in data [:asset-to-pay :networks])
available-accounts (utils/get-accounts-with-token-balance (:accounts wallet)
(:asset-to-pay data))
account (or from-account
(swap-utils/current-viewing-account wallet)
(first available-accounts))
asset-to-pay (if (and (not from-account) (get-in data [:asset-to-pay :networks]))
(:asset-to-pay data)
(swap-utils/select-asset-to-pay-by-symbol
{:wallet wallet
:account account
:test-networks-enabled? test-networks-enabled?
:token-symbol (get-in data [:asset-to-pay :symbol])}))
multi-account-balance? (-> (utils/get-accounts-with-token-balance (:accounts wallet)
asset-to-pay)
multi-account-balance? (-> available-accounts
(count)
(> 1))
network' (or network
Expand Down
31 changes: 7 additions & 24 deletions src/status_im/contexts/wallet/swap/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,16 @@
:else
(i18n/label :t/something-went-wrong-please-try-again-later)))

(defn- first-operable-account
[accounts]
(->> accounts
vals
(remove :watch-only?)
(filter :operable?)
(sort-by :position)
first))

(defn wallet-account
"Picks the account that's gonna be used for the swap operation.
It's gonna be either the preselected account defined by
`[:wallet :current-viewing-account-address]` in `db`
or the first operable account."
(defn current-viewing-account
[wallet]
(if-let [wallet-address (get wallet :current-viewing-account-address)]
(-> wallet
:accounts
vals
(utils/get-account-by-address wallet-address))
(-> wallet :accounts first-operable-account)))
(when-let [wallet-address (get wallet :current-viewing-account-address)]
(get-in wallet [:accounts wallet-address])))

(defn select-asset-to-pay-by-symbol
"Selects an asset to pay by token symbol.
It's used for cases when only token symbol is available and the information
about token needs to be extracted from the database.
That happens when token is being selected on the home screen and
about token needs to be extracted from the database.
That happens when token is being selected on the home screen and
it basically indicates that no account pre-selection was made."
[{:keys [wallet account test-networks-enabled? token-symbol]}]
(let [networks (-> (get-in wallet [:networks (if test-networks-enabled? :test :prod)])
Expand All @@ -73,7 +56,7 @@

(defn select-default-asset-to-receive
"Selects an asset to receive if it was not provided explicitly.
The principle of how the asset is being selected is simple: we get the
The principle of how the asset is being selected is simple: we get the
whole list, remove the currently selected `asset-to-pay` from it, and choose
the first one of what's left."
[{:keys [wallet account test-networks-enabled? asset-to-pay]}]
Expand All @@ -86,7 +69,7 @@
(defn select-network
"Chooses the network.
Usually user needs to do the selection first and if the selection was done
then the list of networks for the defined token will always contain
then the list of networks for the defined token will always contain
one entry. Otherwise `nil` will be returned from here which will serve
as an indicator that the network selector needs to be displayed."
[{:keys [networks]}]
Expand Down