fix(swap): optimize endpoints calls (#21549)

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-11-29 13:21:44 -03:00 committed by GitHub
parent f45f96975e
commit 6859fb8f8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 16 deletions

View File

@ -111,6 +111,10 @@
(fn [{:keys [db]} [max-slippage]]
{:db (assoc-in db [:wallet :ui :swap :max-slippage] (number/parse-float max-slippage))}))
(rf/reg-event-fx :wallet.swap/set-loading-swap-proposal
(fn [{:keys [db]}]
{:db (assoc-in db [:wallet :ui :swap :loading-swap-proposal?] true)}))
(rf/reg-event-fx :wallet/start-get-swap-proposal
(fn [{:keys [db]} [{:keys [amount-in amount-out clean-approval-transaction?]}]]
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
@ -157,11 +161,10 @@
#(cond-> %
:always
(assoc
:last-request-uuid request-uuid
:amount amount
:amount-hex amount-in-hex
:loading-swap-proposal? true
:initial-response? true)
:last-request-uuid request-uuid
:amount amount
:amount-hex amount-in-hex
:initial-response? true)
clean-approval-transaction?
(dissoc :approval-transaction-id :approved-amount :swap-proposal)))
:fx [[:dispatch
@ -242,13 +245,16 @@
[:centralized-metrics/track :metric/swap-proposal-failed {:error (:code error-response)}]]]}))
(rf/reg-event-fx :wallet/stop-get-swap-proposal
(fn []
{:json-rpc/call [{:method "wallet_stopSuggestedRoutesAsyncCalculation"
:params []
:on-error (fn [error]
(log/error "failed to stop fetching swap proposals"
{:event :wallet/stop-get-swap-proposal
:error error}))}]}))
(fn [{:keys [db]}]
(let [route-request-ongoing? (some? (get-in db [:wallet :ui :swap :last-request-uuid]))]
(when route-request-ongoing?
{:db (update-in db [:wallet :ui :swap] dissoc :last-request-uuid)
:json-rpc/call [{:method "wallet_stopSuggestedRoutesAsyncCalculation"
:params []
:on-error (fn [error]
(log/error "failed to stop fetching swap proposals"
{:event :wallet/stop-get-swap-proposal
:error error}))}]}))))
(rf/reg-event-fx
:wallet/clean-swap-proposal

View File

@ -26,6 +26,7 @@
[utils.string :as utils.string]))
(def ^:private default-text-for-unfocused-input "0.00")
(def ^:private swap-proposal-debounce-time-ms 1000)
(defn- on-close
[start-point]
@ -36,13 +37,20 @@
:clean-approval-transaction? true}])
(events-helper/navigate-back))
(defn- start-get-swap-proposal
[amount clean-approval-transaction?]
(rf/dispatch [:wallet/stop-get-swap-proposal])
(rf/dispatch [:wallet.swap/set-loading-swap-proposal])
(debounce/debounce-and-dispatch [:wallet/start-get-swap-proposal
{:amount-in amount
:clean-approval-transaction? clean-approval-transaction?}]
swap-proposal-debounce-time-ms))
(defn- fetch-swap-proposal
[{:keys [amount valid-input? clean-approval-transaction?]}]
(debounce/clear-all)
(if valid-input?
(debounce/debounce-and-dispatch [:wallet/start-get-swap-proposal
{:amount-in amount
:clean-approval-transaction? clean-approval-transaction?}]
100)
(start-get-swap-proposal amount clean-approval-transaction?)
(rf/dispatch [:wallet/clean-swap-proposal
{:clean-amounts? true
:clean-approval-transaction? clean-approval-transaction?}])))