swap-conversion-rate moved to controller

This commit is contained in:
Volodymyr Kozieiev 2024-10-21 13:10:38 +01:00
parent 664d9f0f05
commit 5032104bab
No known key found for this signature in database
GPG Key ID: 82B04968DF4C0535
2 changed files with 120 additions and 122 deletions

View File

@ -119,10 +119,12 @@
(rf/reg-sub (rf/reg-sub
:send-input-amount-screen/data :send-input-amount-screen/data
:<- [:send-input-amount-screen/controller] :<- [:send-input-amount-screen/controller]
:<- [:send-input-amount-screen/currency-information]
:<- [:send-input-amount-screen/max-limit] :<- [:send-input-amount-screen/max-limit]
:<- [:send-input-amount-screen/amount-in-crypto] :<- [:send-input-amount-screen/amount-in-crypto]
:<- [:send-input-amount-screen/token-input-converted-value] :<- [:send-input-amount-screen/token-input-converted-value]
(fn [[{:keys [crypto-currency? input-state] :as controller} (fn [[{:keys [crypto-currency? input-state] :as controller}
{:keys [conversion-rate]}
max-limit max-limit
amount-in-crypto amount-in-crypto
token-input-converted-value]] token-input-converted-value]]
@ -136,23 +138,29 @@
:limit-exceeded? (controlled-input/upper-limit-exceeded? input-state) :limit-exceeded? (controlled-input/upper-limit-exceeded? input-state)
:amount-in-crypto amount-in-crypto :amount-in-crypto amount-in-crypto
:token-input-converted-value token-input-converted-value :token-input-converted-value token-input-converted-value
:conversion-rate conversion-rate
})) }))
;; events ;; events
(rf/reg-event-fx
:send-input-amount-screen/swap-between-fiat-and-crypto
(fn [{:keys [db]}]
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :crypto-currency?] not)}))
(rf/reg-event-fx (rf/reg-event-fx
:send-input-amount-screen/set-input-state :send-input-amount-screen/set-input-state
(fn [{:keys [db]} [f]] (fn [{:keys [db]} [f]]
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :input-state] f)})) {:db (update-in db [:layers :ui :send :input-amount-screen :controller :input-state] f)}))
(rf/reg-event-fx
:send-input-amount-screen/swap-between-fiat-and-crypto
(fn [{:keys [db]} [crypto-currency? conversion-rate]]
{:db (update-in db [:layers :ui :send :input-amount-screen :controller :crypto-currency?] not)
:fx (if crypto-currency?
[[:dispatch
[:send-input-amount-screen/set-input-state #(controlled-input/->fiat % conversion-rate)]]]
[[:dispatch
[:send-input-amount-screen/set-input-state
#(controlled-input/->crypto % conversion-rate)]]])}))

View File

@ -234,129 +234,118 @@
valid-input? valid-input?
limit-exceeded? limit-exceeded?
amount-in-crypto amount-in-crypto
token-input-converted-value] token-input-converted-value
:as state} (rf/sub [:send-input-amount-screen/data]) conversion-rate]
view-id (rf/sub [:view-id]) :as state} (rf/sub [:send-input-amount-screen/data])
active-screen? (= view-id current-screen-id) view-id (rf/sub [:view-id])
bottom (safe-area/get-bottom) active-screen? (= view-id current-screen-id)
on-navigate-back on-navigate-back bottom (safe-area/get-bottom)
handle-on-confirm (fn [amount] on-navigate-back on-navigate-back
(rf/dispatch [:wallet/set-token-amount-to-send handle-on-confirm (fn [amount]
{:amount amount (rf/dispatch [:wallet/set-token-amount-to-send
:stack-id current-screen-id}])) {:amount amount
{fiat-currency :currency} (rf/sub [:profile/profile]) :stack-id current-screen-id}]))
{fiat-currency :currency} (rf/sub [:profile/profile])
{token-symbol :symbol {token-symbol :symbol
token-networks :networks token-networks :networks
:as :as
token} (rf/sub [:wallet/wallet-send-token]) token} (rf/sub [:wallet/wallet-send-token])
send-from-locked-amounts (rf/sub [:wallet/wallet-send-from-locked-amounts]) send-from-locked-amounts (rf/sub [:wallet/wallet-send-from-locked-amounts])
token-by-symbol (rf/sub [:wallet/token-by-symbol token-by-symbol (rf/sub [:wallet/token-by-symbol
(str token-symbol) (str token-symbol)
enabled-from-chain-ids]) enabled-from-chain-ids])
currency (rf/sub [:profile/currency]) token-decimals (-> token
conversion-rate (-> token utils/token-usd-price
:market-values-per-currency utils/one-cent-value
currency utils/calc-max-crypto-decimals)
:price) clear-input! #(rf/dispatch
token-decimals (-> token [:send-input-amount-screen/set-input-state
utils/token-usd-price controlled-input/delete-all])
utils/one-cent-value currency-symbol (rf/sub [:profile/currency-symbol])
utils/calc-max-crypto-decimals) loading-routes? (rf/sub
clear-input! #(rf/dispatch [:send-input-amount-screen/set-input-state [:wallet/wallet-send-loading-suggested-routes?])
controlled-input/delete-all]) route (rf/sub [:wallet/wallet-send-route])
currency-symbol (rf/sub [:profile/currency-symbol]) on-confirm (or default-on-confirm handle-on-confirm)
loading-routes? (rf/sub
[:wallet/wallet-send-loading-suggested-routes?])
route (rf/sub [:wallet/wallet-send-route])
on-confirm (or default-on-confirm handle-on-confirm)
confirm-disabled? (or (nil? route) confirm-disabled? (or (nil? route)
(empty? route) (empty? route)
(not valid-input?)) (not valid-input?))
total-amount-receiver (rf/sub [:wallet/total-amount true]) total-amount-receiver (rf/sub [:wallet/total-amount true])
amount-text (str (number/remove-trailing-zeroes amount-text (str (number/remove-trailing-zeroes
(.toFixed total-amount-receiver (.toFixed total-amount-receiver
(min token-decimals 6))) (min token-decimals 6)))
" " " "
token-symbol) token-symbol)
first-route (first route) first-route (first route)
native-currency-symbol (when-not confirm-disabled? native-currency-symbol (when-not confirm-disabled?
(get-in first-route (get-in first-route
[:from :native-currency-symbol])) [:from :native-currency-symbol]))
fee-formatted (when native-currency-symbol fee-formatted (when native-currency-symbol
(rf/sub [:wallet/wallet-send-fee-fiat-formatted (rf/sub [:wallet/wallet-send-fee-fiat-formatted
native-currency-symbol])) native-currency-symbol]))
show-select-asset-sheet #(rf/dispatch show-select-asset-sheet #(rf/dispatch
[:show-bottom-sheet [:show-bottom-sheet
{:content (fn [] {:content (fn []
[select-asset-bottom-sheet [select-asset-bottom-sheet
clear-input!])}]) clear-input!])}])
sender-network-values (rf/sub sender-network-values (rf/sub
[:wallet/wallet-send-sender-network-values]) [:wallet/wallet-send-sender-network-values])
receiver-network-values (rf/sub receiver-network-values (rf/sub
[:wallet/wallet-send-receiver-network-values]) [:wallet/wallet-send-receiver-network-values])
tx-type (rf/sub [:wallet/wallet-send-tx-type]) tx-type (rf/sub [:wallet/wallet-send-tx-type])
token-not-supported-in-receiver-networks? (and (not= tx-type :tx/bridge) token-not-supported-in-receiver-networks? (and (not= tx-type :tx/bridge)
(->> receiver-network-values (->> receiver-network-values
(remove #(= (:type %) :add)) (remove #(= (:type %) :add))
(every? #(= (:type %) :not-available)))) (every? #(= (:type %) :not-available))))
suggested-routes (rf/sub [:wallet/wallet-send-suggested-routes]) suggested-routes (rf/sub [:wallet/wallet-send-suggested-routes])
routes (when suggested-routes routes (when suggested-routes
(or (:best suggested-routes) [])) (or (:best suggested-routes) []))
no-routes-found? (and no-routes-found? (and
(every-network-value-is-zero? (every-network-value-is-zero?
sender-network-values) sender-network-values)
(not (nil? routes)) (not (nil? routes))
(not loading-routes?) (not loading-routes?)
(not token-not-supported-in-receiver-networks?)) (not token-not-supported-in-receiver-networks?))
receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks]) receiver-networks (rf/sub [:wallet/wallet-send-receiver-networks])
receiver-preferred-networks (rf/sub receiver-preferred-networks (rf/sub
[:wallet/wallet-send-receiver-preferred-networks]) [:wallet/wallet-send-receiver-preferred-networks])
receiver-preferred-networks-set (set receiver-preferred-networks) receiver-preferred-networks-set (set receiver-preferred-networks)
sending-to-unpreferred-networks? (not (every? (fn [receiver-selected-network] sending-to-unpreferred-networks? (not (every? (fn [receiver-selected-network]
(contains? (contains?
receiver-preferred-networks-set receiver-preferred-networks-set
receiver-selected-network)) receiver-selected-network))
receiver-networks)) receiver-networks))
should-try-again? (and (not limit-exceeded?) no-routes-found?) should-try-again? (and (not limit-exceeded?) no-routes-found?)
current-address (rf/sub [:wallet/current-viewing-account-address]) current-address (rf/sub [:wallet/current-viewing-account-address])
owned-eth-token (rf/sub [:wallet/token-by-symbol owned-eth-token (rf/sub [:wallet/token-by-symbol
(string/upper-case (string/upper-case
constants/mainnet-short-name) constants/mainnet-short-name)
enabled-from-chain-ids]) enabled-from-chain-ids])
not-enough-asset? (and not-enough-asset? (and
(or no-routes-found? limit-exceeded?) (or no-routes-found? limit-exceeded?)
(not-empty sender-network-values) (not-empty sender-network-values)
(if (= token-symbol (if (= token-symbol
(string/upper-case (string/upper-case
constants/mainnet-short-name)) constants/mainnet-short-name))
(money/equal-to (money/equal-to
(controlled-input/value-bn input-state) (controlled-input/value-bn input-state)
(controlled-input/upper-limit-bn input-state)) (controlled-input/upper-limit-bn input-state))
(money/equal-to (:total-balance (money/equal-to (:total-balance
owned-eth-token) owned-eth-token)
0))) 0)))
show-no-routes? (and show-no-routes? (and
(or no-routes-found? limit-exceeded?) (or no-routes-found? limit-exceeded?)
(not-empty sender-network-values) (not-empty sender-network-values)
(not not-enough-asset?)) (not not-enough-asset?))
request-fetch-routes (fn [bounce-duration-ms] request-fetch-routes (fn [bounce-duration-ms]
(fetch-routes (fetch-routes
{:amount amount-in-crypto {:amount amount-in-crypto
:valid-input? valid-input? :valid-input? valid-input?
:bounce-duration-ms bounce-duration-ms :bounce-duration-ms bounce-duration-ms
:token token :token token
:reset-amounts-to-zero? (and limit-exceeded? :reset-amounts-to-zero? (and limit-exceeded?
(some? routes))})) (some? routes))}))
swap-between-fiat-and-crypto (fn [] ]
(if crypto-currency?
(rf/dispatch [:send-input-amount-screen/set-input-state
#(controlled-input/->fiat % conversion-rate)])
(rf/dispatch [:send-input-amount-screen/set-input-state
#(controlled-input/->crypto % conversion-rate)])
)
(rf/dispatch
[:send-input-amount-screen/swap-between-fiat-and-crypto]))]
(rn/use-effect (rn/use-effect
(fn [] (fn []
(when active-screen? (when active-screen?
@ -401,7 +390,8 @@
{:container-style style/input-container {:container-style style/input-container
:token-symbol token-symbol :token-symbol token-symbol
:value input-value :value input-value
:on-swap swap-between-fiat-and-crypto :on-swap #(rf/dispatch [:send-input-amount-screen/swap-between-fiat-and-crypto
crypto-currency? conversion-rate])
:on-token-press show-select-asset-sheet :on-token-press show-select-asset-sheet
:error? input-error :error? input-error
:currency-symbol (if crypto-currency? token-symbol fiat-currency) :currency-symbol (if crypto-currency? token-symbol fiat-currency)