Skip to content

Commit

Permalink
[#21557] feat: show from account page in swap flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri committed Nov 12, 2024
1 parent 028f959 commit d516016
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/status_im/contexts/wallet/common/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@

(defn token-balance-display-for-network
"Formats a token balance for a specific chain and rounds it to a specified number of decimals.
If the balance is less than the smallest representable value based on rounding decimals,
If the balance is less than the smallest representable value based on rounding decimals,
a threshold value is displayed instead."
[token chain-id rounding-decimals]
(let [token-decimals (:decimals token)
Expand Down Expand Up @@ -549,3 +549,7 @@
(:less-than-three-minutes constants/wallet-transaction-estimation) "1-3"
(:less-than-five-minutes constants/wallet-transaction-estimation) "3-5"
">5"))

(defn multi-account?
[{:keys [accounts]}]
(> (count accounts) 1))
77 changes: 47 additions & 30 deletions src/status_im/contexts/wallet/swap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
[utils.number :as number]))

(rf/reg-event-fx :wallet.swap/start
(fn [{:keys [db]} [{:keys [network asset-to-receive open-new-screen?] :as data}]]
(fn [{:keys [db]} [{:keys [network asset-to-receive open-new-screen? from-account] :as data}]]
(let [{:keys [wallet]} db
test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
view-id (:view-id db)
account (swap-utils/wallet-account wallet)
account (or from-account (swap-utils/wallet-account wallet))
asset-to-pay (if (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? (utils/multi-account? wallet)
network' (or network
(swap-utils/select-network asset-to-pay))
start-point (if open-new-screen? :action-menu :swap-button)]
Expand All @@ -35,34 +36,38 @@
(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 network'
[[:dispatch [:wallet/switch-current-viewing-account (:address account)]]
[:dispatch
(if open-new-screen?
[:open-modal :screen/wallet.setup-swap]
[:navigate-to-within-stack
[:screen/wallet.setup-swap :screen/wallet.swap-select-asset-to-pay]])]
[:dispatch
[:centralized-metrics/track :metric/swap-start
{:network (:chain-id network)
:pay_token (:symbol asset-to-pay)
:receive_token (:symbol asset-to-receive)
:start_point start-point
:launch_screen view-id}]]
[:dispatch [:wallet.swap/set-default-slippage]]]
[[:dispatch
[:show-bottom-sheet
{:content (fn []
[network-selection/view
{:token-symbol (:symbol asset-to-pay)
:on-select-network (fn [network]
(rf/dispatch [:hide-bottom-sheet])
(rf/dispatch
[:wallet.swap/start
{:asset-to-pay asset-to-pay
:asset-to-receive asset-to-receive
:network network
:open-new-screen? open-new-screen?}]))}])}]]])})))
:fx (if (and multi-account? (= view-id :wallet-stack) (not from-account))
[[:dispatch [:open-modal :screen/wallet.swap-from]]]
(if network'
[[:dispatch [:wallet/switch-current-viewing-account (:address account)]]
[:dispatch
(if open-new-screen?
[:open-modal :screen/wallet.setup-swap]
[:navigate-to-within-stack
[:screen/wallet.setup-swap :screen/wallet.swap-select-asset-to-pay]])]
[:dispatch
[:centralized-metrics/track :metric/swap-start
{:network (:chain-id network)
:pay_token (:symbol asset-to-pay)
:receive_token (:symbol asset-to-receive)
:start_point start-point
:launch_screen view-id}]]
[:dispatch [:wallet.swap/set-default-slippage]]]
[[:dispatch
[:show-bottom-sheet
{:content (fn []
[network-selection/view
{:token-symbol (:symbol asset-to-pay)
:on-select-network (fn [network]
(rf/dispatch [:hide-bottom-sheet])
(rf/dispatch
[:wallet.swap/start
{:asset-to-pay asset-to-pay
:asset-to-receive asset-to-receive
:network network
:open-new-screen?
open-new-screen?
:from-account from-account}]))}])}]]]))})))

(rf/reg-event-fx :wallet.swap/select-asset-to-pay
(fn [{:keys [db]} [{:keys [token]}]]
Expand Down Expand Up @@ -527,3 +532,15 @@
[:dispatch [:wallet/navigate-to-account-within-stack address]])
[:dispatch [:wallet/fetch-activities-for-current-account]]
[:dispatch [:wallet/select-account-tab :activity]]]})))

