Skip to content

Commit ac07213

Browse files
committed
Swap and bridge improvements
1 parent 9d5f6d0 commit ac07213

File tree

5 files changed

+210
-67
lines changed

5 files changed

+210
-67
lines changed

src/status_im/common/signals/events.cljs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@
3939
"wallet.suggested.routes"
4040
{:fx [[:dispatch [:wallet/handle-suggested-routes (transforms/js->clj event-js)]]]}
4141

42+
"wallet.router.sending-transactions-started"
43+
{:fx [[:dispatch
44+
[:wallet/sending-transactions-started-signal-received (transforms/js->clj event-js)]]]}
45+
4246
"wallet.router.sign-transactions"
4347
{:fx [[:dispatch [:wallet/sign-transactions-signal-received (transforms/js->clj event-js)]]]}
4448

4549
"wallet.router.transactions-sent"
4650
{:fx [[:dispatch [:wallet/transactions-sent-signal-received (transforms/js->clj event-js)]]]}
4751

52+
"wallet.transaction.status-changed"
53+
{:fx [[:dispatch [:wallet/status-changed-signal-received (transforms/js->clj event-js)]]]}
54+
4855
"envelope.sent"
4956
(messages.transport/update-envelopes-status
5057
cofx

src/status_im/contexts/wallet/common/utils.cljs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,87 @@
480480
(filter (fn [{:keys [tokens]}]
481481
(some positive-balance-in-any-chain? tokens))
482482
operable-account))))
483+
484+
(defn send-details-map
485+
"Generates a new map with all the sen details using the original
486+
details we've received from `status-go`. The new one contains only
487+
the keys we need."
488+
[send-details]
489+
(let [{:keys [uuid
490+
sendType
491+
fromAddress
492+
toAddress
493+
fromChain
494+
toChain
495+
fromAmount
496+
toAmount
497+
fromAsset
498+
toAsset
499+
username
500+
publicKey
501+
packId]} send-details]
502+
{:uuid uuid
503+
:send-type sendType
504+
:address-from fromAddress
505+
:address-to toAddress
506+
:tx-to ""
507+
:from-chain fromChain
508+
:to-chain toChain
509+
:from-amount fromAmount
510+
:to-amount toAmount
511+
:from-asset fromAsset
512+
:to-asset toAsset
513+
:username username
514+
:public-key publicKey
515+
:pack-id packId
516+
:tx-hash ""
517+
:approval-tx? false}))
518+
519+
(defn details-map
520+
"Generates map with all the transaction details based on what we've
521+
got from status-go as `send-details` and `sent-transaction`."
522+
[send-details sent-transaction]
523+
(let [send-details (send-details-map send-details)
524+
{:keys [toAddress fromChain toChain amountIn
525+
amountOut fromToken toToken hash
526+
approvalTx]} sent-transaction
527+
amount-in (money/from-hex amountIn)
528+
amount-out (money/from-hex amountOut)
529+
sent-transaction? (and sent-transaction (> (-> sent-transaction :hash count) 0))]
530+
(if sent-transaction?
531+
(cond-> send-details
532+
true (assoc :tx-to toAddress)
533+
(> fromChain 0) (assoc :from-chain fromChain)
534+
(> toChain 0) (assoc :to-chain toChain)
535+
(not= amount-in "0") (assoc :from-amount amount-in)
536+
(not= amount-out "0") (assoc :to-amount amount-out)
537+
(> (count fromToken) 0) (assoc :from-asset fromToken)
538+
(> (count toToken) 0) (assoc :to-asset toToken)
539+
true (assoc :tx-hash hash)
540+
true (assoc :approval-tx? approvalTx))
541+
send-details)))
542+
543+
(defn contact-name-by-address
544+
[db address]
545+
(or (get-in db [:wallet :accounts address :name])
546+
(get-in db [:contacts/contacts address :primary-name])))
547+
548+
(defn tx-to-name
549+
"Returns the transaction name that will be used for certain types of transactions."
550+
[send-type]
551+
(cond
552+
(= send-type constants/send-type-bridge) "Hop"
553+
(= send-type constants/send-type-swap) "ParaSwap"
554+
:else nil))
555+
556+
(defn transaction-approval-required?
557+
"Indicates whether the transaction needs approval based on the information
558+
from the database."
559+
[transactions {:keys [swap-proposal approval-transaction-id]}]
560+
(let [approval-transaction (when approval-transaction-id
561+
(get transactions approval-transaction-id))
562+
already-approved? (and approval-transaction
563+
(= (:status approval-transaction)
564+
:confirmed))]
565+
(and (:approval-required swap-proposal)
566+
(not already-approved?))))

