fix(wallet): Error while bridging tokens due to bonderFees (#20929)

This commit is contained in:
Ulises Manuel 2024-07-31 15:40:24 -06:00 committed by GitHub
parent 29252336cc
commit bd4b7fc49d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 22 deletions

View File

@ -213,8 +213,7 @@
[new-path] [new-path]
(let [bonder-fees (:tx-bonder-fees new-path) (let [bonder-fees (:tx-bonder-fees new-path)
token-fees (+ (money/wei->ether bonder-fees) token-fees (+ (money/wei->ether bonder-fees)
(money/wei->ether (money/wei->ether (:tx-token-fees new-path)))]
(:tx-token-fees new-path)))]
{:from (:from-chain new-path) {:from (:from-chain new-path)
:amount-in-locked (:amount-in-locked new-path) :amount-in-locked (:amount-in-locked new-path)
:amount-in (:amount-in new-path) :amount-in (:amount-in new-path)

View File

@ -105,9 +105,7 @@
cleaned-receiver-network-values (-> (get-in db [:wallet :ui :send :receiver-network-values]) cleaned-receiver-network-values (-> (get-in db [:wallet :ui :send :receiver-network-values])
(send-utils/reset-loading-network-amounts-to-zero))] (send-utils/reset-loading-network-amounts-to-zero))]
{:db (-> db {:db (-> db
(update-in [:wallet :ui :send] (update-in [:wallet :ui :send] dissoc :route)
dissoc
:route)
(assoc-in [:wallet :ui :send :sender-network-values] cleaned-sender-network-values) (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 :receiver-network-values] cleaned-receiver-network-values)
(assoc-in [:wallet :ui :send :loading-suggested-routes?] false) (assoc-in [:wallet :ui :send :loading-suggested-routes?] false)
@ -491,26 +489,48 @@
{:event :wallet/stop-get-suggested-routes {:event :wallet/stop-get-suggested-routes
:error error}))}]})) :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] (fn [_ data]
(if-let [error (some-> data (if-let [{:keys [code details]} (-> data :ErrorResponse first)]
first (let [error-message (if (= code "0") "An error occurred" details)]
:ErrorResponse
(#(if (= (:code %) "0") "An error occurred" (:details %))))]
(do
(log/error "failed to get suggested routes (async)" (log/error "failed to get suggested routes (async)"
{:event :wallet/handle-suggested-routes {:event :wallet/handle-suggested-routes
:error error}) :error error-message})
{:fx [[:dispatch [:wallet/suggested-routes-error error]]]}) {:fx [[:dispatch [:wallet/suggested-routes-error error-message]]]})
(let [suggested-routes-new-data (data-store/rpc->suggested-routes data) (let [best-routes-fix (comp ->old-route-paths
suggested-routes (-> suggested-routes-new-data remove-invalid-bonder-fees-routes
first remove-multichain-routes)
;; if route is multichain, we remove it candidates-fix (comp ->old-route-paths
(update :best (fn [best] (if (> (count best) 1) [] best))) remove-invalid-bonder-fees-routes)
(update :best #(map data-store/new->old-route-path %)) routes (-> data
(update :candidates #(map data-store/new->old-route-path %)))] (first)
{:fx [[:dispatch (data-store/rpc->suggested-routes)
[:wallet/suggested-routes-success 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 (rf/reg-event-fx :wallet/add-authorized-transaction
(fn [{:keys [db]} [transaction]] (fn [{:keys [db]} [transaction]]