From bd4b7fc49ddcba23ee1f245869603cbb473cdcef Mon Sep 17 00:00:00 2001 From: Ulises Manuel <90291778+ulisesmac@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:40:24 -0600 Subject: [PATCH] fix(wallet): Error while bridging tokens due to bonderFees (#20929) --- src/status_im/contexts/wallet/data_store.cljs | 3 +- .../contexts/wallet/send/events.cljs | 60 ++++++++++++------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/status_im/contexts/wallet/data_store.cljs b/src/status_im/contexts/wallet/data_store.cljs index d92a5da009..d305295ae2 100644 --- a/src/status_im/contexts/wallet/data_store.cljs +++ b/src/status_im/contexts/wallet/data_store.cljs @@ -213,8 +213,7 @@ [new-path] (let [bonder-fees (:tx-bonder-fees new-path) token-fees (+ (money/wei->ether bonder-fees) - (money/wei->ether - (:tx-token-fees new-path)))] + (money/wei->ether (:tx-token-fees new-path)))] {:from (:from-chain new-path) :amount-in-locked (:amount-in-locked new-path) :amount-in (:amount-in new-path) diff --git a/src/status_im/contexts/wallet/send/events.cljs b/src/status_im/contexts/wallet/send/events.cljs index 5d07851fdd..91dda0608c 100644 --- a/src/status_im/contexts/wallet/send/events.cljs +++ b/src/status_im/contexts/wallet/send/events.cljs @@ -105,9 +105,7 @@ cleaned-receiver-network-values (-> (get-in db [:wallet :ui :send :receiver-network-values]) (send-utils/reset-loading-network-amounts-to-zero))] {:db (-> db - (update-in [:wallet :ui :send] - dissoc - :route) + (update-in [:wallet :ui :send] dissoc :route) (assoc-in [:wallet :ui :send :sender-network-values] cleaned-sender-network-values) (assoc-in [:wallet :ui :send :receiver-network-values] cleaned-receiver-network-values) (assoc-in [:wallet :ui :send :loading-suggested-routes?] false) @@ -491,26 +489,48 @@ {:event :wallet/stop-get-suggested-routes :error error}))}]})) -(rf/reg-event-fx :wallet/handle-suggested-routes +(defn- bridge-amount-greater-than-bonder-fees? + [{{token-decimals :decimals} :from-token + bonder-fees :tx-bonder-fees + amount-in :amount-in}] + (let [bonder-fees (utils.money/token->unit bonder-fees token-decimals) + amount-to-bridge (utils.money/token->unit amount-in token-decimals)] + (> amount-to-bridge bonder-fees))) + +(defn- remove-multichain-routes + [routes] + (if (> (count routes) 1) + [] ;; if route is multichain, we remove it + routes)) + +(defn- remove-invalid-bonder-fees-routes + [routes] + (filter bridge-amount-greater-than-bonder-fees? routes)) + +(defn- ->old-route-paths + [routes] + (map data-store/new->old-route-path routes)) + +(rf/reg-event-fx + :wallet/handle-suggested-routes (fn [_ data] - (if-let [error (some-> data - first - :ErrorResponse - (#(if (= (:code %) "0") "An error occurred" (:details %))))] - (do + (if-let [{:keys [code details]} (-> data :ErrorResponse first)] + (let [error-message (if (= code "0") "An error occurred" details)] (log/error "failed to get suggested routes (async)" {:event :wallet/handle-suggested-routes - :error error}) - {:fx [[:dispatch [:wallet/suggested-routes-error error]]]}) - (let [suggested-routes-new-data (data-store/rpc->suggested-routes data) - suggested-routes (-> suggested-routes-new-data - first - ;; if route is multichain, we remove it - (update :best (fn [best] (if (> (count best) 1) [] best))) - (update :best #(map data-store/new->old-route-path %)) - (update :candidates #(map data-store/new->old-route-path %)))] - {:fx [[:dispatch - [:wallet/suggested-routes-success suggested-routes]]]})))) + :error error-message}) + {:fx [[:dispatch [:wallet/suggested-routes-error error-message]]]}) + (let [best-routes-fix (comp ->old-route-paths + remove-invalid-bonder-fees-routes + remove-multichain-routes) + candidates-fix (comp ->old-route-paths + remove-invalid-bonder-fees-routes) + routes (-> data + (first) + (data-store/rpc->suggested-routes) + (update :best best-routes-fix) + (update :candidates candidates-fix))] + {:fx [[:dispatch [:wallet/suggested-routes-success routes]]]})))) (rf/reg-event-fx :wallet/add-authorized-transaction (fn [{:keys [db]} [transaction]]