src/status_im/contexts/wallet/events.cljs

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -783,17 +783,72 @@
783783

784784
(rf/reg-event-fx
785785
:wallet/transactions-sent-signal-received
786-
(fn [{:keys [db]}
786+
(fn [_
787+
[{sent-transactions :sentTransactions
788+
send-details :sendDetails}]]
789+
{:fx [[:dispatch
790+
[:wallet/show-transaction-notification
791+
{:status :sent
792+
:send-details send-details
793+
:sent-transaction (first sent-transactions)}]]
794+
[:dispatch
795+
(if (:errorResponse send-details)
796+
[:wallet/transaction-failure send-details]
797+
[(if (= (:sendType send-details) constants/send-type-swap)
798+
:wallet.swap/transaction-success
799+
:wallet/transaction-success)
800+
sent-transactions])]]}))
801+
802+
(rf/reg-event-fx
803+
:wallet/sending-transactions-started-signal-received
804+
(fn [_
787805
[{sent-transactions :sentTransactions
788806
send-details :sendDetails}]]
789-
(let [swap? (get-in db [:wallet :ui :swap])]
790-
{:fx [[:dispatch
791-
(if-let [error-response (:errorResponse send-details)]
792-
[(if swap?
793-
:wallet.swap/transaction-failure
794-
:wallet/transaction-failure)
795-
error-response]
796-
[(if swap?
797-
:wallet.swap/transaction-success
798-
:wallet/transaction-success)
799-
sent-transactions])]]})))
807+
{:fx [[:dispatch
808+
[:wallet/show-transaction-notification
809+
{:status :sending
810+
:send-details send-details
811+
:sent-transaction (first sent-transactions)}]]]}))
812+
813+
(rf/reg-event-fx
814+
:wallet/status-changed-signal-received
815+
(fn [_
816+
[{sent-transactions :sentTransactions
817+
send-details :sendDetails}]]
818+
{:fx [[:dispatch
819+
[:wallet/show-transaction-notification
820+
{:status :status-changed
821+
:send-details send-details
822+
:sent-transaction (first sent-transactions)}]]]}))
823+
824+
(rf/reg-event-fx
825+
:wallet/show-transaction-notification
826+
(fn [{:keys [db]} [{:keys [status send-details sent-transaction]}]]
827+
(let [{:keys [error send-type]
828+
:as details}
829+
(as-> (utils/details-map send-details sent-transaction) $
830+
(assoc $ :error (get-in send-details [:ErrorResponse :details]))
831+
(assoc $ :account-from-name (utils/contact-name-by-address db (:address-from $)))
832+
(assoc $ :account-to-name (utils/contact-name-by-address db (:address-to $)))
833+
(assoc $
834+
:tx-to-name
835+
(or (utils/tx-to-name (:send-type $))
836+
(utils/contact-name-by-address db (:tx-to $)))))]
837+
(cond
838+
;; handle errors and show notifications about errors
839+
error
840+
(do
841+
(log/warn "Error when sending transaction" details)
842+
{:fx [[:dispatch
843+
[:toasts/upsert
844+
{:id (keyword (str status "-failure"))
845+
:type :negative
846+
:text error}]]]})
847+
848+
;; handle swap notifications in a separate fx
849+
(= send-type constants/send-type-swap)
850+
{:fx [[:dispatch
851+
[:wallet.swap/show-transaction-notification
852+
{:status status
853+
:send-details details}]]]}))))
854+

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,6 @@
546546
token-decimal (when token (:decimals token))
547547
token-id (utils/format-token-id token collectible)
548548
to-token-id ""
549-
gas-rates constants/gas-rate-medium
550549
to-hex (fn [v] (send-utils/amount-in-hex v (if token token-decimal 0)))
551550
amount-in (to-hex amount)
552551
amount-out (to-hex amount-out)
@@ -582,10 +581,11 @@
582581
:amountIn amount-in
583582
:amountOut amount-out
584583
:tokenID token-id
584+
:tokenIDIsOwnerToken false
585585
:toTokenID to-token-id
586586
:disabledFromChainIDs disabled-from-chain-ids
587587
:disabledToChainIDs disabled-to-chain-ids
588-
:gasFeeMode gas-rates
588+
:gasFeeMode constants/gas-rate-medium
589589
:fromLockedAmount {}
590590
:username (:username args)
591591
:publicKey (:publicKey args)
@@ -660,19 +660,20 @@
660660
(update :best best-routes-fix)
661661
(update :candidates candidates-fix)))
662662

