Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement proper error handling for swaps #21261

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/quo/components/banners/alert_banner/schema.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
[:text {:optional true} [:maybe string?]]
[:container-style {:optional true} [:maybe :map]]
[:button-text {:optional true} [:maybe string?]]
[:text-number-of-lines {:optional true} [:maybe number?]]
[:on-button-press {:optional true} [:maybe fn?]]]]]
:any])
14 changes: 11 additions & 3 deletions src/quo/components/banners/alert_banner/style.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
:padding-vertical 12}
container-style))

(def content-container
{:flex 1
:flex-direction :row})

(defn label
[theme]
{:flex 1
:color (colors/resolve-color :danger theme)
:margin-horizontal 4})
{:color (colors/resolve-color :danger theme)
:margin-horizontal 4
:flex 1
:flex-wrap :wrap})

(def button-text
{:color colors/white})

(def icon
{:margin-top 2})
22 changes: 12 additions & 10 deletions src/quo/components/banners/alert_banner/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[schema.core :as schema]))

(defn- view-internal
[{:keys [action? text button-text container-style on-button-press]}]
[{:keys [action? text button-text text-number-of-lines container-style on-button-press]}]
(let [theme (quo.theme/use-theme)]
[rn/view
{:accessibility-label :alert-banner}
Expand All @@ -24,15 +24,17 @@
colors/danger-50-opa-10
theme)
colors/danger-50-opa-0]}
[icon/icon
:i/alert
{:color (colors/resolve-color :danger theme)
:size 16}]
[text/text
{:style (style/label theme)
:size :paragraph-2
:number-of-lines 1}
text]
[rn/view {:style style/content-container}
[icon/icon
:i/alert
{:color (colors/resolve-color :danger theme)
:size 16
:container-style style/icon}]
[text/text
{:style (style/label theme)
:size :paragraph-2
:number-of-lines (or text-number-of-lines 1)}
text]]
(when action?
[button/button
{:accessibility-label :button
Expand Down
2 changes: 1 addition & 1 deletion src/quo/components/buttons/button/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
(set-pressed-state nil)
(when on-press-out (on-press-out))))]
[rn/touchable-without-feedback
{:disabled disabled?
{:disabled (boolean disabled?)
:accessibility-label accessibility-label
:on-press-in on-press-in-cb
:on-press-out on-press-out-cb
Expand Down
13 changes: 7 additions & 6 deletions src/quo/components/settings/data_item/style.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@
:height 16})

(defn description
[blur? theme]
{:color (if blur?
colors/white
(colors/theme-colors colors/neutral-100
colors/white
theme))})
[subtitle-color blur? theme]
{:color (or subtitle-color
(if blur?
colors/white
(colors/theme-colors colors/neutral-100
colors/white
theme)))})
(def right-icon
{:margin-left 12})

Expand Down
9 changes: 6 additions & 3 deletions src/quo/components/settings/data_item/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[rn/view {:style (style/loading-container size blur? theme)}]))

