Skip to content

Commit

Permalink
some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed May 23, 2024
1 parent 52c9c3d commit c210884
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 62 deletions.
15 changes: 2 additions & 13 deletions src/status_im/contexts/wallet/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[react-native.platform :as platform]
[status-im.constants :as constants]
[status-im.contexts.settings.wallet.events]
[status-im.contexts.wallet.common.utils :as utils]
[status-im.contexts.wallet.common.utils.networks :as network-utils]
[status-im.contexts.wallet.data-store :as data-store]
[status-im.contexts.wallet.db :as db]
Expand Down Expand Up @@ -43,18 +42,8 @@
[:dispatch [:wallet/show-account-created-toast address]]]}))

(rf/reg-event-fx :wallet/switch-current-viewing-account
(fn [{:keys [db]} [{:keys [address network-details update-balance?]}]]
(let [current-token (-> db :wallet :ui :send :token)
token (when (and current-token update-balance?)
;; When switching accounts, if there is a selected token we need to update
;; it to reflect balances of the new account
(let [token (utils/get-token-from-account db (:symbol current-token) address)]
(assoc token
:networks (network-utils/network-list token network-details)
:total-balance (utils/calculate-total-token-balance token))))]
{:db (cond-> db
:always (assoc-in [:wallet :current-viewing-account-address] address)
token (assoc-in [:wallet :ui :send :token] token))})))
(fn [{:keys [db]} [address]]
{:db (assoc-in db [:wallet :current-viewing-account-address] address)}))

(rf/reg-event-fx :wallet/clean-current-viewing-account
(fn [{:keys [db]}]
Expand Down
32 changes: 15 additions & 17 deletions src/status_im/contexts/wallet/send/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@
(when (empty? receiver-network-values) :receiver-network-values)))})))

(rf/reg-event-fx :wallet/get-suggested-routes
(fn [{:keys [db now]} [{:keys [amount]}]]
(fn [{:keys [db now]} [{:keys [amount updated-token]}]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
token (get-in db [:wallet :ui :send :token])
token (or updated-token (get-in db [:wallet :ui :send :token]))
transaction-type (get-in db [:wallet :ui :send :tx-type])
collectible (get-in db [:wallet :ui :send :collectible])
to-address (get-in db [:wallet :ui :send :to-address])
Expand Down Expand Up @@ -381,9 +381,8 @@
(send-utils/loading-network-amounts
{:valid-networks
(if (= transaction-type :tx/bridge)
(filter
#(not= bridge-to-chain-id %)
sender-token-available-networks-for-suggested-routes)
(remove #(= bridge-to-chain-id %)
sender-token-available-networks-for-suggested-routes)
sender-token-available-networks-for-suggested-routes)
:disabled-chain-ids disabled-from-chain-ids
:receiver-networks receiver-networks
Expand Down Expand Up @@ -414,13 +413,16 @@
network-preferences
gas-rates
from-locked-amount]]
{:db (-> db
(assoc-in [:wallet :ui :send :amount] amount)
(assoc-in [:wallet :ui :send :loading-suggested-routes?] true)
(assoc-in [:wallet :ui :send :sender-network-values] sender-network-values)
(assoc-in [:wallet :ui :send :receiver-network-values] receiver-network-values)
(assoc-in [:wallet :ui :send :suggested-routes-call-timestamp] now)
(update-in [:wallet :ui :send] dissoc :network-links))
{:db (cond-> db
:always (assoc-in [:wallet :ui :send :amount] amount)
:always (assoc-in [:wallet :ui :send :loading-suggested-routes?] true)
:always (assoc-in [:wallet :ui :send :sender-network-values]
sender-network-values)
:always (assoc-in [:wallet :ui :send :receiver-network-values]
receiver-network-values)
:always (assoc-in [:wallet :ui :send :suggested-routes-call-timestamp] now)
:always (update-in [:wallet :ui :send] dissoc :network-links)
token (assoc-in [:wallet :ui :send :token] token))
:json-rpc/call [{:method "wallet_getSuggestedRoutes"
:params request-params
:on-success (fn [suggested-routes]
Expand Down Expand Up @@ -634,11 +636,7 @@
(assoc-in [:wallet :ui :send :token] token)
(update-in [:wallet :ui :send] dissoc :token-symbol))
db)
:fx [[:dispatch
[:wallet/switch-current-viewing-account
{:address address
:network-details network-details
:update-balance? false}]]
:fx [[:dispatch [:wallet/switch-current-viewing-account address]]
[:dispatch
[:wallet/wizard-navigate-forward
{:current-screen stack-id
Expand Down
12 changes: 7 additions & 5 deletions src/status_im/contexts/wallet/send/input_amount/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@
enabled-from-chain-ids (rf/sub
[:wallet/wallet-send-enabled-from-chain-ids])
{token-balance :total-balance
available-balance :available-balance} (rf/sub [:wallet/token-by-symbol
(str token-symbol)
enabled-from-chain-ids])
available-balance :available-balance
:as token-by-symbol} (rf/sub [:wallet/token-by-symbol
(str token-symbol)
enabled-from-chain-ids])
currency-symbol (rf/sub [:profile/currency-symbol])
currency (rf/sub [:profile/currency])
conversion-rate (-> token :market-values-per-currency currency :price)
Expand Down Expand Up @@ -328,7 +329,7 @@
(number/remove-trailing-zeroes new-value))))))
:on-token-press show-select-asset-sheet}]
[routes/view
{:token token
{:token token-by-symbol
:input-value input-amount
:value amount
:valid-input? valid-input?
Expand Down Expand Up @@ -364,7 +365,8 @@
conversion-rate)
2))]
(rf/dispatch [:wallet/get-suggested-routes
{:amount amount}]))
{:amount amount
:updated-token token-by-symbol}]))
sending-to-unpreferred-networks?
#(show-unpreferred-networks-alert on-confirm)
:else
Expand Down
12 changes: 8 additions & 4 deletions src/status_im/contexts/wallet/send/routes/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
(def network-link-2x-height 111)

