Skip to content

Commit 23eeb62

Browse files
feat(swap): fetch swap proposals
Signed-off-by: Brian Sztamfater <[email protected]>
1 parent 96b88a4 commit 23eeb62

File tree

19 files changed

+653
-167
lines changed

19 files changed

+653
-167
lines changed

src/quo/components/banners/alert_banner/schema.cljs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[:map {:closed true}
88
[:action? {:optional true} [:maybe boolean?]]
99
[:text {:optional true} [:maybe string?]]
10+
[:container-style {:optional true} [:maybe :map]]
1011
[:button-text {:optional true} [:maybe string?]]
1112
[:on-button-press {:optional true} [:maybe fn?]]]]]
1213
:any])

src/quo/components/banners/alert_banner/style.cljs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
(ns quo.components.banners.alert-banner.style
22
(:require [quo.foundations.colors :as colors]))
33

4-
(def container
5-
{:flex-direction :row
6-
:align-items :center
7-
:height 50
8-
:padding-horizontal 20
9-
:padding-vertical 12})
4+
(defn container
5+
[container-style]
6+
(merge
7+
{:flex-direction :row
8+
:align-items :center
9+
:height 50
10+
:padding-horizontal 20
11+
:padding-vertical 12}
12+
container-style))
1013

1114
(defn label
1215
[theme]

src/quo/components/banners/alert_banner/view.cljs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
[schema.core :as schema]))
1212