(defn- left-subtitle
[{:keys [size subtitle-type icon icon-color blur? subtitle customization-color emoji network-image]
[{:keys [size subtitle-type subtitle-color icon icon-color blur? subtitle customization-color emoji
network-image]
:or {subtitle-type :default}}]
(let [theme (quo.theme/use-theme)]
[rn/view {:style style/subtitle-container}
Expand All @@ -40,7 +41,7 @@
[text/text
{:weight :medium
:size :paragraph-2
:style (style/description blur? theme)}
:style (style/description subtitle-color blur? theme)}
subtitle]
(when (= subtitle-type :editable)
[icons/icon :i/edit
Expand All @@ -65,7 +66,7 @@

(defn- left-side
"The description can either be given as a string `subtitle-type` or a component `custom-subtitle`"
[{:keys [title status size blur? custom-subtitle icon subtitle subtitle-type icon-color
[{:keys [title status size blur? custom-subtitle icon subtitle subtitle-type subtitle-color icon-color
customization-color network-image emoji]
:as props}]
(let [theme (quo.theme/use-theme)]
Expand All @@ -85,6 +86,7 @@
{:theme theme
:size size
:subtitle-type subtitle-type
:subtitle-color subtitle-color
:icon icon
:icon-color icon-color
:blur? blur?
Expand Down Expand Up @@ -124,6 +126,7 @@
[:icon-color {:optional true} [:maybe :schema.common/customization-color]]
[:status {:optional true} [:maybe [:enum :default :loading]]]
[:subtitle-type {:optional true} [:maybe [:enum :default :icon :network :account :editable]]]
[:subtitle-color {:optional true} [:maybe :schema.common/customization-color]]
[:size {:optional true} [:maybe [:enum :default :small :large]]]
[:title :string]
[:subtitle {:optional true} [:maybe [:or :string :double]]]
Expand Down
19 changes: 11 additions & 8 deletions src/status_im/common/controlled_input/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[status-im.contexts.wallet.common.utils :as wallet-utils]
[utils.money :as money]))

(def ^:private default-max-limit 12)

(def init-state
{:value ""
:error? false
Expand Down Expand Up @@ -101,9 +103,8 @@
(def ^:private dot ".")

(defn- can-add-character?
[state character]
(let [max-length 12
current (input-value state)
[state character max-length]
(let [current (input-value state)
length-overflow? (>= (count current) max-length)
extra-dot? (and (= character dot) (string/includes? current dot))
extra-leading-zero? (and (= current "0") (= "0" (str character)))
Expand All @@ -123,11 +124,13 @@
(str value character)))

(defn add-character
[state character]
(if (can-add-character? state character)
(set-input-value state
(normalize-value-as-numeric (input-value state) character))
state))
([state character]
(add-character state character default-max-limit))
([state character max-length]
(if (can-add-character? state character max-length)
(set-input-value state
(normalize-value-as-numeric (input-value state) character))
state)))

(defn delete-last
([state]
Expand Down
7 changes: 7 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,10 @@

(def ^:const min-token-decimals-to-display 6)
(def ^:const swap-proposal-refresh-interval-ms 15000)

(def router-error-code-generic "0")
(def router-error-code-paraswap-custom-error "WPP-030")
(def router-error-code-price-timeout "WPP-037")
(def router-error-code-not-enough-liquidity "WPP-038")
(def router-error-code-price-impact-too-high "WPP-039")
(def router-error-code-not-enough-native-balance "WR-002")
Comment on lines +613 to +618
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a code style comment, but can we have a map for the errors to make it easier on the eyes 😅? I see that we do it is as above in mostly all cases for constants, but to me it would be a bit cleaner this way:

(def router-errors
  {:error/generic              "0"
   :error/paraswap-custom      "WPP-030"
   :error/price-timeout        "WP-037"
   :error/not-enough-liquidity "WPP-038"})

;; when asserting the error code
(= error-response-code
   (:error/not-enough-liquidity constants/router-errors))

it could also be just me, so I wonder if others have any preferences, cause going through the constants namespace gets more and more annoying recently and I think it's due to the structure of things.

cc: @ilmotta @ulisesmac is there some clojure-friendly way of doing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I started using a map and then changed to something similar as the desktop implementation. Open to suggestions on how to improve the approach :)

4 changes: 2 additions & 2 deletions src/status_im/contexts/wallet/sheets/buy_token/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
(def ^:private initial-tab (:id (first tabs)))

(defn view
[]
[{:keys [title]}]
(rn/use-mount (fn []
(rf/dispatch [:wallet/get-crypto-on-ramps])))
(let [crypto-on-ramps (rf/sub [:wallet/crypto-on-ramps])
Expand All @@ -45,7 +45,7 @@
#(set-min-height
(oops/oget % :nativeEvent :layout :height)))]
[:<>
[quo/drawer-top {:title (i18n/label :t/buy-assets)}]
[quo/drawer-top {:title (or title (i18n/label :t/buy-assets))}]
[quo/segmented-control
{:size 32
:container-style style/tabs
Expand Down
16 changes: 2 additions & 14 deletions src/status_im/contexts/wallet/swap/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,8 @@
assoc
:swap-proposal (when-not (empty? best-routes)
(assoc (first best-routes) :uuid request-uuid))
:error-response (when (empty? best-routes) error-response)
:error-response error-response
:loading-swap-proposal? false)}
(empty? best-routes)
(assoc :fx
[[:dispatch
[:toasts/upsert
{:id :swap-proposal-error
:type :negative
:text error-response}]]])
;; Router is unstable and it can return a swap proposal and after auto-refetching it can
;; return an error. Ideally this shouldn't happen, but adding this behavior so if the
;; user is in swap confirmation screen or in token approval confirmation screen, we
Expand All @@ -157,12 +150,7 @@
{:db (-> db
(update-in [:wallet :ui :swap] dissoc :route :swap-proposal)
(assoc-in [:wallet :ui :swap :loading-swap-proposal?] false)
(assoc-in [:wallet :ui :swap :error-response] error-message))
:fx [[:dispatch
[:toasts/upsert
{:id :swap-proposal-error
:type :negative
:text error-message}]]]}))
(assoc-in [:wallet :ui :swap :error-response] error-message))}))

(rf/reg-event-fx :wallet/stop-get-swap-proposal
(fn []
Expand Down
4 changes: 3 additions & 1 deletion src/status_im/contexts/wallet/swap/setup_swap/style.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
:justify-content :flex-end})

(def alert-banner
{:height 40})
{:height :auto
:min-height 40
:max-height 62})
Loading