token-input-converted-value, amount-in-crypto moved to controller

This commit is contained in:
Volodymyr Kozieiev 2024-10-19 23:04:51 +01:00
parent a6c346ea7d
commit 664d9f0f05
No known key found for this signature in database
GPG Key ID: 82B04968DF4C0535
2 changed files with 62 additions and 51 deletions

View File

@ -3,6 +3,7 @@
[status-im.common.controlled-input.utils :as controlled-input]
[status-im.contexts.wallet.common.utils :as utils]
[utils.money :as money]
[utils.number :as number]
[utils.re-frame :as rf]))
;; notes
@ -82,16 +83,59 @@
conversion-rate)
utils/cut-fiat-balance-to-two-decimals))))
(rf/reg-sub
:send-input-amount-screen/amount-in-crypto
:<- [:send-input-amount-screen/controller]
:<- [:send-input-amount-screen/currency-information]
(fn [[{:keys [crypto-currency? input-state]}
{:keys [conversion-rate token-decimals]}]]
(let [input-value (controlled-input/input-value input-state)]
(if crypto-currency?
input-value
(number/remove-trailing-zeroes
(.toFixed (/ input-value conversion-rate)
token-decimals))))))
(rf/reg-sub
:send-input-amount-screen/token-input-converted-value
:<- [:send-input-amount-screen/controller]
:<- [:send-input-amount-screen/currency-information]
(fn [[{:keys [crypto-currency? input-state]}
{:keys [conversion-rate currency-symbol token-symbol]}]]
(let [input-value (controlled-input/input-value input-state)]
(if crypto-currency?
(utils/prettify-balance
currency-symbol
(money/crypto->fiat input-value
conversion-rate))
(utils/prettify-crypto-balance
(or (clj->js token-symbol) "")
(money/fiat->crypto input-value
conversion-rate)
conversion-rate)))))
(rf/reg-sub
:send-input-amount-screen/data
:<- [:send-input-amount-screen/controller]
:<- [:send-input-amount-screen/max-limit]
(fn [[{:keys [crypto-currency? input-state] :as controller} max-limit]]
{:crypto-currency? crypto-currency?
:max-limit max-limit
:input-state input-state
:<- [:send-input-amount-screen/amount-in-crypto]
:<- [:send-input-amount-screen/token-input-converted-value]
(fn [[{:keys [crypto-currency? input-state] :as controller}
max-limit
amount-in-crypto
token-input-converted-value]]
{:crypto-currency? crypto-currency?
:max-limit max-limit
:input-state input-state
:input-value (controlled-input/input-value input-state)
:input-error (controlled-input/input-error input-state)
:valid-input? (not (or (controlled-input/empty-value? input-state)
(controlled-input/input-error input-state)))
:limit-exceeded? (controlled-input/upper-limit-exceeded? input-state)
:amount-in-crypto amount-in-crypto
:token-input-converted-value token-input-converted-value
}))
@ -103,6 +147,7 @@
(fn [{:keys [db]}]
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :crypto-currency?] not)}))
(rf/reg-event-fx
:send-input-amount-screen/set-input-state
(fn [{:keys [db]} [f]]
@ -114,6 +159,7 @@
(comment
(rf/dispatch [:send-input-amount-screen/swap-between-fiat-and-crypto])
(rf/sub [:send-input-amount-screen/max-limit])
(rf/sub [:send-input-amount-screen/amount-in-crypto])
(rf/sub [:send-input-amount-screen/data])
(rf/sub [:send-input-amount-screen/controller])
@ -124,27 +170,3 @@
:token (:token (rf/sub [:send-input-amount-screen/currency-information]))})
)
#_(rf/reg-sub
:wallet/wallet-send-enough-assets?
:<- [:wallet/wallet-send]
:-> :enough-assets?)
#_(rf/reg-sub
:wallet/wallet-send-token
:<- [:wallet/wallet-send]
:<- [:wallet/network-details]
:<- [:wallet/wallet-send-disabled-from-chain-ids]
(fn [[wallet-send networks disabled-from-chain-ids]]
(let [token (:token wallet-send)
disabled-from-chain-ids? (set disabled-from-chain-ids)
enabled-from-chain-ids (->> networks
(map :chain-id)
(remove disabled-from-chain-ids?)
set)]
(some-> token
(assoc :networks (network-utils/network-list token networks)
:available-balance (utils/calculate-total-token-balance token)
:total-balance (utils/calculate-total-token-balance
token
enabled-from-chain-ids))))))

View File

@ -11,6 +11,7 @@
[status-im.contexts.wallet.common.account-switcher.view :as account-switcher]
[status-im.contexts.wallet.common.asset-list.view :as asset-list]
[status-im.contexts.wallet.common.utils :as utils]
[status-im.contexts.wallet.send.input-amount.controller]
[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]
@ -227,7 +228,13 @@
from-enabled-networks :from-enabled-networks}]
(let [{:keys [crypto-currency?
max-limit
input-state]
input-state
input-value
input-error
valid-input?
limit-exceeded?
amount-in-crypto
token-input-converted-value]
:as state} (rf/sub [:send-input-amount-screen/data])
view-id (rf/sub [:view-id])
active-screen? (= view-id current-screen-id)
@ -262,17 +269,10 @@
[:wallet/wallet-send-loading-suggested-routes?])
route (rf/sub [:wallet/wallet-send-route])
on-confirm (or default-on-confirm handle-on-confirm)
input-value (controlled-input/input-value input-state)
valid-input? (not (or (controlled-input/empty-value? input-state)
(controlled-input/input-error input-state)))
confirm-disabled? (or (nil? route)
(empty? route)
(not valid-input?))
amount-in-crypto (if crypto-currency?
input-value
(number/remove-trailing-zeroes
(.toFixed (/ input-value conversion-rate)
token-decimals)))
total-amount-receiver (rf/sub [:wallet/total-amount true])
amount-text (str (number/remove-trailing-zeroes
(.toFixed total-amount-receiver
@ -318,8 +318,6 @@
receiver-preferred-networks-set
receiver-selected-network))
receiver-networks))
input-error (controlled-input/input-error input-state)
limit-exceeded? (controlled-input/upper-limit-exceeded? input-state)
should-try-again? (and (not limit-exceeded?) no-routes-found?)
current-address (rf/sub [:wallet/current-viewing-account-address])
owned-eth-token (rf/sub [:wallet/token-by-symbol
@ -394,7 +392,7 @@
[rn/view
{:style style/screen
:accessibility-label (str "container"
(when (controlled-input/input-error input-state) "-error"))}
(when input-error "-error"))}
[account-switcher/view
{:icon-name :i/arrow-left
:on-press #(rf/dispatch [:navigate-back])
@ -405,18 +403,9 @@
:value input-value
:on-swap swap-between-fiat-and-crypto
:on-token-press show-select-asset-sheet
:error? (controlled-input/input-error input-state)
:error? input-error
:currency-symbol (if crypto-currency? token-symbol fiat-currency)
:converted-value (if crypto-currency?
(utils/prettify-balance
currency-symbol
(money/crypto->fiat input-value
conversion-rate))
(utils/prettify-crypto-balance
(or (clj->js token-symbol) "")
(money/fiat->crypto input-value
conversion-rate)
conversion-rate))
:converted-value token-input-converted-value
:hint-component [quo/network-tags
{:networks (seq from-enabled-networks)
:title (i18n/label