fix(swap): optimize endpoints calls

Signed-off-by: Brian Sztamfater <brian@status.im>
This commit is contained in:
Brian Sztamfater 2024-11-02 01:10:20 -03:00
parent a7d330ecb3
commit cd825809ae
No known key found for this signature in database
GPG Key ID: 59EB921E0706B48F
3 changed files with 35 additions and 16 deletions

View File

@ -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

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?}])))

View File

@ -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]