From cd825809aea2971d18c6e4abbb4c40d810777676 Mon Sep 17 00:00:00 2001 From: Brian Sztamfater Date: Sat, 2 Nov 2024 01:10:20 -0300 Subject: [PATCH] fix(swap): optimize endpoints calls Signed-off-by: Brian Sztamfater --- .../contexts/wallet/swap/events.cljs | 30 +++++++++++-------- .../contexts/wallet/swap/setup_swap/view.cljs | 16 +++++++--- src/status_im/subs/wallet/swap.cljs | 5 ++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/status_im/contexts/wallet/swap/events.cljs b/src/status_im/contexts/wallet/swap/events.cljs index a0761665fa..5d1be6833e 100644 --- a/src/status_im/contexts/wallet/swap/events.cljs +++ b/src/status_im/contexts/wallet/swap/events.cljs @@ -105,6 +105,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]) @@ -152,11 +156,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 @@ -237,13 +240,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 [last-request-uuid (get-in db [:wallet :ui :swap :last-request-uuid])] + (when last-request-uuid + {: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 diff --git a/src/status_im/contexts/wallet/swap/setup_swap/view.cljs b/src/status_im/contexts/wallet/swap/setup_swap/view.cljs index 220ebd8fc3..85c1050a12 100644 --- a/src/status_im/contexts/wallet/swap/setup_swap/view.cljs +++ b/src/status_im/contexts/wallet/swap/setup_swap/view.cljs @@ -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?}]))) diff --git a/src/status_im/subs/wallet/swap.cljs b/src/status_im/subs/wallet/swap.cljs index 95e1716a67..9d95fc45b4 100644 --- a/src/status_im/subs/wallet/swap.cljs +++ b/src/status_im/subs/wallet/swap.cljs @@ -47,6 +47,11 @@ :<- [:wallet/swap] :-> :error-response) +(rf/reg-sub + :wallet/swap-last-request-uuid + :<- [:wallet/swap] + :-> :last-request-uuid) + (rf/reg-sub :wallet/swap-error-response-code :<- [:wallet/swap-error-response]