663+
663664
(rf/reg-event-fx
664665
:wallet/handle-suggested-routes
665666
(fn [{:keys [db]} [data]]
666-
(let [{send :send swap? :swap} (-> db :wallet :ui)
667+
(let [{:keys [send swap]} (-> db :wallet :ui)
667668
skip-processing-routes? (:skip-processing-suggested-routes? send)
668669
clean-user-tx-settings? (get-in db
669670
[:wallet :ui :send :custom-tx-settings
670671
:delete-on-routes-update?])]
671-
(when (or swap? (not skip-processing-routes?))
672+
(when (or swap (not skip-processing-routes?))
672673
(let [{error-code :code
673674
:as error} (:ErrorResponse data)
674675
enough-assets? (not (and (:Best data) (= error-code "WR-002")))
675-
failure? (and error enough-assets? (not swap?))
676+
failure? (and error enough-assets? (not swap))
676677
error-message (if (zero? error-code) "An error occurred" (:details error))]
677678
(when failure?
678679
(log/error "failed to get suggested routes (async)"
@@ -683,11 +684,11 @@
683684
{:db (update-in db [:wallet :ui :send] dissoc :custom-tx-settings)})
684685
{:fx [[:dispatch
685686
(cond
686-
(and failure? swap?) [:wallet/swap-proposal-error error]
687-
failure? [:wallet/suggested-routes-error error-message]
688-
swap? [:wallet/swap-proposal-success (fix-routes data)]
689-
:else [:wallet/suggested-routes-success (fix-routes data)
690-
enough-assets?])]]}))))))
687+
(and failure? swap) [:wallet/swap-proposal-error error]
688+
failure? [:wallet/suggested-routes-error error-message]
689+
swap [:wallet/swap-proposal-success (fix-routes data)]
690+
:else [:wallet/suggested-routes-success (fix-routes data)
691+
enough-assets?])]]}))))))
691692

692693
(rf/reg-event-fx
693694
:wallet/transaction-success
@@ -711,16 +712,14 @@
711712

712713
(rf/reg-event-fx
713714
:wallet/transaction-failure
714-
(fn [_ [{:keys [details]}]]
715-
{:fx [[:dispatch [:wallet/end-transaction-flow]]
716-
[:dispatch-later
717-
[{:ms 2000
718-
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]
719-
[:dispatch
720-
[:toasts/upsert
721-
{:id :send-transaction-failure
722-
:type :negative
723-
:text (or details "An error occured")}]]]}))
715+
(fn [_ [send-details]]
716+
(if (= (:sendType send-details) constants/send-type-swap)
717+
{:fx [[:dispatch [:wallet.swap/track-transaction-execution-failed (:errorResponse send-details)]]
718+
[:dispatch [:wallet.swap/end-transaction-flow]]]}
719+
{:fx [[:dispatch [:wallet/end-transaction-flow]]
720+
[:dispatch-later
721+
[{:ms 2000
722+
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]]})))
724723

725724
(rf/reg-event-fx :wallet/clean-just-completed-transaction
726725
(fn [{:keys [db]}]

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

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,6 @@
432432
[:wallet.swap/set-sign-transactions-callback-fx
433433
[:dispatch [:wallet/prepare-signatures-for-transactions :swap]]]]]})))
434434

