remove unnecessary subs and functions

This commit is contained in:
Brian Sztamfater 2024-11-13 08:40:58 +07:00
parent 327fa1e47e
commit 39a575e062
No known key found for this signature in database
GPG Key ID: 59EB921E0706B48F
1 changed files with 96 additions and 168 deletions

View File

@ -13,25 +13,13 @@
[status-im.contexts.wallet.common.utils :as utils] [status-im.contexts.wallet.common.utils :as utils]
[status-im.contexts.wallet.send.input-amount.estimated-fees :as estimated-fees] [status-im.contexts.wallet.send.input-amount.estimated-fees :as estimated-fees]
[status-im.contexts.wallet.send.input-amount.style :as style] [status-im.contexts.wallet.send.input-amount.style :as style]
[status-im.contexts.wallet.send.routes.view :as routes]
[status-im.contexts.wallet.sheets.buy-token.view :as buy-token] [status-im.contexts.wallet.sheets.buy-token.view :as buy-token]
[status-im.contexts.wallet.sheets.unpreferred-networks-alert.view :as unpreferred-networks-alert]
[status-im.setup.hot-reload :as hot-reload] [status-im.setup.hot-reload :as hot-reload]
[utils.debounce :as debounce] [utils.debounce :as debounce]
[utils.i18n :as i18n] [utils.i18n :as i18n]
[utils.money :as money] [utils.money :as money]
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
(defn- every-network-value-is-zero?
[sender-network-values]
(every? (fn [{:keys [total-amount]}]
(and
total-amount
(money/bignumber? total-amount)
(money/equal-to total-amount
(money/bignumber "0"))))
sender-network-values))
(defn select-asset-bottom-sheet (defn select-asset-bottom-sheet
[clear-input!] [clear-input!]
(let [{preselected-token-symbol :symbol} (rf/sub [:wallet/wallet-send-token])] (let [{preselected-token-symbol :symbol} (rf/sub [:wallet/wallet-send-token])]
@ -47,37 +35,6 @@
(rf/dispatch [:wallet/edit-token-to-send token]) (rf/dispatch [:wallet/edit-token-to-send token])
(clear-input!))}]])) (clear-input!))}]]))
(defn- token-not-available
[token-symbol receiver-networks token-networks]
(let [theme (quo.theme/use-theme)
add-token-networks (fn []
(let [chain-ids (concat receiver-networks
(mapv #(:chain-id %) token-networks))]
(rf/dispatch [:wallet/update-receiver-networks chain-ids])))]
[rn/view {:style (style/token-not-available-container theme)}
[rn/view
[quo/icon :i/alert
{:size 16
:color (colors/resolve-color :danger theme)}]]
[rn/view {:style style/token-not-available-content-container}
[quo/text
{:style (style/token-not-available-text theme)
:size :paragraph-2}
(i18n/label :t/token-not-available-on-receiver-networks {:token-symbol token-symbol})]
[quo/button
{:size 24
:customization-color (colors/resolve-color :danger theme)
:on-press add-token-networks}
(i18n/label :t/add-networks-token-can-be-sent-to {:token-symbol token-symbol})]]]))
(defn- show-unpreferred-networks-alert
[on-confirm]
(rf/dispatch
[:show-bottom-sheet
{:content (fn []
[unpreferred-networks-alert/view
{:on-confirm on-confirm}])}]))
(defn- no-routes-found (defn- no-routes-found
[] []
[rn/view {:style style/no-routes-found-container} [rn/view {:style style/no-routes-found-container}
@ -119,7 +76,7 @@
(defn- insufficient-asset-amount? (defn- insufficient-asset-amount?
[{:keys [token-symbol owned-eth-token input-state no-routes-found? limit-exceeded? [{:keys [token-symbol owned-eth-token input-state no-routes-found? limit-exceeded?
sender-network-values enough-assets?]}] enough-assets?]}]
(let [eth-selected? (= token-symbol (string/upper-case constants/mainnet-short-name)) (let [eth-selected? (= token-symbol (string/upper-case constants/mainnet-short-name))
zero-owned-eth? (money/equal-to (:total-balance owned-eth-token) 0) zero-owned-eth? (money/equal-to (:total-balance owned-eth-token) 0)
input-at-max-owned-amount? (money/equal-to input-at-max-owned-amount? (money/equal-to
@ -129,7 +86,6 @@
input-at-max-owned-amount? input-at-max-owned-amount?
zero-owned-eth?)] zero-owned-eth?)]
(and (or no-routes-found? limit-exceeded?) (and (or no-routes-found? limit-exceeded?)
(seq sender-network-values)
(or exceeded-input? (not enough-assets?))))) (or exceeded-input? (not enough-assets?)))))
(defn view (defn view
@ -142,122 +98,102 @@
button-one-props :button-one-props button-one-props :button-one-props
current-screen-id :current-screen-id current-screen-id :current-screen-id
initial-crypto-currency? :initial-crypto-currency? initial-crypto-currency? :initial-crypto-currency?
enabled-from-chain-ids :enabled-from-chain-ids
from-enabled-networks :from-enabled-networks
:or {initial-crypto-currency? true}}] :or {initial-crypto-currency? true}}]
(let [view-id (rf/sub [:view-id]) (let [view-id (rf/sub [:view-id])
active-screen? (= view-id current-screen-id) {fiat-currency :currency} (rf/sub [:profile/profile])
bottom (safe-area/get-bottom) currency (rf/sub [:profile/currency])
[crypto-currency? {token-symbol :symbol
set-crypto-currency] (rn/use-state initial-crypto-currency?) :as token} (rf/sub [:wallet/wallet-send-token])
handle-on-confirm (fn [amount] network (rf/sub [:wallet/send-network])
(rf/dispatch [:wallet/set-token-amount-to-send
{:amount amount
:stack-id current-screen-id}]))
{fiat-currency :currency} (rf/sub [:profile/profile])
{token-symbol :symbol
token-networks :networks
:as token} (rf/sub [:wallet/wallet-send-token])
network (rf/sub [:wallet/send-network])
send-from-locked-amounts (rf/sub [:wallet/wallet-send-from-locked-amounts])
{:keys [total-balance] {:keys [total-balance]
:as token-by-symbol} (rf/sub [:wallet/token-by-symbol :as token-by-symbol} (rf/sub [:wallet/token-by-symbol
(str token-symbol) (str token-symbol)
enabled-from-chain-ids]) (:chain-id network)])
token-balance (or default-limit-crypto total-balance) currency-symbol (rf/sub [:profile/currency-symbol])
usd-conversion-rate (utils/token-usd-price token) loading-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?])
currency (rf/sub [:profile/currency]) route (rf/sub [:wallet/wallet-send-route])
conversion-rate (-> token current-address (rf/sub [:wallet/current-viewing-account-address])
:market-values-per-currency current-color (rf/sub [:wallet/current-viewing-account-color])
currency enough-assets? (rf/sub [:wallet/wallet-send-enough-assets?])
:price) owned-eth-token (rf/sub [:wallet/token-by-symbol
token-decimals (rf/sub [:wallet/send-display-token-decimals]) (string/upper-case constants/mainnet-short-name)
[input-state set-input-state] (rn/use-state controlled-input/init-state) (:chain-id network)])
clear-input! #(set-input-state controlled-input/delete-all) suggested-routes (rf/sub [:wallet/wallet-send-suggested-routes])
currency-symbol (rf/sub [:profile/currency-symbol]) [crypto-currency?
loading-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?]) set-crypto-currency] (rn/use-state initial-crypto-currency?)
route (rf/sub [:wallet/wallet-send-route]) [input-state set-input-state] (rn/use-state controlled-input/init-state)
on-confirm (or default-on-confirm handle-on-confirm) active-screen? (= view-id current-screen-id)
max-limit (if crypto-currency? bottom (safe-area/get-bottom)
(utils/cut-crypto-decimals-to-fit-usd-cents token-balance (or default-limit-crypto total-balance)
token-balance usd-conversion-rate (utils/token-usd-price token)
usd-conversion-rate) conversion-rate (-> token
(utils/cut-fiat-balance-to-two-decimals :market-values-per-currency
(money/crypto->fiat token-balance conversion-rate))) currency
input-value (controlled-input/input-value input-state) :price)
valid-input? (not (or (controlled-input/empty-value? input-state) token-decimals (rf/sub [:wallet/send-display-token-decimals])
(controlled-input/input-error input-state))) clear-input! #(set-input-state controlled-input/delete-all)
amount-in-crypto (if crypto-currency? max-limit (if crypto-currency?
input-value (utils/cut-crypto-decimals-to-fit-usd-cents
(rf/sub [:wallet/send-amount-fixed token-balance
(/ input-value conversion-rate)])) usd-conversion-rate)
show-select-asset-sheet #(rf/dispatch (utils/cut-fiat-balance-to-two-decimals
[:show-bottom-sheet (money/crypto->fiat token-balance conversion-rate)))
{:content (fn [] input-value (controlled-input/input-value input-state)
[select-asset-bottom-sheet valid-input? (not (or (controlled-input/empty-value? input-state)
clear-input!])}]) (controlled-input/input-error input-state)))
sender-network-values (rf/sub [:wallet/wallet-send-sender-network-values]) amount-in-crypto (if crypto-currency?
receiver-network-values (rf/sub [:wallet/wallet-send-receiver-network-values]) input-value
tx-type (rf/sub [:wallet/wallet-send-tx-type]) (rf/sub [:wallet/send-amount-fixed
unsupported-token-in-receiver? (and (not= tx-type :tx/bridge) (/ input-value conversion-rate)]))
(->> receiver-network-values routes (when suggested-routes
(remove #(= (:type %) :add)) (or (:best suggested-routes) []))
(every? #(= (:type %) :not-available)))) no-routes-found? (and
suggested-routes (rf/sub [:wallet/wallet-send-suggested-routes]) (some? routes)
routes (when suggested-routes (not loading-routes?))
(or (:best suggested-routes) [])) input-error (controlled-input/input-error input-state)
no-routes-found? (and limit-exceeded? (controlled-input/upper-limit-exceeded? input-state)
(every-network-value-is-zero? sender-network-values) not-enough-asset? (insufficient-asset-amount?
(some? routes) {:enough-assets? enough-assets?
(not loading-routes?) :token-symbol token-symbol
(not unsupported-token-in-receiver?)) :owned-eth-token owned-eth-token
receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks]) :input-state input-state
receiver-preferred-networks (rf/sub [:wallet/wallet-send-receiver-preferred-networks]) :no-routes-found? no-routes-found?
receiver-preferred-network? (set receiver-preferred-networks) :limit-exceeded? limit-exceeded?})
sending-to-unpreferred-networks? (some (comp not receiver-preferred-network?) should-try-again? (and (not limit-exceeded?)
receiver-networks) no-routes-found?
input-error (controlled-input/input-error input-state) (not not-enough-asset?))
limit-exceeded? (controlled-input/upper-limit-exceeded? input-state) show-no-routes? (and (or no-routes-found? limit-exceeded?)
current-address (rf/sub [:wallet/current-viewing-account-address]) (not not-enough-asset?))
current-color (rf/sub [:wallet/current-viewing-account-color]) confirm-disabled? (or (nil? route)
enough-assets? (rf/sub [:wallet/wallet-send-enough-assets?]) (empty? route)
owned-eth-token (rf/sub [:wallet/token-by-symbol (not valid-input?))
(string/upper-case constants/mainnet-short-name) fee-formatted (when (or (not confirm-disabled?) not-enough-asset?)
enabled-from-chain-ids]) (get-fee-formatted route))
not-enough-asset? (insufficient-asset-amount? handle-on-confirm (fn [amount]
{:enough-assets? enough-assets? (rf/dispatch [:wallet/set-token-amount-to-send
:token-symbol token-symbol {:amount amount
:owned-eth-token owned-eth-token :stack-id current-screen-id}]))
:input-state input-state on-confirm (or default-on-confirm handle-on-confirm)
:no-routes-found? no-routes-found? show-select-asset-sheet #(rf/dispatch
:limit-exceeded? limit-exceeded? [:show-bottom-sheet
:sender-network-values sender-network-values}) {:content (fn []
should-try-again? (and (not limit-exceeded?) [select-asset-bottom-sheet
no-routes-found? clear-input!])}])
(not not-enough-asset?)) request-fetch-routes (fn [bounce-duration-ms]
show-no-routes? (and (or no-routes-found? limit-exceeded?) (fetch-routes
(not-empty sender-network-values) {:amount amount-in-crypto
(not not-enough-asset?)) :valid-input? valid-input?
confirm-disabled? (or (nil? route) :bounce-duration-ms bounce-duration-ms
(empty? route) :token token
(not valid-input?)) :reset-amounts-to-zero? (and limit-exceeded?
fee-formatted (when (or (not confirm-disabled?) not-enough-asset?) (some? routes))}))
(get-fee-formatted route)) swap-between-fiat-and-crypto (fn []
request-fetch-routes (fn [bounce-duration-ms] (if crypto-currency?
(fetch-routes (set-input-state
{:amount amount-in-crypto #(controlled-input/->fiat % conversion-rate))
:valid-input? valid-input? (set-input-state
:bounce-duration-ms bounce-duration-ms #(controlled-input/->crypto % conversion-rate)))
:token token (set-crypto-currency (not crypto-currency?)))]
:reset-amounts-to-zero? (and limit-exceeded?
(some? routes))}))
swap-between-fiat-and-crypto (fn []
(if crypto-currency?
(set-input-state
#(controlled-input/->fiat % conversion-rate))
(set-input-state
#(controlled-input/->crypto % conversion-rate)))
(set-crypto-currency (not crypto-currency?)))]
(rn/use-effect (rn/use-effect
(fn [] (fn []
(when active-screen? (when active-screen?
@ -285,10 +221,6 @@
(rf/dispatch [:wallet/stop-and-clean-suggested-routes]) (rf/dispatch [:wallet/stop-and-clean-suggested-routes])
(rf/dispatch [:wallet/clean-disabled-from-networks])) (rf/dispatch [:wallet/clean-disabled-from-networks]))
[current-address]) [current-address])
(rn/use-effect
(fn []
(request-fetch-routes 0))
[send-from-locked-amounts])
(rn/use-effect (rn/use-effect
(fn [] (fn []
(when active-screen? (when active-screen?
@ -354,14 +286,10 @@
loading-routes? loading-routes?
(and (not should-try-again?) (and (not should-try-again?)
confirm-disabled?)) confirm-disabled?))
:on-press (cond :on-press (if should-try-again?
should-try-again?
#(rf/dispatch [:wallet/start-get-suggested-routes #(rf/dispatch [:wallet/start-get-suggested-routes
{:amount amount-in-crypto {:amount amount-in-crypto
:updated-token token-by-symbol}]) :updated-token token-by-symbol}])
sending-to-unpreferred-networks?
#(show-unpreferred-networks-alert on-confirm)
:else
#(on-confirm amount-in-crypto)) #(on-confirm amount-in-crypto))
:customization-color current-color} :customization-color current-color}
(when should-try-again? (when should-try-again?