(rf/reg-event-fx :wallet.swap/start-from-account
(fn [{:keys [db]} [account]]
(let [asset-to-pay (get-in db [:wallet :ui :swap :asset-to-pay])
asset-to-receive (get-in db [:wallet :ui :swap :asset-to-receive])]
{:fx [[:dispatch [:navigate-back]]
[:dispatch
[:wallet.swap/start
{:asset-to-pay asset-to-pay
:asset-to-receive asset-to-receive
:open-new-screen? true
:from-account account}]]]})))
7 changes: 7 additions & 0 deletions src/status_im/contexts/wallet/swap/from/style.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns status-im.contexts.wallet.swap.from.style)

(def accounts-list
{:padding-bottom 12})

(def accounts-list-container
{:padding-horizontal 8})
53 changes: 53 additions & 0 deletions src/status_im/contexts/wallet/swap/from/view.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns status-im.contexts.wallet.swap.from.view
(:require
[quo.core :as quo]
[react-native.core :as rn]
[react-native.safe-area :as safe-area]
[status-im.common.events-helper :as events-helper]
[status-im.common.floating-button-page.view :as floating-button-page]
[status-im.contexts.wallet.swap.from.style :as style]
[status-im.setup.hot-reload :as hot-reload]
[utils.i18n :as i18n]
[utils.money :as money]
[utils.re-frame :as rf]))

(defn- on-account-press
[account]
(rf/dispatch [:wallet.swap/start-from-account account]))

(defn- render-fn
[item _ _]
(let [has-balance (money/greater-than (:asset-pay-balance item) (money/bignumber "0"))]
[quo/account-item
{:type (if has-balance :tag :default)
:on-press #(on-account-press item)
:state (if has-balance :default :active)
:token-props {:symbol (:asset-pay-symbol item)
:value (:asset-pay-balance item)}
:account-props (assoc item
:address (:formatted-address item)
:full-address? true)}]))

(defn- on-close
[]
(rf/dispatch [:wallet/clean-current-viewing-account]))

(defn view
[]
(let [accounts (rf/sub [:wallet/accounts-with-balances])]
(hot-reload/use-safe-unmount on-close)
[floating-button-page/view
{:footer-container-padding 0
:header [quo/page-nav
{:margin-top (safe-area/get-top)
:icon-name :i/close
:on-press events-helper/navigate-back}]}
[quo/page-top
{:title (i18n/label :t/from-label)
:title-accessibility-label :title-label}]
[rn/flat-list
{:style style/accounts-list
:content-container-style style/accounts-list-container
:data accounts
:render-fn render-fn
:shows-horizontal-scroll-indicator false}]]))
7 changes: 7 additions & 0 deletions src/status_im/navigation/screens.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
[status-im.contexts.wallet.send.send-amount.view :as wallet-send-input-amount]
[status-im.contexts.wallet.send.transaction-confirmation.view :as wallet-transaction-confirmation]
[status-im.contexts.wallet.send.transaction-progress.view :as wallet-transaction-progress]
[status-im.contexts.wallet.swap.from.view :as wallet-swap-from]
[status-im.contexts.wallet.swap.select-asset-to-pay.view :as wallet-swap-select-asset-to-pay]
[status-im.contexts.wallet.swap.set-spending-cap.view :as wallet-swap-set-spending-cap]
[status-im.contexts.wallet.swap.setup-swap.view :as wallet-swap-setup-swap]
Expand Down Expand Up @@ -635,6 +636,12 @@
:insets {:top? true}}
:component wallet-swap-select-asset-to-pay/view}

{:name :screen/wallet.swap-from
:metrics {:track? true}
:options {:modalPresentationStyle :overCurrentContext
:insets {:bottom? true}}
:component wallet-swap-from/view}

{:name :screen/wallet.setup-swap
:metrics {:track? true
:alias-id :wallet-swap.input-amount-to-swap}
Expand Down
21 changes: 21 additions & 0 deletions src/status_im/subs/wallet/swap.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,24 @@
:currency-symbol currency-symbol
:balance (or amount 0)
:token asset-to-pay-with-current-account-balance})))

(rf/reg-sub
:wallet/accounts-with-balances
:<- [:wallet/operable-accounts]
:<- [:wallet/swap-asset-to-pay]
(fn [[accounts asset-to-pay]]
(let [token-symbol (:symbol asset-to-pay)]
(map
(fn [account]
(let [tokens (:tokens account)
filtered-tokens (filter #(= (:symbol %) token-symbol) tokens)
asset-pay-balance (utils/calculate-total-token-balance filtered-tokens)
formatted-address @(rf/subscribe [:wallet/account-address (:address account)
(:network-preferences-names account)])]
(assoc account
:formatted-address formatted-address
:asset-pay-balance (utils/sanitized-token-amount-to-display
asset-pay-balance
constants/min-token-decimals-to-display)
:asset-pay-symbol token-symbol)))
accounts))))

0 comments on commit d516016

Please sign in to comment.