435-
(defn transaction-approval-required?
436-
[transactions {:keys [swap-proposal approval-transaction-id]}]
437-
(let [approval-transaction (when approval-transaction-id
438-
(get transactions approval-transaction-id))
439-
already-approved? (and approval-transaction
440-
(= (:status approval-transaction)
441-
:confirmed))]
442-
(and (:approval-required swap-proposal)
443-
(not already-approved?))))
444-
445435
(rf/reg-event-fx
446436
:wallet.swap/mark-as-pending
447437
(fn [{:keys [db]} [transaction-id]]
@@ -468,7 +458,7 @@
468458
(-> amount-out
469459
(number/hex->whole receive-token-decimals)
470460
(money/to-fixed receive-token-decimals)))
471-
approval-required? (transaction-approval-required? transactions swap)]
461+
approval-required? (utils/transaction-approval-required? transactions swap)]
472462
{:fx [[:dispatch
473463
[:centralized-metrics/track
474464
(if approval-required?
@@ -496,23 +486,11 @@
496486
[:dispatch [:wallet.swap/mark-as-pending (-> sent-transactions first :hash)]])
497487
(when-not approval-required?
498488
;; just end the whole transaction flow if no approval needed
499-
[:dispatch [:wallet.swap/end-transaction-flow]])
500-
(when-not approval-required?
501-
[:dispatch-later
502-
{:ms 500
503-
:dispatch [:toasts/upsert
504-
{:id :swap-transaction-pending
505-
:icon :i/info
506-
:type :neutral
507-
:text (i18n/label :t/swapping-to
508-
{:pay-amount amount
509-
:pay-token-symbol token-id-from
510-
:receive-token-symbol token-id-to
511-
:receive-amount receive-amount})}]}])]})))
489+
[:dispatch [:wallet.swap/end-transaction-flow]])]})))
512490

513491
(rf/reg-event-fx
514-
:wallet.swap/transaction-failure
515-
(fn [{:keys [db]} [{:keys [details] :as error}]]
492+
:wallet.swap/track-transaction-execution-failed
493+
(fn [{:keys [db]} [error]]
516494
(let [transactions (get-in db [:wallet :transactions])
517495
{:keys [asset-to-pay
518496
asset-to-receive
@@ -521,7 +499,7 @@
521499
swap-chain-id (:chain-id network)
522500
token-id-from (:symbol asset-to-pay)
523501
token-id-to (:symbol asset-to-receive)
524-
approval-required? (transaction-approval-required? transactions swap)]
502+
approval-required? (utils/transaction-approval-required? transactions swap)]
525503
{:fx [[:centralized-metrics/track
526504
(if approval-required?
527505
:metric/swap-approval-execution-failed
@@ -530,20 +508,14 @@
530508
:error error
531509
:pay_token token-id-from}
532510
(not approval-required?)
533-
(assoc :receive_token token-id-to))]
534-
[:dispatch [:wallet.swap/end-transaction-flow]]
535-
[:dispatch
536-
[:toasts/upsert
537-
{:id :send-transaction-error
538-
:type :negative
539-
:text (or details "An error occured")}]]]})))
511+
(assoc :receive_token token-id-to))]]})))
540512

541513
(rf/reg-event-fx
542514
:wallet.swap/clean-up-transaction-flow
543515
(fn [{:keys [db]}]
544516
(let [transactions (get-in db [:wallet :transactions])
545517
swap (get-in db [:wallet :ui :swap])
546-
approval-required? (transaction-approval-required? transactions swap)]
518+
approval-required? (utils/transaction-approval-required? transactions swap)]
547519
{:db (update-in db [:wallet :ui] dissoc :swap)
548520
:fx [[:dispatch
549521
[:dismiss-modal
@@ -577,3 +549,29 @@
577549
[:dispatch
578550
[:navigate-to-within-stack
579551
[:screen/wallet.swap-select-asset-to-pay :screen/wallet.swap-select-account]]]])})))
552+
553+
(rf/reg-event-fx
554+
:wallet.swap/show-transaction-notification
555+
(fn [{:keys [db]} [{:keys [status send-details]}]]
556+
(let [transactions (get-in db [:wallet :transactions])
557+
{:keys [asset-to-pay asset-to-receive] :as swap} (get-in db [:wallet :ui :swap])]
558+
;; show toast when approval is not required
559+
(when (and (= status :sent)
560+
(not (utils/transaction-approval-required? transactions swap)))
561+
{:fx [[:dispatch-later
562+
{:ms 500
563+
:dispatch [:toasts/upsert
564+
{:id :swap-transaction-pending
565+
:icon :i/info
566+
:type :neutral
567+
:text (i18n/label :t/swapping-to
568+
{:pay-amount (-> send-details
569+
:from-amount
570+
(money/token->unit
571+
(:decimals asset-to-pay)))
572+
:receive-amount (-> send-details
573+
:to-amount
574+
(money/token->unit
575+
(:decimals asset-to-receive)))
576+
:pay-token-symbol (:from-asset send-details)
577+
:receive-token-symbol (:to-asset send-details)})}]}]]}))))

0 commit comments

Comments
 (0)