Skip to content

Commit

Permalink
fix(swap): optimize endpoints calls (#21549)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Sztamfater <[email protected]>
  • Loading branch information
briansztamfater authored Nov 29, 2024
1 parent f45f969 commit 6859fb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
30 changes: 18 additions & 12 deletions src/status_im/contexts/wallet/swap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/status_im/contexts/wallet/swap/setup_swap/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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?}])))
Expand Down

0 comments on commit 6859fb8

Please sign in to comment.