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

View File

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