mirror of
https://github.com/status-im/status-react.git
synced 2025-01-10 19:16:59 +00:00
feat(swap): switch pay/receive assets (#21179)
Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
parent
2f3d3fc7f9
commit
9068801e4f
@ -8,9 +8,8 @@
|
|||||||
[taoensso.timbre :as log]
|
[taoensso.timbre :as log]
|
||||||
[utils.address :as address]
|
[utils.address :as address]
|
||||||
[utils.debounce :as debounce]
|
[utils.debounce :as debounce]
|
||||||
[utils.hex :as hex]
|
|
||||||
[utils.i18n :as i18n]
|
[utils.i18n :as i18n]
|
||||||
[utils.number]
|
[utils.money :as money]
|
||||||
[utils.number :as number]))
|
[utils.number :as number]))
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet.swap/start
|
(rf/reg-event-fx :wallet.swap/start
|
||||||
@ -47,14 +46,14 @@
|
|||||||
|
|
||||||
(rf/reg-event-fx :wallet.swap/set-max-slippage
|
(rf/reg-event-fx :wallet.swap/set-max-slippage
|
||||||
(fn [{:keys [db]} [max-slippage]]
|
(fn [{:keys [db]} [max-slippage]]
|
||||||
{:db (assoc-in db [:wallet :ui :swap :max-slippage] (utils.number/parse-float max-slippage))}))
|
{:db (assoc-in db [:wallet :ui :swap :max-slippage] (number/parse-float max-slippage))}))
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet.swap/select-asset-to-receive
|
(rf/reg-event-fx :wallet.swap/select-asset-to-receive
|
||||||
(fn [{:keys [db]} [{:keys [token]}]]
|
(fn [{:keys [db]} [{:keys [token]}]]
|
||||||
{:db (assoc-in db [:wallet :ui :swap :asset-to-receive] token)}))
|
{:db (assoc-in db [:wallet :ui :swap :asset-to-receive] token)}))
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet/start-get-swap-proposal
|
(rf/reg-event-fx :wallet/start-get-swap-proposal
|
||||||
(fn [{:keys [db]} [{:keys [amount-in amount-out]}]]
|
(fn [{:keys [db]} [{:keys [amount-in amount-out clean-approval-transaction?]}]]
|
||||||
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
|
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
|
||||||
{:keys [asset-to-pay asset-to-receive
|
{:keys [asset-to-pay asset-to-receive
|
||||||
network]} (get-in db [:wallet :ui :swap])
|
network]} (get-in db [:wallet :ui :swap])
|
||||||
@ -97,12 +96,17 @@
|
|||||||
(when-let [amount (or amount-in amount-out)]
|
(when-let [amount (or amount-in amount-out)]
|
||||||
{:db (update-in db
|
{:db (update-in db
|
||||||
[:wallet :ui :swap]
|
[:wallet :ui :swap]
|
||||||
#(-> %
|
#(cond-> %
|
||||||
(assoc
|
:always
|
||||||
:last-request-uuid request-uuid
|
(assoc
|
||||||
:amount amount
|
:last-request-uuid request-uuid
|
||||||
:loading-swap-proposal? true)
|
:amount amount
|
||||||
(dissoc :error-response)))
|
:amount-hex amount-in-hex
|
||||||
|
:loading-swap-proposal? true)
|
||||||
|
:always
|
||||||
|
(dissoc :error-response)
|
||||||
|
clean-approval-transaction?
|
||||||
|
(dissoc :approval-transaction-id :approved-amount :swap-proposal)))
|
||||||
:json-rpc/call [{:method "wallet_getSuggestedRoutesAsync"
|
:json-rpc/call [{:method "wallet_getSuggestedRoutesAsync"
|
||||||
:params params
|
:params params
|
||||||
:on-error (fn [error]
|
:on-error (fn [error]
|
||||||
@ -115,17 +119,30 @@
|
|||||||
(rf/reg-event-fx :wallet/swap-proposal-success
|
(rf/reg-event-fx :wallet/swap-proposal-success
|
||||||
(fn [{:keys [db]} [swap-proposal]]
|
(fn [{:keys [db]} [swap-proposal]]
|
||||||
(let [last-request-uuid (get-in db [:wallet :ui :swap :last-request-uuid])
|
(let [last-request-uuid (get-in db [:wallet :ui :swap :last-request-uuid])
|
||||||
|
amount-hex (get-in db [:wallet :ui :swap :amount-hex])
|
||||||
view-id (:view-id db)
|
view-id (:view-id db)
|
||||||
request-uuid (:uuid swap-proposal)
|
request-uuid (:uuid swap-proposal)
|
||||||
best-routes (:best swap-proposal)
|
best-routes (:best swap-proposal)
|
||||||
error-response (:error-response swap-proposal)]
|
error-response (:error-response swap-proposal)]
|
||||||
(when (= request-uuid last-request-uuid)
|
(when (and (= request-uuid last-request-uuid)
|
||||||
|
(or (and (empty? best-routes) error-response)
|
||||||
|
(and
|
||||||
|
(pos? (count best-routes))
|
||||||
|
(= (:amount-in (first best-routes)) amount-hex))))
|
||||||
(cond-> {:db (update-in db
|
(cond-> {:db (update-in db
|
||||||
[:wallet :ui :swap]
|
[:wallet :ui :swap]
|
||||||
assoc
|
assoc
|
||||||
:swap-proposal (first best-routes)
|
:swap-proposal (when-not (empty? best-routes)
|
||||||
|
(assoc (first best-routes) :uuid request-uuid))
|
||||||
:error-response (when (empty? best-routes) error-response)
|
:error-response (when (empty? best-routes) error-response)
|
||||||
:loading-swap-proposal? false)}
|
:loading-swap-proposal? false)}
|
||||||
|
(empty? best-routes)
|
||||||
|
(assoc :fx
|
||||||
|
[[:dispatch
|
||||||
|
[:toasts/upsert
|
||||||
|
{:id :swap-proposal-error
|
||||||
|
:type :negative
|
||||||
|
:text error-response}]]])
|
||||||
;; Router is unstable and it can return a swap proposal and after auto-refetching it can
|
;; Router is unstable and it can return a swap proposal and after auto-refetching it can
|
||||||
;; return an error. Ideally this shouldn't happen, but adding this behavior so if the
|
;; return an error. Ideally this shouldn't happen, but adding this behavior so if the
|
||||||
;; user is in swap confirmation screen or in token approval confirmation screen, we
|
;; user is in swap confirmation screen or in token approval confirmation screen, we
|
||||||
@ -156,16 +173,18 @@
|
|||||||
{:event :wallet/stop-get-swap-proposal
|
{:event :wallet/stop-get-swap-proposal
|
||||||
:error error}))}]}))
|
:error error}))}]}))
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet/clean-swap-proposal
|
(rf/reg-event-fx
|
||||||
(fn [{:keys [db]}]
|
:wallet/clean-swap-proposal
|
||||||
{:db (update-in db
|
(fn [{:keys [db]} [{:keys [clean-approval-transaction?]}]]
|
||||||
[:wallet :ui :swap]
|
(let [keys-to-dissoc (cond-> [:amount
|
||||||
dissoc
|
:amount-hex
|
||||||
:last-request-uuid
|
:last-request-uuid
|
||||||
:swap-proposal
|
:swap-proposal
|
||||||
:error-response
|
:error-response
|
||||||
:loading-swap-proposal?
|
:loading-swap-proposal?]
|
||||||
:approval-transaction-id)}))
|
clean-approval-transaction? (conj :approval-transaction-id :approved-amount))]
|
||||||
|
{:db (apply update-in db [:wallet :ui :swap] dissoc keys-to-dissoc)
|
||||||
|
:fx [[:dispatch [:wallet/stop-get-swap-proposal]]]})))
|
||||||
|
|
||||||
(rf/reg-event-fx :wallet/clean-swap
|
(rf/reg-event-fx :wallet/clean-swap
|
||||||
(fn [{:keys [db]}]
|
(fn [{:keys [db]}]
|
||||||
@ -245,10 +264,8 @@
|
|||||||
constants/min-token-decimals-to-display)
|
constants/min-token-decimals-to-display)
|
||||||
receive-amount (when amount-out
|
receive-amount (when amount-out
|
||||||
(number/remove-trailing-zeroes
|
(number/remove-trailing-zeroes
|
||||||
(.toFixed (number/convert-to-whole-number
|
(.toFixed (number/hex->whole
|
||||||
(native-module/hex-to-number
|
amount-out
|
||||||
(hex/normalize-hex
|
|
||||||
amount-out))
|
|
||||||
receive-token-decimals)
|
receive-token-decimals)
|
||||||
decimals-to-display)))]
|
decimals-to-display)))]
|
||||||
(rf/dispatch [:wallet.swap/add-authorized-transaction
|
(rf/dispatch [:wallet.swap/add-authorized-transaction
|
||||||
@ -268,7 +285,7 @@
|
|||||||
:screen/wallet.swap-confirmation)])
|
:screen/wallet.swap-confirmation)])
|
||||||
(when-not approval-required?
|
(when-not approval-required?
|
||||||
(rf/dispatch [:wallet/select-account-tab :activity])
|
(rf/dispatch [:wallet/select-account-tab :activity])
|
||||||
(debounce/debounce-and-dispatch [:wallet/clean-swap] 1000)
|
(rf/dispatch [:wallet/clean-swap])
|
||||||
(debounce/debounce-and-dispatch
|
(debounce/debounce-and-dispatch
|
||||||
[:toasts/upsert
|
[:toasts/upsert
|
||||||
{:id :swap-transaction-pending
|
{:id :swap-transaction-pending
|
||||||
@ -342,6 +359,8 @@
|
|||||||
:token-symbol token-symbol
|
:token-symbol token-symbol
|
||||||
:provider-name provider-name
|
:provider-name provider-name
|
||||||
:account-name account-name}))}]]]}
|
:account-name account-name}))}]]]}
|
||||||
|
transaction-confirmed?
|
||||||
|
(assoc :db (assoc-in db [:wallet :ui :swap :approved-amount] amount))
|
||||||
(not transaction-confirmed?)
|
(not transaction-confirmed?)
|
||||||
(assoc :db (update-in db [:wallet :ui :swap] dissoc :approval-transaction-id)))))))
|
(assoc :db (update-in db [:wallet :ui :swap] dissoc :approval-transaction-id)))))))
|
||||||
|
|
||||||
@ -368,3 +387,27 @@
|
|||||||
:receive-token-symbol receive-token-symbol
|
:receive-token-symbol receive-token-symbol
|
||||||
:receive-amount receive-amount})
|
:receive-amount receive-amount})
|
||||||
(i18n/label :t/swap-failed))}]]]}))))
|
(i18n/label :t/swap-failed))}]]]}))))
|
||||||
|
|
||||||
|
(rf/reg-event-fx :wallet.swap/flip-assets
|
||||||
|
(fn [{:keys [db]}]
|
||||||
|
(let [{:keys [asset-to-pay asset-to-receive
|
||||||
|
swap-proposal amount]} (get-in db [:wallet :ui :swap])
|
||||||
|
receive-token-decimals (:decimals asset-to-receive)
|
||||||
|
amount-out (when swap-proposal (:amount-out swap-proposal))
|
||||||
|
receive-amount (when amount-out
|
||||||
|
(-> amount-out
|
||||||
|
(number/hex->whole receive-token-decimals)
|
||||||
|
(money/to-fixed receive-token-decimals)))]
|
||||||
|
{:db (update-in db
|
||||||
|
[:wallet :ui :swap]
|
||||||
|
#(-> %
|
||||||
|
(assoc
|
||||||
|
:asset-to-pay asset-to-receive
|
||||||
|
:asset-to-receive asset-to-pay
|
||||||
|
:amount (or receive-amount amount))
|
||||||
|
(dissoc :swap-proposal
|
||||||
|
:error-response
|
||||||
|
:loading-swap-proposal?
|
||||||
|
:last-request-uuid
|
||||||
|
:approved-amount
|
||||||
|
:approval-transaction-id)))})))
|
||||||
|
@ -212,7 +212,7 @@
|
|||||||
(defn- slide-button
|
(defn- slide-button
|
||||||
[]
|
[]
|
||||||
(let [loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
(let [loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
||||||
swap-proposal (rf/sub [:wallet/swap-proposal])
|
swap-proposal (rf/sub [:wallet/swap-proposal-without-fees])
|
||||||
account (rf/sub [:wallet/current-viewing-account])
|
account (rf/sub [:wallet/current-viewing-account])
|
||||||
on-auth-success (rn/use-callback #(rf/dispatch
|
on-auth-success (rn/use-callback #(rf/dispatch
|
||||||
[:wallet/swap-transaction
|
[:wallet/swap-transaction
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
(ns status-im.contexts.wallet.swap.setup-swap.view
|
(ns status-im.contexts.wallet.swap.setup-swap.view
|
||||||
(:require [clojure.string :as string]
|
(:require [clojure.string :as string]
|
||||||
[native-module.core :as native-module]
|
|
||||||
[quo.core :as quo]
|
[quo.core :as quo]
|
||||||
[react-native.core :as rn]
|
[react-native.core :as rn]
|
||||||
[react-native.platform :as platform]
|
[react-native.platform :as platform]
|
||||||
@ -11,7 +10,7 @@
|
|||||||
[status-im.contexts.wallet.common.account-switcher.view :as account-switcher]
|
[status-im.contexts.wallet.common.account-switcher.view :as account-switcher]
|
||||||
[status-im.contexts.wallet.common.utils :as utils]
|
[status-im.contexts.wallet.common.utils :as utils]
|
||||||
[status-im.contexts.wallet.swap.setup-swap.style :as style]
|
[status-im.contexts.wallet.swap.setup-swap.style :as style]
|
||||||
[utils.hex :as hex]
|
[utils.debounce :as debounce]
|
||||||
[utils.i18n :as i18n]
|
[utils.i18n :as i18n]
|
||||||
[utils.money :as money]
|
[utils.money :as money]
|
||||||
[utils.number :as number]
|
[utils.number :as number]
|
||||||
@ -22,14 +21,18 @@
|
|||||||
|
|
||||||
(defn- on-close
|
(defn- on-close
|
||||||
[]
|
[]
|
||||||
(rf/dispatch [:wallet/clean-swap-proposal])
|
(rf/dispatch [:wallet/clean-swap-proposal {:clean-approval-transaction? true}])
|
||||||
(events-helper/navigate-back))
|
(events-helper/navigate-back))
|
||||||
|
|
||||||
(defn- fetch-swap-proposal
|
(defn- fetch-swap-proposal
|
||||||
[{:keys [amount valid-input?]}]
|
[{:keys [amount valid-input? clean-approval-transaction?]}]
|
||||||
(if valid-input?
|
(if valid-input?
|
||||||
(rf/dispatch [:wallet/start-get-swap-proposal {:amount-in amount}])
|
(debounce/debounce-and-dispatch [:wallet/start-get-swap-proposal
|
||||||
(rf/dispatch [:wallet/clean-swap-proposal])))
|
{:amount-in amount
|
||||||
|
:clean-approval-transaction? clean-approval-transaction?}]
|
||||||
|
100)
|
||||||
|
(rf/dispatch [:wallet/clean-swap-proposal
|
||||||
|
{:clean-approval-transaction? clean-approval-transaction?}])))
|
||||||
|
|
||||||
(defn- data-item
|
(defn- data-item
|
||||||
[{:keys [title subtitle size subtitle-icon loading?]}]
|
[{:keys [title subtitle size subtitle-icon loading?]}]
|
||||||
@ -69,11 +72,13 @@
|
|||||||
asset-to-pay (rf/sub [:wallet/swap-asset-to-pay])
|
asset-to-pay (rf/sub [:wallet/swap-asset-to-pay])
|
||||||
currency (rf/sub [:profile/currency])
|
currency (rf/sub [:profile/currency])
|
||||||
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
||||||
swap-proposal (rf/sub [:wallet/swap-proposal])
|
swap-proposal (rf/sub [:wallet/swap-proposal-without-fees])
|
||||||
approval-required (rf/sub [:wallet/swap-proposal-approval-required])
|
approval-required (rf/sub [:wallet/swap-proposal-approval-required])
|
||||||
approval-amount-required (rf/sub [:wallet/swap-proposal-approval-amount-required])
|
approval-amount-required (rf/sub [:wallet/swap-proposal-approval-amount-required])
|
||||||
currency-symbol (rf/sub [:profile/currency-symbol])
|
currency-symbol (rf/sub [:profile/currency-symbol])
|
||||||
approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status])
|
approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status])
|
||||||
|
approval-transaction-id (rf/sub [:wallet/swap-approval-transaction-id])
|
||||||
|
approved-amount (rf/sub [:wallet/swap-approved-amount])
|
||||||
pay-input-num-value (controlled-input/value-numeric input-state)
|
pay-input-num-value (controlled-input/value-numeric input-state)
|
||||||
pay-input-amount (controlled-input/input-value input-state)
|
pay-input-amount (controlled-input/input-value input-state)
|
||||||
pay-token-symbol (:symbol asset-to-pay)
|
pay-token-symbol (:symbol asset-to-pay)
|
||||||
@ -93,16 +98,16 @@
|
|||||||
(min pay-token-decimals
|
(min pay-token-decimals
|
||||||
constants/min-token-decimals-to-display)))
|
constants/min-token-decimals-to-display)))
|
||||||
approval-amount-required-num (when approval-amount-required
|
approval-amount-required-num (when approval-amount-required
|
||||||
(str (number/convert-to-whole-number
|
(str (number/hex->whole approval-amount-required
|
||||||
(native-module/hex-to-number
|
pay-token-decimals)))
|
||||||
(hex/normalize-hex
|
pay-input-error? (or (and (not (string/blank? pay-input-amount))
|
||||||
approval-amount-required))
|
(money/greater-than
|
||||||
pay-token-decimals)))
|
(money/bignumber pay-input-num-value)
|
||||||
pay-input-error? (and (not (string/blank? pay-input-amount))
|
(money/bignumber
|
||||||
(money/greater-than
|
pay-token-balance-selected-chain)))
|
||||||
(money/bignumber pay-input-num-value)
|
(money/equal-to (money/bignumber
|
||||||
(money/bignumber
|
available-crypto-limit)
|
||||||
pay-token-balance-selected-chain)))
|
(money/bignumber 0)))
|
||||||
valid-pay-input? (and
|
valid-pay-input? (and
|
||||||
(not (string/blank?
|
(not (string/blank?
|
||||||
pay-input-amount))
|
pay-input-amount))
|
||||||
@ -111,8 +116,9 @@
|
|||||||
request-fetch-swap-proposal (rn/use-callback
|
request-fetch-swap-proposal (rn/use-callback
|
||||||
(fn []
|
(fn []
|
||||||
(fetch-swap-proposal
|
(fetch-swap-proposal
|
||||||
{:amount pay-input-amount
|
{:amount pay-input-amount
|
||||||
:valid-input? valid-pay-input?}))
|
:valid-input? valid-pay-input?
|
||||||
|
:clean-approval-transaction? true}))
|
||||||
[pay-input-amount])]
|
[pay-input-amount])]
|
||||||
(rn/use-effect
|
(rn/use-effect
|
||||||
(fn []
|
(fn []
|
||||||
@ -123,7 +129,8 @@
|
|||||||
:error? pay-input-error?
|
:error? pay-input-error?
|
||||||
:token pay-token-symbol
|
:token pay-token-symbol
|
||||||
:customization-color :blue
|
:customization-color :blue
|
||||||
:show-approval-label? (and swap-proposal approval-required)
|
:show-approval-label? (or (and swap-proposal approval-required)
|
||||||
|
approval-transaction-id)
|
||||||
:auto-focus? true
|
:auto-focus? true
|
||||||
:show-keyboard? false
|
:show-keyboard? false
|
||||||
:status (cond
|
:status (cond
|
||||||
@ -145,7 +152,7 @@
|
|||||||
:confirmed :approved
|
:confirmed :approved
|
||||||
:finalised :approved
|
:finalised :approved
|
||||||
:approve)
|
:approve)
|
||||||
:token-value approval-amount-required-num
|
:token-value (or approval-amount-required-num approved-amount)
|
||||||
:button-props {:on-press on-approve-press}
|
:button-props {:on-press on-approve-press}
|
||||||
:customization-color account-color
|
:customization-color account-color
|
||||||
:token-symbol pay-token-symbol}}]))
|
:token-symbol pay-token-symbol}}]))
|
||||||
@ -169,14 +176,9 @@
|
|||||||
receive-token-symbol (:symbol asset-to-receive)
|
receive-token-symbol (:symbol asset-to-receive)
|
||||||
receive-token-decimals (:decimals asset-to-receive)
|
receive-token-decimals (:decimals asset-to-receive)
|
||||||
amount-out-whole-number (when amount-out
|
amount-out-whole-number (when amount-out
|
||||||
(number/convert-to-whole-number
|
(number/hex->whole amount-out receive-token-decimals))
|
||||||
(native-module/hex-to-number
|
|
||||||
(utils.hex/normalize-hex
|
|
||||||
amount-out))
|
|
||||||
receive-token-decimals))
|
|
||||||
amount-out-num (if amount-out-whole-number
|
amount-out-num (if amount-out-whole-number
|
||||||
(number/remove-trailing-zeroes
|
(number/to-fixed amount-out-whole-number receive-token-decimals)
|
||||||
(.toFixed amount-out-whole-number receive-token-decimals))
|
|
||||||
default-text-for-unfocused-input)
|
default-text-for-unfocused-input)
|
||||||
receive-token-fiat-value (str (utils/calculate-token-fiat-value
|
receive-token-fiat-value (str (utils/calculate-token-fiat-value
|
||||||
{:currency currency
|
{:currency currency
|
||||||
@ -205,7 +207,7 @@
|
|||||||
(defn- action-button
|
(defn- action-button
|
||||||
[{:keys [on-press]}]
|
[{:keys [on-press]}]
|
||||||
(let [account-color (rf/sub [:wallet/current-viewing-account-color])
|
(let [account-color (rf/sub [:wallet/current-viewing-account-color])
|
||||||
swap-proposal (rf/sub [:wallet/swap-proposal])
|
swap-proposal (rf/sub [:wallet/swap-proposal-without-fees])
|
||||||
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
||||||
approval-required? (rf/sub [:wallet/swap-proposal-approval-required])
|
approval-required? (rf/sub [:wallet/swap-proposal-approval-required])
|
||||||
approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status])]
|
approval-transaction-status (rf/sub [:wallet/swap-approval-transaction-status])]
|
||||||
@ -223,10 +225,10 @@
|
|||||||
[]
|
[]
|
||||||
(let [[pay-input-state set-pay-input-state] (rn/use-state controlled-input/init-state)
|
(let [[pay-input-state set-pay-input-state] (rn/use-state controlled-input/init-state)
|
||||||
[pay-input-focused? set-pay-input-focused?] (rn/use-state true)
|
[pay-input-focused? set-pay-input-focused?] (rn/use-state true)
|
||||||
[refetch-interval set-refetch-interval] (rn/use-state nil)
|
refetch-interval (rn/use-ref-atom nil)
|
||||||
error-response (rf/sub [:wallet/swap-error-response])
|
error-response (rf/sub [:wallet/swap-error-response])
|
||||||
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
||||||
swap-proposal (rf/sub [:wallet/swap-proposal])
|
swap-proposal (rf/sub [:wallet/swap-proposal-without-fees])
|
||||||
asset-to-pay (rf/sub [:wallet/swap-asset-to-pay])
|
asset-to-pay (rf/sub [:wallet/swap-asset-to-pay])
|
||||||
network (rf/sub [:wallet/swap-network])
|
network (rf/sub [:wallet/swap-network])
|
||||||
pay-input-amount (controlled-input/input-value pay-input-state)
|
pay-input-amount (controlled-input/input-value pay-input-state)
|
||||||
@ -270,7 +272,9 @@
|
|||||||
(fn []
|
(fn []
|
||||||
(set-pay-input-state
|
(set-pay-input-state
|
||||||
controlled-input/delete-last)
|
controlled-input/delete-last)
|
||||||
(rf/dispatch [:wallet/clean-swap-proposal])))
|
(rf/dispatch [:wallet/clean-swap-proposal
|
||||||
|
{:clean-approval-transaction?
|
||||||
|
true}])))
|
||||||
on-max-press (rn/use-callback
|
on-max-press (rn/use-callback
|
||||||
(fn [max-value]
|
(fn [max-value]
|
||||||
(set-pay-input-state
|
(set-pay-input-state
|
||||||
@ -280,32 +284,66 @@
|
|||||||
max-value)))))
|
max-value)))))
|
||||||
on-refresh-swap-proposal (rn/use-callback
|
on-refresh-swap-proposal (rn/use-callback
|
||||||
(fn []
|
(fn []
|
||||||
(let [bottom-sheets (rf/sub
|
(let
|
||||||
[:bottom-sheet-sheets])]
|
[bottom-sheets (rf/sub
|
||||||
|
[:bottom-sheet-sheets])
|
||||||
|
approval-transaction-status
|
||||||
|
(rf/sub
|
||||||
|
[:wallet/swap-approval-transaction-status])]
|
||||||
(when-not valid-pay-input?
|
(when-not valid-pay-input?
|
||||||
(js/clearInterval refetch-interval)
|
(when @refetch-interval
|
||||||
(set-refetch-interval nil))
|
(js/clearTimeout @refetch-interval))
|
||||||
|
(reset! refetch-interval nil))
|
||||||
|
(when @refetch-interval
|
||||||
|
(js/clearTimeout @refetch-interval))
|
||||||
|
(reset! refetch-interval nil)
|
||||||
(when (and valid-pay-input?
|
(when (and valid-pay-input?
|
||||||
(not loading-swap-proposal?)
|
(not loading-swap-proposal?)
|
||||||
(not bottom-sheets))
|
(not bottom-sheets)
|
||||||
|
(not= approval-transaction-status
|
||||||
|
:pending))
|
||||||
(fetch-swap-proposal
|
(fetch-swap-proposal
|
||||||
{:amount pay-input-amount
|
{:amount pay-input-amount
|
||||||
:valid-input? valid-pay-input?}))))
|
:valid-input? valid-pay-input?
|
||||||
|
:clean-approval-transaction? false}))))
|
||||||
[valid-pay-input? loading-swap-proposal?
|
[valid-pay-input? loading-swap-proposal?
|
||||||
pay-input-amount])]
|
pay-input-amount])
|
||||||
|
on-asset-to-pay-change (fn []
|
||||||
|
(when valid-pay-input?
|
||||||
|
(fetch-swap-proposal
|
||||||
|
{:amount pay-input-amount
|
||||||
|
:valid-input? valid-pay-input?
|
||||||
|
:clean-approval-transaction? true})))]
|
||||||
(rn/use-effect (fn []
|
(rn/use-effect (fn []
|
||||||
(when refetch-interval
|
(when @refetch-interval
|
||||||
(js/clearInterval refetch-interval)
|
(js/clearInterval @refetch-interval)
|
||||||
(set-refetch-interval nil))
|
(reset! refetch-interval nil))
|
||||||
(when (or swap-proposal error-response)
|
(when (or swap-proposal error-response)
|
||||||
(set-refetch-interval (js/setInterval
|
(reset! refetch-interval
|
||||||
on-refresh-swap-proposal
|
(js/setInterval
|
||||||
constants/swap-proposal-refresh-interval-ms))))
|
on-refresh-swap-proposal
|
||||||
|
constants/swap-proposal-refresh-interval-ms))))
|
||||||
[swap-proposal error-response])
|
[swap-proposal error-response])
|
||||||
(rn/use-unmount (fn []
|
(rn/use-unmount (fn []
|
||||||
(when refetch-interval
|
(rf/dispatch [:wallet/clean-swap-proposal {:clean-approval-transaction? true}])
|
||||||
(js/clearInterval refetch-interval)
|
(when @refetch-interval
|
||||||
(set-refetch-interval nil))))
|
(js/clearInterval @refetch-interval)
|
||||||
|
(reset! refetch-interval nil))))
|
||||||
|
(rn/use-effect
|
||||||
|
(fn []
|
||||||
|
(when asset-to-pay
|
||||||
|
(let [swap-amount (rf/sub [:wallet/swap-amount])]
|
||||||
|
(when (and swap-amount refetch-interval)
|
||||||
|
(js/clearTimeout @refetch-interval)
|
||||||
|
(reset! refetch-interval nil))
|
||||||
|
(if (and swap-amount (not= swap-amount pay-input-amount))
|
||||||
|
(set-pay-input-state
|
||||||
|
(fn [input-state]
|
||||||
|
(controlled-input/set-input-value
|
||||||
|
input-state
|
||||||
|
swap-amount)))
|
||||||
|
(on-asset-to-pay-change)))))
|
||||||
|
[asset-to-pay])
|
||||||
[rn/view {:style style/container}
|
[rn/view {:style style/container}
|
||||||
[account-switcher/view
|
[account-switcher/view
|
||||||
{:on-press on-close
|
{:on-press on-close
|
||||||
@ -322,7 +360,12 @@
|
|||||||
:on-input-focus (fn []
|
:on-input-focus (fn []
|
||||||
(when platform/android? (rf/dispatch [:dismiss-keyboard]))
|
(when platform/android? (rf/dispatch [:dismiss-keyboard]))
|
||||||
(set-pay-input-focused? true))}]
|
(set-pay-input-focused? true))}]
|
||||||
[swap-order-button {:on-press #(js/alert "Swap Order Pressed")}]
|
[swap-order-button
|
||||||
|
{:on-press (fn []
|
||||||
|
(when @refetch-interval
|
||||||
|
(js/clearTimeout @refetch-interval)
|
||||||
|
(reset! refetch-interval nil))
|
||||||
|
(rf/dispatch [:wallet.swap/flip-assets]))}]
|
||||||
[receive-token-input
|
[receive-token-input
|
||||||
{:input-focused? (not pay-input-focused?)
|
{:input-focused? (not pay-input-focused?)
|
||||||
:on-token-press #(js/alert "Token Pressed")
|
:on-token-press #(js/alert "Token Pressed")
|
||||||
|
@ -157,12 +157,13 @@
|
|||||||
(defn- slide-button
|
(defn- slide-button
|
||||||
[]
|
[]
|
||||||
(let [loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
(let [loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])
|
||||||
swap-proposal (rf/sub [:wallet/swap-proposal])
|
swap-proposal (rf/sub [:wallet/swap-proposal-without-fees])
|
||||||
account (rf/sub [:wallet/current-viewing-account])
|
account (rf/sub [:wallet/current-viewing-account])
|
||||||
account-color (:color account)
|
account-color (:color account)
|
||||||
on-auth-success (rn/use-callback #(rf/dispatch
|
on-auth-success (rn/use-callback (fn [data]
|
||||||
[:wallet/swap-transaction
|
(rf/dispatch [:wallet/stop-get-swap-proposal])
|
||||||
(security/safe-unmask-data %)]))]
|
(rf/dispatch [:wallet/swap-transaction
|
||||||
|
(security/safe-unmask-data data)])))]
|
||||||
[standard-auth/slide-button
|
[standard-auth/slide-button
|
||||||
{:size :size-48
|
{:size :size-48
|
||||||
:track-text (i18n/label :t/slide-to-swap)
|
:track-text (i18n/label :t/slide-to-swap)
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
(ns status-im.subs.wallet.swap
|
(ns status-im.subs.wallet.swap
|
||||||
(:require [clojure.string :as string]
|
(:require [clojure.string :as string]
|
||||||
[native-module.core :as native-module]
|
|
||||||
[re-frame.core :as rf]
|
[re-frame.core :as rf]
|
||||||
[status-im.constants :as constants]
|
[status-im.constants :as constants]
|
||||||
[status-im.contexts.wallet.common.utils :as utils]
|
[status-im.contexts.wallet.common.utils :as utils]
|
||||||
[status-im.contexts.wallet.send.utils :as send-utils]
|
[status-im.contexts.wallet.send.utils :as send-utils]
|
||||||
[utils.hex :as hex]
|
|
||||||
[utils.money :as money]
|
[utils.money :as money]
|
||||||
[utils.number :as number]))
|
[utils.number :as number]))
|
||||||
|
|
||||||
@ -95,6 +93,25 @@
|
|||||||
:<- [:wallet/swap]
|
:<- [:wallet/swap]
|
||||||
:-> :swap-proposal)
|
:-> :swap-proposal)
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:wallet/swap-proposal-without-fees
|
||||||
|
:<- [:wallet/swap]
|
||||||
|
(fn [swap]
|
||||||
|
(let [swap-proposal (:swap-proposal swap)]
|
||||||
|
(reduce dissoc
|
||||||
|
swap-proposal
|
||||||
|
[:gas-fees :gas-amount :token-fees :bonder-fees :approval-gas-fees]))))
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:wallet/swap-amount
|
||||||
|
:<- [:wallet/swap]
|
||||||
|
:-> :amount)
|
||||||
|
|
||||||
|
(rf/reg-sub
|
||||||
|
:wallet/swap-approved-amount
|
||||||
|
:<- [:wallet/swap]
|
||||||
|
:-> :approved-amount)
|
||||||
|
|
||||||
(rf/reg-sub
|
(rf/reg-sub
|
||||||
:wallet/swap-loading-swap-proposal?
|
:wallet/swap-loading-swap-proposal?
|
||||||
:<- [:wallet/swap]
|
:<- [:wallet/swap]
|
||||||
@ -117,11 +134,7 @@
|
|||||||
(fn [[amount-out asset-to-receive]]
|
(fn [[amount-out asset-to-receive]]
|
||||||
(let [receive-token-decimals (:decimals asset-to-receive)
|
(let [receive-token-decimals (:decimals asset-to-receive)
|
||||||
receive-amount (when amount-out
|
receive-amount (when amount-out
|
||||||
(number/convert-to-whole-number
|
(number/hex->whole amount-out receive-token-decimals))
|
||||||
(native-module/hex-to-number
|
|
||||||
(hex/normalize-hex
|
|
||||||
amount-out))
|
|
||||||
receive-token-decimals))
|
|
||||||
receive-amount (when amount-out
|
receive-amount (when amount-out
|
||||||
(number/remove-trailing-zeroes
|
(number/remove-trailing-zeroes
|
||||||
(.toFixed receive-amount
|
(.toFixed receive-amount
|
||||||
@ -136,11 +149,7 @@
|
|||||||
(fn [[amount-in asset-to-pay]]
|
(fn [[amount-in asset-to-pay]]
|
||||||
(let [pay-token-decimals (:decimals asset-to-pay)
|
(let [pay-token-decimals (:decimals asset-to-pay)
|
||||||
pay-amount (when amount-in
|
pay-amount (when amount-in
|
||||||
(number/convert-to-whole-number
|
(number/hex->whole amount-in pay-token-decimals))
|
||||||
(native-module/hex-to-number
|
|
||||||
(hex/normalize-hex
|
|
||||||
amount-in))
|
|
||||||
pay-token-decimals))
|
|
||||||
pay-amount (when amount-in
|
pay-amount (when amount-in
|
||||||
(number/remove-trailing-zeroes
|
(number/remove-trailing-zeroes
|
||||||
(.toFixed pay-amount
|
(.toFixed pay-amount
|
||||||
|
@ -44,15 +44,18 @@
|
|||||||
|
|
||||||
(defn greater-than-or-equals
|
(defn greater-than-or-equals
|
||||||
[^js bn1 ^js bn2]
|
[^js bn1 ^js bn2]
|
||||||
(.greaterThanOrEqualTo bn1 bn2))
|
(when (bignumber? bn1)
|
||||||
|
(.greaterThanOrEqualTo bn1 bn2)))
|
||||||
|
|
||||||
(defn greater-than
|
(defn greater-than
|
||||||
[bn1 bn2]
|
[bn1 bn2]
|
||||||
(.greaterThan ^js bn1 bn2))
|
(when (bignumber? bn1)
|
||||||
|
(.greaterThan ^js bn1 bn2)))
|
||||||
|
|
||||||
(defn less-than
|
(defn less-than
|
||||||
[bn1 bn2]
|
[bn1 bn2]
|
||||||
(.lessThan ^js bn1 bn2))
|
(when (bignumber? bn1)
|
||||||
|
(.lessThan ^js bn1 bn2)))
|
||||||
|
|
||||||
(defn equal-to
|
(defn equal-to
|
||||||
[n1 n2]
|
[n1 n2]
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
(ns utils.number
|
(ns utils.number
|
||||||
(:require [clojure.string :as string]))
|
(:require [clojure.string :as string]
|
||||||
|
[native-module.core :as native-module]
|
||||||
|
[utils.hex :as utils.hex]
|
||||||
|
[utils.money :as utils.money]))
|
||||||
|
|
||||||
(defn naive-round
|
(defn naive-round
|
||||||
"Quickly and naively round number `n` up to `decimal-places`.
|
"Quickly and naively round number `n` up to `decimal-places`.
|
||||||
@ -61,3 +64,16 @@
|
|||||||
(str "." (string/replace decimals #"0+$" ""))
|
(str "." (string/replace decimals #"0+$" ""))
|
||||||
"")
|
"")
|
||||||
""))))
|
""))))
|
||||||
|
|
||||||
|
(defn hex->whole
|
||||||
|
[num decimals]
|
||||||
|
(-> num
|
||||||
|
utils.hex/normalize-hex
|
||||||
|
native-module/hex-to-number
|
||||||
|
(convert-to-whole-number decimals)))
|
||||||
|
|
||||||
|
(defn to-fixed
|
||||||
|
[num decimals]
|
||||||
|
(-> num
|
||||||
|
(utils.money/to-fixed decimals)
|
||||||
|
remove-trailing-zeroes))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user