(defn- fetch-routes
[{:keys [amount bounce-duration-ms valid-input?]}]
[{:keys [amount bounce-duration-ms token valid-input?]}]
(if valid-input?
(debounce/debounce-and-dispatch
[:wallet/get-suggested-routes {:amount amount}]
[:wallet/get-suggested-routes
{:amount amount
:updated-token token}]
bounce-duration-ms)
(rf/dispatch [:wallet/clean-suggested-routes])))

Expand Down Expand Up @@ -203,14 +205,16 @@
(fetch-routes
{:amount value
:valid-input? valid-input?
:bounce-duration-ms 2000})))
:bounce-duration-ms 2000
:token token})))
[input-value valid-input?])
(rn/use-effect
#(when (and active-screen? (> (count token-available-networks-for-suggested-routes) 0))
(fetch-routes
{:amount value
:valid-input? valid-input?
:bounce-duration-ms 0}))
:bounce-duration-ms 0
:token token}))
[disabled-from-chain-ids])
[rn/scroll-view {:content-container-style style/routes-container}
(when show-routes?
Expand Down
11 changes: 5 additions & 6 deletions src/status_im/contexts/wallet/send/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@
[{:keys [balances-per-chain disabled-chain-ids only-with-balance?]}]
(let [disabled-set (set disabled-chain-ids)]
(->> balances-per-chain
(filter (fn [[_ {:keys [raw-balance]}]]
(or (not only-with-balance?)
(and only-with-balance?
(and (money/bignumber? raw-balance)
(filter (fn [[_ {:keys [chain-id raw-balance]}]]
(and (not (contains? disabled-set chain-id))
(or (not only-with-balance?)
(and only-with-balance?
(money/bignumber? raw-balance)
(money/greater-than raw-balance (money/bignumber "0")))))))
(filter (fn [[_ {:keys [chain-id]}]]
(not (contains? disabled-set chain-id))))
(map first))))

(def ^:private network-priority-score
Expand Down
11 changes: 3 additions & 8 deletions src/status_im/contexts/wallet/sheets/account_options/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[utils.re-frame :as rf]))

(defn- render-account-item
[{:keys [name emoji color address watch-only?]} _ _ {:keys [network-details]}]
[{:keys [name emoji color address watch-only?]}]
[quo/account-item
{:account-props (cond-> {:name name
:emoji emoji
Expand All @@ -27,10 +27,7 @@
(assoc :type :watch-only))
:title-icon (when watch-only? :i/reveal)
:on-press (fn []
(rf/dispatch [:wallet/switch-current-viewing-account
{:address address
:network-details network-details
:update-balance? true}])
(rf/dispatch [:wallet/switch-current-viewing-account address])
(rf/dispatch [:hide-bottom-sheet]))}])

(defn- options
Expand Down Expand Up @@ -114,14 +111,12 @@
(fn []
(let [theme (quo.theme/use-theme)
accounts (rf/sub [:wallet/accounts-without-current-viewing-account])
show-account-selector? (pos? (count accounts))
network-details (rf/sub [:wallet/network-details])]
show-account-selector? (pos? (count accounts))]
[:<>
(when show-account-selector?
[gesture/flat-list
{:data accounts
:render-fn render-account-item
:render-data {:network-details network-details}
:content-container-style (style/list-container @options-height)
:shows-vertical-scroll-indicator false}])
[options
Expand Down
13 changes: 4 additions & 9 deletions src/status_im/contexts/wallet/sheets/select_account/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
[utils.re-frame :as rf]))

(defn- render-account-item
[{:keys [color address] :as account} _ _ {:keys [selected-account-address network-details]}]
[{:keys [color address] :as account} _ _ {:keys [selected-account-address]}]
[quo/account-item
{:type :default
:account-props (assoc account :customization-color color)
:customization-color color
:state (if (= address selected-account-address) :selected :default)
:on-press (fn []
(rf/dispatch [:wallet/switch-current-viewing-account
{:address address
:network-details network-details
:update-balance? true}])
(rf/dispatch [:wallet/switch-current-viewing-account address])
(rf/dispatch [:hide-bottom-sheet]))}])

(defn view
[]
(let [selected-account-address (rf/sub [:wallet/current-viewing-account-address])
accounts (rf/sub [:wallet/accounts-without-watched-accounts])
network-details (rf/sub [:wallet/network-details])]
accounts (rf/sub [:wallet/accounts-without-watched-accounts])]
[:<>
[quo/drawer-top {:title (i18n/label :t/select-account)}]
[gesture/flat-list
{:data accounts
:render-fn render-account-item
:render-data {:selected-account-address selected-account-address
:network-details network-details}
:render-data {:selected-account-address selected-account-address}
:content-container-style style/list-container
:shows-vertical-scroll-indicator false}]]))

0 comments on commit c210884

Please sign in to comment.