Skip to content

Commit 16b2c90

Browse files
feat: show swap proposal exchange rate (#22078)
Signed-off-by: Brian Sztamfater <[email protected]>
1 parent a210d59 commit 16b2c90

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

src/status_im/contexts/wallet/swap/setup_swap/style.cljs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
(ns status-im.contexts.wallet.swap.setup-swap.style)
1+
(ns status-im.contexts.wallet.swap.setup-swap.style
2+
(:require [quo.foundations.colors :as colors]))
23

34
(def container
45
{:flex 1})
@@ -40,3 +41,22 @@
4041
{:height :auto
4142
:min-height 40
4243
:max-height 62})
44+
45+
(defn exchange-rate-loader
46+
[theme]
47+
{:margin-top 16
48+
:width 72
49+
:height 14
50+
:border-radius 6
51+
:background-color (colors/theme-colors colors/neutral-5 colors/neutral-90 theme)})
52+
53+
(def exchange-rate-container
54+
{:margin-top 16
55+
:flex-direction :row})
56+
57+
(def exchange-rate-crypto-label
58+
{:color colors/neutral-50})
59+
60+
(defn exchange-rate-fiat-label
61+
[theme]
62+
{:color (colors/theme-colors colors/neutral-40 colors/neutral-60 theme)})

src/status_im/contexts/wallet/swap/setup_swap/view.cljs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,33 @@
343343
(= (:symbol token)
344344
(:symbol asset-to-pay)))}]))
345345

346+
(defn- swap-exchange-rate-view
347+
[]
348+
(let [theme (quo.theme/use-theme)
349+
asset-to-pay (rf/sub [:wallet/swap-asset-to-pay])
350+
asset-to-receive (rf/sub [:wallet/swap-asset-to-receive])
351+
swap-exchange-rate-crypto (rf/sub [:wallet/swap-exchange-rate-crypto])
352+
swap-exchange-rate-fiat (rf/sub [:wallet/swap-exchange-rate-fiat])
353+
loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?])]
354+
(cond
355+
loading-swap-proposal?
356+
[rn/view {:style (style/exchange-rate-loader theme)}]
357+
swap-exchange-rate-crypto
358+
[rn/view {:style style/exchange-rate-container}
359+
[quo/text
360+
{:weight :medium
361+
:size :paragraph-2
362+
:style style/exchange-rate-crypto-label}
363+
(i18n/label :t/swap-exchange-rate-in-crypto
364+
{:receive-token-symbol (:symbol asset-to-receive)
365+
:exchange-rate swap-exchange-rate-crypto
366+
:pay-token-symbol (:symbol asset-to-pay)})]
367+
[quo/text
368+
{:weight :medium
369+
:size :paragraph-2
370+
:style (style/exchange-rate-fiat-label theme)}
371+
(str " (" swap-exchange-rate-fiat ")")]])))
372+
346373
(defn view
347374
[]
348375
(let [[pay-input-state set-pay-input-state] (rn/use-state controlled-input/init-state)
@@ -495,7 +522,7 @@
495522
:params {:show-account-balances? true
496523
:asset-symbol (:symbol asset-to-pay)
497524
:network network}}]
498-
[rn/view {:style style/inputs-container}
525+
[rn/scroll-view {:style style/inputs-container}
499526
[pay-token-input
500527
{:input-state pay-input-state
501528
:on-max-press on-max-press
@@ -514,7 +541,8 @@
514541
[receive-token-input
515542
{:input-focused? (not pay-input-focused?)
516543
:on-token-press #(rf/dispatch [:show-bottom-sheet {:content receive-token-bottom-sheet}])
517-
:on-input-focus #(set-pay-input-focused? false)}]]
544+
:on-input-focus #(set-pay-input-focused? false)}]
545+
[swap-exchange-rate-view]]
518546
[rn/view {:style style/footer-container}
519547
(when-not loading-swap-proposal? [alert-banner {:pay-input-error? pay-input-error?}])
520548
(when (or loading-swap-proposal? swap-proposal)

src/status_im/subs/wallet/swap.cljs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,43 @@
333333
:token token-for-fees
334334
:prices-per-token prices-per-token})]
335335
fee-in-fiat)))
336+
337+
(rf/reg-sub
338+
:wallet/swap-exchange-rate-crypto
339+
:<- [:wallet/swap-proposal-amount-in]
340+
:<- [:wallet/swap-asset-to-pay]
341+
:<- [:wallet/swap-proposal-amount-out]
342+
:<- [:wallet/swap-asset-to-receive]
343+
(fn [[amount-in asset-to-pay amount-out asset-to-receive]]
344+
(let [pay-token-decimals (:decimals asset-to-pay)
345+
receive-token-decimals (:decimals asset-to-receive)
346+
amount-in-num (some-> amount-in
347+
(number/hex->whole pay-token-decimals)
348+
(number/to-fixed pay-token-decimals)
349+
(money/->bignumber))
350+
amount-out-num (some-> amount-out
351+
(number/hex->whole receive-token-decimals)
352+
(number/to-fixed receive-token-decimals)
353+
(money/->bignumber))
354+
exchange-rate (when (and amount-in-num amount-out-num)
355+
(money/div amount-in-num amount-out-num))
356+
display-decimals (min pay-token-decimals
357+
constants/min-token-decimals-to-display)]
358+
(when exchange-rate
359+
(number/to-fixed exchange-rate display-decimals)))))
360+
361+
(rf/reg-sub
362+
:wallet/swap-exchange-rate-fiat
363+
:<- [:wallet/swap-exchange-rate-crypto]
364+
:<- [:wallet/swap-asset-to-pay]
365+
:<- [:profile/currency]
366+
:<- [:profile/currency-symbol]
367+
:<- [:wallet/prices-per-token]
368+
(fn [[swap-exchange-rate asset-to-pay currency currency-symbol prices-per-token]]
369+
(when swap-exchange-rate
370+
(let [fiat-value (utils/calculate-token-fiat-value
371+
{:currency currency
372+
:balance swap-exchange-rate
373+
:token asset-to-pay
374+
:prices-per-token prices-per-token})]
375+
(utils/fiat-formatted-for-ui currency-symbol fiat-value)))))

translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,7 @@
25662566
"sun": "Sun",
25672567
"swap": "Swap",
25682568
"swap-details": "Swap details",
2569+
"swap-exchange-rate-in-crypto": "1 {{receive-token-symbol}} ≈ {{exchange-rate}} {{pay-token-symbol}}",
25692570
"swap-failed": "Swap failed",
25702571
"swapped-to": "Swapped {{pay-amount}} {{pay-token-symbol}} to {{receive-amount}} {{receive-token-symbol}}",
25712572
"swapping-to": "Swapping {{pay-amount}} {{pay-token-symbol}} to {{receive-amount}} {{receive-token-symbol}}",

0 commit comments

Comments
 (0)