From 2f43e51ad44b04e075b12ab62a6f8b4132655422 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Tue, 26 Nov 2024 19:56:40 +0300 Subject: [PATCH 1/4] [#21323] fix: wrong networks when asset not available in default account --- .../contexts/wallet/swap/events.cljs | 7 ++--- src/status_im/contexts/wallet/swap/utils.cljs | 27 +++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/status_im/contexts/wallet/swap/events.cljs b/src/status_im/contexts/wallet/swap/events.cljs index 350c0ddae9f..5e97e3d7526 100644 --- a/src/status_im/contexts/wallet/swap/events.cljs +++ b/src/status_im/contexts/wallet/swap/events.cljs @@ -16,7 +16,9 @@ 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)) + available-accounts (utils/get-accounts-with-token-balance (:accounts wallet) + (:asset-to-pay data)) + account (or from-account (swap-utils/wallet-account wallet available-accounts)) asset-to-pay (if (get-in data [:asset-to-pay :networks]) (:asset-to-pay data) (swap-utils/select-asset-to-pay-by-symbol @@ -24,8 +26,7 @@ :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 diff --git a/src/status_im/contexts/wallet/swap/utils.cljs b/src/status_im/contexts/wallet/swap/utils.cljs index 4070e5b4b98..d4932c17fa0 100644 --- a/src/status_im/contexts/wallet/swap/utils.cljs +++ b/src/status_im/contexts/wallet/swap/utils.cljs @@ -30,33 +30,24 @@ :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 + "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." - [wallet] + or the first operable account from the list of available accounts (accounts with token balance)." + [wallet available-accounts] (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))) + (first available-accounts))) (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)]) @@ -73,7 +64,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]}] @@ -86,7 +77,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]}] From 46dd63f3d858de504cf7d8e7b2808356afb6edaf Mon Sep 17 00:00:00 2001 From: Mohsen Date: Wed, 27 Nov 2024 17:00:04 +0300 Subject: [PATCH 2/4] [#21323] fix: improve condition --- src/status_im/contexts/wallet/swap/events.cljs | 4 +++- src/status_im/contexts/wallet/swap/utils.cljs | 13 ++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/status_im/contexts/wallet/swap/events.cljs b/src/status_im/contexts/wallet/swap/events.cljs index 5e97e3d7526..22d05def1f4 100644 --- a/src/status_im/contexts/wallet/swap/events.cljs +++ b/src/status_im/contexts/wallet/swap/events.cljs @@ -18,7 +18,9 @@ root-screen? (or (= view-id :wallet-stack) (nil? view-id)) available-accounts (utils/get-accounts-with-token-balance (:accounts wallet) (:asset-to-pay data)) - account (or from-account (swap-utils/wallet-account wallet available-accounts)) + account (or from-account + (swap-utils/current-viewing-account wallet) + (first available-accounts)) asset-to-pay (if (get-in data [:asset-to-pay :networks]) (:asset-to-pay data) (swap-utils/select-asset-to-pay-by-symbol diff --git a/src/status_im/contexts/wallet/swap/utils.cljs b/src/status_im/contexts/wallet/swap/utils.cljs index d4932c17fa0..9cc28dabd26 100644 --- a/src/status_im/contexts/wallet/swap/utils.cljs +++ b/src/status_im/contexts/wallet/swap/utils.cljs @@ -30,18 +30,13 @@ :else (i18n/label :t/something-went-wrong-please-try-again-later))) -(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 from the list of available accounts (accounts with token balance)." - [wallet available-accounts] - (if-let [wallet-address (get wallet :current-viewing-account-address)] +(defn current-viewing-account + [wallet] + (when-let [wallet-address (get wallet :current-viewing-account-address)] (-> wallet :accounts vals - (utils/get-account-by-address wallet-address)) - (first available-accounts))) + (utils/get-account-by-address wallet-address)))) (defn select-asset-to-pay-by-symbol "Selects an asset to pay by token symbol. From c671536380ed07d3d002ac9e2c669dec79a3c4d9 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Thu, 28 Nov 2024 09:40:13 +0300 Subject: [PATCH 3/4] [#21323] fix: get accounts by index --- src/status_im/contexts/wallet/swap/utils.cljs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/status_im/contexts/wallet/swap/utils.cljs b/src/status_im/contexts/wallet/swap/utils.cljs index 9cc28dabd26..2eb3b36d274 100644 --- a/src/status_im/contexts/wallet/swap/utils.cljs +++ b/src/status_im/contexts/wallet/swap/utils.cljs @@ -33,10 +33,7 @@ (defn current-viewing-account [wallet] (when-let [wallet-address (get wallet :current-viewing-account-address)] - (-> wallet - :accounts - vals - (utils/get-account-by-address wallet-address)))) + (get-in wallet [:accounts wallet-address]))) (defn select-asset-to-pay-by-symbol "Selects an asset to pay by token symbol. From c079337b8aebadfd76a5cbecd846c20f5bf868d3 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Mon, 2 Dec 2024 11:00:16 +0300 Subject: [PATCH 4/4] [#21323] fix: wrong network selection from select account page --- src/status_im/contexts/wallet/swap/events.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/status_im/contexts/wallet/swap/events.cljs b/src/status_im/contexts/wallet/swap/events.cljs index 22d05def1f4..c0ef94b4206 100644 --- a/src/status_im/contexts/wallet/swap/events.cljs +++ b/src/status_im/contexts/wallet/swap/events.cljs @@ -21,7 +21,7 @@ account (or from-account (swap-utils/current-viewing-account wallet) (first available-accounts)) - asset-to-pay (if (get-in data [:asset-to-pay :networks]) + 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