1313
(defn- view-internal
14-
[{:keys [action? text button-text on-button-press]}]
14+
[{:keys [action? text button-text container-style on-button-press]}]
1515
(let [theme (quo.theme/use-theme)]
1616
[rn/view
1717
{:accessibility-label :alert-banner}
1818
[linear-gradient/linear-gradient
19-
{:style style/container
19+
{:style (style/container container-style)
2020
:start {:x 0 :y 0}
2121
:end {:x 0 :y 1}
2222
:colors [(colors/theme-colors

src/quo/components/settings/data_item/view.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
[:subtitle-type {:optional true} [:maybe [:enum :default :icon :network :account :editable]]]
127127
[:size {:optional true} [:maybe [:enum :default :small :large]]]
128128
[:title :string]
129-
[:subtitle {:optional true} [:maybe :string]]
129+
[:subtitle {:optional true} [:maybe [:or :string :double]]]
130130
[:custom-subtitle {:optional true} [:maybe fn?]]
131131
[:icon {:optional true} [:maybe :keyword]]
132132
[:emoji {:optional true} [:maybe :string]]

src/quo/components/wallet/swap_input/style.cljs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns quo.components.wallet.swap-input.style
22
(:require [quo.foundations.colors :as colors]
3+
[quo.foundations.shadows :as shadows]
34
[quo.foundations.typography :as typography]))
45

56
(defn- border-color
@@ -11,11 +12,13 @@
1112
(colors/theme-colors colors/neutral-5 colors/neutral-90 theme))
1213

1314
(defn content
14-
[theme]
15-
{:border-width 1
16-
:border-radius 16
17-
:border-color (border-color theme)
18-
:background-color (colors/theme-colors colors/white colors/neutral-95 theme)})
15+
[typing? theme]
16+
(merge
17+
{:border-width 1
18+
:border-radius 16
19+
:border-color (border-color theme)
20+
:background-color (colors/theme-colors colors/white colors/neutral-95 theme)}
21+
(when typing? (shadows/get 1 theme))))
1922

2023
(defn row-1
2124
[loading?]

src/quo/components/wallet/swap_input/view.cljs

+11-5
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@
1919
[:props
2020
[:map {:closed true}
2121
[:type {:optional true} [:maybe [:enum :pay :receive]]]
22-
[:status {:optional true} [:maybe [:enum :default :disabled :loading]]]
22+
[:status {:optional true} [:maybe [:enum :default :typing :disabled :loading]]]
2323
[:token {:optional true} [:maybe :string]]
2424
[:value {:optional true} [:maybe :string]]
2525
[:default-value {:optional true} [:maybe :string]]
2626
[:currency-symbol {:optional true} [:maybe :string]]
2727
[:fiat-value {:optional true} [:maybe :string]]
2828
[:show-approval-label? {:optional true} [:maybe :boolean]]
29+
[:auto-focus? {:optional true} [:maybe :boolean]]
30+
[:input-disabled? {:optional true} [:maybe :boolean]]
2931
[:error? {:optional true} [:maybe :boolean]]
3032
[:show-keyboard? {:optional true} [:maybe :boolean]]
3133
[:approval-label-props {:optional true} [:maybe approval-label.schema/?schema]]
3234
[:network-tag-props {:optional true} [:maybe :map]]
3335
[:on-change-text {:optional true} [:maybe fn?]]
3436
[:enable-swap? {:optional true} [:maybe :boolean]]
3537
[:on-swap-press {:optional true} [:maybe fn?]]
38+
[:on-input-focus {:optional true} [:maybe fn?]]
3639
[:on-token-press {:optional true} [:maybe fn?]]
3740
[:on-max-press {:optional true} [:maybe fn?]]
3841
[:customization-color {:optional true} [:maybe :schema.common/customization-color]]
@@ -41,13 +44,14 @@
4144

4245
(defn view-internal
4346
[{:keys [type status token value fiat-value show-approval-label? error? network-tag-props
44-
approval-label-props default-value enable-swap?
47+
approval-label-props default-value auto-focus? input-disabled? enable-swap?
4548
currency-symbol on-change-text show-keyboard?
46-
container-style on-swap-press on-token-press on-max-press]}]
49+
container-style on-swap-press on-token-press on-max-press on-input-focus]}]
4750
(let [theme (quo.theme/use-theme)
4851
pay? (= type :pay)
4952
disabled? (= status :disabled)
5053
loading? (= status :loading)
54+
typing? (= status :typing)
5155
controlled-input? (some? value)
5256
input-ref (rn/use-ref-atom nil)
5357
set-input-ref (rn/use-callback (fn [ref] (reset! input-ref ref)) [])
@@ -58,7 +62,7 @@
5862
[rn/view
5963
{:style container-style
6064
:accessibility-label :swap-input}
61-
[rn/view {:style (style/content theme)}
65+
[rn/view {:style (style/content typing? theme)}
6266
[rn/view
6367
{:style (style/row-1 loading?)}
6468
[rn/pressable {:on-press on-token-press}
@@ -78,7 +82,9 @@
7882
colors/neutral-50
7983
theme)
8084
:keyboard-type :numeric
81-
:auto-focus true
85+
:editable (not input-disabled?)
86+
:auto-focus auto-focus?
87+
:on-focus on-input-focus
8288
:on-change-text on-change-text
8389
:show-soft-input-on-focus show-keyboard?
8490
:default-value default-value

src/status_im/constants.cljs

+3
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@
553553
(def ^:const send-type-bridge 5)
554554
(def ^:const send-type-erc-721-transfer 6)
555555
(def ^:const send-type-erc-1155-transfer 7)
556+
(def ^:const send-type-swap 8)
556557

557558
(def ^:const multi-transaction-type-send 0)
558559
(def ^:const multi-transaction-type-approve 1)
@@ -598,3 +599,5 @@
598599
:color :blue
599600
:contract-address "0xdef171fe48cf0115b1d80b88dc8eab59176fee57"
600601
:terms-and-conditions-url "https://files.paraswap.io/tos_v4.pdf"})
602+
603+
(def ^:const token-for-fees-symbol "ETH")

src/status_im/contexts/wallet/send/events.cljs

+23-17
Original file line numberDiff line numberDiff line change
@@ -518,23 +518,29 @@
518518

519519
(rf/reg-event-fx
520520
:wallet/handle-suggested-routes
521-
(fn [_ [data]]
522-
(if-let [{:keys [code details]} (-> data :ErrorResponse)]
523-
(let [error-message (if (= code "0") "An error occurred" details)]
524-
(log/error "failed to get suggested routes (async)"
525-
{:event :wallet/handle-suggested-routes
526-
:error error-message})
527-
{:fx [[:dispatch [:wallet/suggested-routes-error error-message]]]})
528-
(let [best-routes-fix (comp ->old-route-paths
529-
remove-invalid-bonder-fees-routes
530-
remove-multichain-routes)
531-
candidates-fix (comp ->old-route-paths
532-
remove-invalid-bonder-fees-routes)
533-
routes (-> data
534-
(data-store/rpc->suggested-routes)
535-
(update :best best-routes-fix)
536-
(update :candidates candidates-fix))]
537-
{:fx [[:dispatch [:wallet/suggested-routes-success routes]]]}))))
521+
(fn [{:keys [db]} [data]]
522+
(let [swap? (get-in db [:wallet :ui :swap])
523+
{:keys [code details] :as error-response} (-> data :ErrorResponse)]
524+
(if (and (not swap?) error-response)
525+
(let [error-message (if (= code "0") "An error occurred" details)]
526+
(log/error "failed to get suggested routes (async)"
527+
{:event :wallet/handle-suggested-routes
528+
:error error-message})
529+
{:fx [(if swap?
530+
[:dispatch [:wallet/swap-proposal-error error-message]]
531+
[:dispatch [:wallet/suggested-routes-error error-message]])]})
532+
(let [best-routes-fix (comp ->old-route-paths
533+
remove-invalid-bonder-fees-routes
534+
remove-multichain-routes)
535+
candidates-fix (comp ->old-route-paths
536+
remove-invalid-bonder-fees-routes)
537+
routes (-> data
538+
(data-store/rpc->suggested-routes)
539+
(update :best best-routes-fix)
540+
(update :candidates candidates-fix))]
541+
{:fx [(if swap?
542+
[:dispatch [:wallet/swap-proposal-success routes]]
543+
[:dispatch [:wallet/suggested-routes-success routes]])]})))))
538544

539545
(rf/reg-event-fx :wallet/add-authorized-transaction
540546
(fn [{:keys [db]} [transaction]]

src/status_im/contexts/wallet/swap/events.cljs

+110-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(ns status-im.contexts.wallet.swap.events
22
(:require [re-frame.core :as rf]
33
[status-im.constants :as constants]
4+
[status-im.contexts.wallet.send.utils :as send-utils]
45
[status-im.contexts.wallet.sheets.network-selection.view :as network-selection]
6+
[taoensso.timbre :as log]
57
[utils.number]))
68

79
(rf/reg-event-fx :wallet.swap/start
@@ -14,7 +16,9 @@
1416
(assoc-in [:wallet :ui :swap :asset-to-pay] token)
1517
(assoc-in [:wallet :ui :swap :network] network))
1618
:fx (if network
17-
[[:dispatch [:navigate-to :screen/wallet.swap-propasal]]
19+
[[:dispatch
20+
[:navigate-to-within-stack
21+
[:screen/wallet.setup-swap :screen/wallet.swap-select-asset-to-pay]]]
1822
[:dispatch [:wallet.swap/set-default-slippage]]]
1923
[[:dispatch
2024
[:show-bottom-sheet
@@ -30,10 +34,6 @@
3034
:stack-id
3135
:screen/wallet.swap-select-asset-to-pay}]))}])}]]])}))
3236

33-
(rf/reg-event-fx :wallet.swap/clean-asset-to-pay
34-
(fn [{:keys [db]}]
35-
{:db (update-in db [:wallet :ui :swap] dissoc :asset-to-pay)}))
36-
3737
(rf/reg-event-fx :wallet.swap/set-default-slippage
3838
(fn [{:keys [db]}]
3939
{:db
@@ -47,18 +47,113 @@
4747
(fn [{:keys [db]} [{:keys [token]}]]
4848
{:db (assoc-in db [:wallet :ui :swap :asset-to-receive] token)}))
4949

50-
(rf/reg-event-fx :wallet.swap/set-pay-amount
51-
(fn [{:keys [db]} [amount]]
52-
{:db (assoc-in db [:wallet :ui :swap :pay-amount] amount)}))
50+
(rf/reg-event-fx :wallet.swap/recalculate-fees
51+
(fn [{:keys [db]} [loading-fees?]]
52+
{:db (assoc-in db [:wallet :ui :swap :loading-fees?] loading-fees?)}))
53+
54+
(rf/reg-event-fx :wallet/start-get-swap-proposal
55+
(fn [{:keys [db]} [{:keys [amount-in amount-out]}]]
56+
(let [wallet-address (get-in db [:wallet :current-viewing-account-address])
57+
{:keys [asset-to-pay asset-to-receive
58+
network]} (get-in db [:wallet :ui :swap])
59+
test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?])
60+
networks ((if test-networks-enabled? :test :prod)
61+
(get-in db [:wallet :networks]))
62+
network-chain-ids (map :chain-id networks)
63+
pay-token-decimal (:decimals asset-to-pay)
64+
pay-token-id (:symbol asset-to-pay)
65+
receive-token-id (:symbol asset-to-receive)
66+
receive-token-decimals (:decimals asset-to-receive)
67+
gas-rates constants/gas-rate-medium
68+
amount-in-hex (if amount-in
69+
(send-utils/amount-in-hex amount-in pay-token-decimal)
70+
0)
71+
amount-out-hex (when amount-out
72+
(send-utils/amount-in-hex amount-out receive-token-decimals))
73+
to-address wallet-address
74+
from-address wallet-address
75+
swap-chain-id (:chain-id network)
76+
disabled-to-chain-ids (filter #(not= % swap-chain-id) network-chain-ids)
77+
disabled-from-chain-ids (filter #(not= % swap-chain-id) network-chain-ids)
78+
from-locked-amount {}
79+
send-type constants/send-type-swap
80+
request-uuid (str (random-uuid))
81+
params [(cond->
82+
{:uuid request-uuid
83+
:sendType send-type
84+
:addrFrom from-address
85+
:addrTo to-address
86+
:tokenID pay-token-id
87+
:toTokenID receive-token-id
88+
:disabledFromChainIDs disabled-from-chain-ids
89+
:disabledToChainIDs disabled-to-chain-ids
90+
:gasFeeMode gas-rates
91+
:fromLockedAmount from-locked-amount}
92+
amount-in (assoc :amountIn amount-in-hex)
93+
amount-out (assoc :amountOut amount-out-hex))]]
94+
(when-let [amount (or amount-in amount-out)]
95+
{:db (update-in db
96+
[:wallet :ui :swap]
97+
#(-> %
98+
(assoc
99+
:last-request-uuid request-uuid
100+
:amount amount
101+
:loading-swap-proposal? true)
102+
(dissoc :error-response)))
103+
:json-rpc/call [{:method "wallet_getSuggestedRoutesV2Async"
104+
:params params
105+
:on-error (fn [error]
106+
(rf/dispatch [:wallet/swap-proposal-error error])
107+
(log/error "failed to get suggested routes (async)"
108+
{:event :wallet/start-get-swap-proposal
109+
:error (:message error)
110+
:params params}))}]}))))
53111

54-
(rf/reg-event-fx :wallet.swap/set-swap-proposal
112+
(rf/reg-event-fx :wallet/swap-proposal-success
55113
(fn [{:keys [db]} [swap-proposal]]
56-
{:db (assoc-in db [:wallet :ui :swap :swap-proposal] swap-proposal)}))
114+
(let [last-request-uuid (get-in db [:wallet :ui :swap :last-request-uuid])
115+
request-uuid (:uuid swap-proposal)
116+
best-routes (:best swap-proposal)
117+
error-response (:error-response swap-proposal)]
118+
(when (= request-uuid last-request-uuid)
119+
{:db (update-in db
120+
[:wallet :ui :swap]
121+
assoc
122+
:swap-proposal (first best-routes)
123+
:error-response (when (empty? best-routes) error-response)
124+
:loading-swap-proposal? false)}))))
57125

58-
(rf/reg-event-fx :wallet.swap/set-provider
126+
(rf/reg-event-fx :wallet/swap-proposal-error
127+
(fn [{:keys [db]} [error-message]]
128+
{:db (-> db
129+
(update-in [:wallet :ui :swap] dissoc :route :swap-proposal)
130+
(assoc-in [:wallet :ui :swap :loading-swap-proposal?] false)
131+
(assoc-in [:wallet :ui :swap :error-response] error-message))
132+
:fx [[:dispatch
133+
[:toasts/upsert
134+
{:id :swap-proposal-error
135+
:type :negative
136+
:text error-message}]]]}))
137+
138+
(rf/reg-event-fx :wallet/stop-get-swap-proposal
139+
(fn []
140+
{:json-rpc/call [{:method "wallet_stopSuggestedRoutesV2AsyncCalcualtion"
141+
:params []
142+
:on-error (fn [error]
143+
(log/error "failed to stop fetching swap proposals"
144+
{:event :wallet/stop-get-swap-proposal
145+
:error error}))}]}))
146+
147+
(rf/reg-event-fx :wallet/clean-swap-proposal
59148
(fn [{:keys [db]}]
60-
{:db (assoc-in db [:wallet :ui :swap :providers] [constants/swap-default-provider])}))
149+
{:db (update-in db
150+
[:wallet :ui :swap]
151+
dissoc
152+
:last-request-uuid
153+
:swap-proposal
154+
:error-response
155+
:loading-swap-proposal?)}))
61156

62-
(rf/reg-event-fx :wallet.swap/recalculate-fees
63-
(fn [{:keys [db]} [loading-fees?]]
64-
{:db (assoc-in db [:wallet :ui :swap :loading-fees?] loading-fees?)}))
157+
(rf/reg-event-fx :wallet/clean-swap
158+
(fn [{:keys [db]}]
159+
{:db (update-in db [:wallet :ui] dissoc :swap)}))

0 commit comments

Comments
 (0)