Skip to content

Commit b517215

Browse files
committed
Swap and bridge improvements
1 parent 8c82a84 commit b517215

File tree

5 files changed

+210
-68
lines changed

5 files changed

+210
-68
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
@@ -781,17 +781,72 @@
781781

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

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@
462462
token-decimal (when token (:decimals token))
463463
token-id (utils/format-token-id token collectible)
464464
to-token-id ""
465-
gas-rates constants/gas-rate-medium
466465
to-hex (fn [v] (send-utils/amount-in-hex v (if token token-decimal 0)))
467466
amount-in (to-hex amount)
468467
amount-out (to-hex amount-out)
@@ -498,10 +497,11 @@
498497
:amountIn amount-in
499498
:amountOut amount-out
500499
:tokenID token-id
500+
:tokenIDIsOwnerToken false
501501
:toTokenID to-token-id
502502
:disabledFromChainIDs disabled-from-chain-ids
503503
:disabledToChainIDs disabled-to-chain-ids
504-
:gasFeeMode gas-rates
504+
:gasFeeMode constants/gas-rate-medium
505505
:fromLockedAmount {}
506506
:username (:username args)
507507
:publicKey (:publicKey args)
@@ -576,28 +576,28 @@
576576
(update :best best-routes-fix)
577577
(update :candidates candidates-fix)))
578578

579+
579580
(rf/reg-event-fx
580581
:wallet/handle-suggested-routes
581582
(fn [{:keys [db]} [data]]
582-
(let [{send :send swap? :swap} (-> db :wallet :ui)
583-
skip-processing-routes? (:skip-processing-suggested-routes? send)]
584-
(when (or swap? (not skip-processing-routes?))
583+
(let [{:keys [send swap]} (-> db :wallet :ui)]
584+
(when (or swap (not (:skip-processing-suggested-routes? send)))
585585
(let [{error-code :code
586586
:as error} (:ErrorResponse data)
587587
enough-assets? (not (and (:Best data) (= error-code "WR-002")))
588-
failure? (and error enough-assets? (not swap?))
588+
failure? (and error enough-assets? (not swap))
589589
error-message (if (zero? error-code) "An error occurred" (:details error))]
590590
(when failure?
591591
(log/error "failed to get suggested routes (async)"
592592
{:event :wallet/handle-suggested-routes
593593
:error error-message}))
594594
{:fx [[:dispatch
595595
(cond
596-
(and failure? swap?) [:wallet/swap-proposal-error error]
597-
failure? [:wallet/suggested-routes-error error-message]
598-
swap? [:wallet/swap-proposal-success (fix-routes data)]
599-
:else [:wallet/suggested-routes-success (fix-routes data)
600-
enough-assets?])]]})))))
596+
(and failure? swap) [:wallet/swap-proposal-error error]
597+
failure? [:wallet/suggested-routes-error error-message]
598+
swap [:wallet/swap-proposal-success (fix-routes data)]
599+
:else [:wallet/suggested-routes-success (fix-routes data)
600+
enough-assets?])]]})))))
601601

602602
(rf/reg-event-fx
603603
:wallet/transaction-success
@@ -621,16 +621,14 @@
621621

622622
(rf/reg-event-fx
623623
:wallet/transaction-failure
624-
(fn [_ [{:keys [details]}]]
625-
{:fx [[:dispatch [:wallet/end-transaction-flow]]
626-
[:dispatch-later
627-
[{:ms 2000
628-
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]
629-
[:dispatch
630-
[:toasts/upsert
631-
{:id :send-transaction-failure
632-
:type :negative
633-
:text (or details "An error occured")}]]]}))
624+
(fn [_ [send-details]]
625+
(if (= (:sendType send-details) constants/send-type-swap)
626+
{:fx [[:dispatch [:wallet.swap/track-transaction-execution-failed (:errorResponse send-details)]]
627+
[:dispatch [:wallet.swap/end-transaction-flow]]]}
628+
{:fx [[:dispatch [:wallet/end-transaction-flow]]
629+
[:dispatch-later
630+
[{:ms 2000
631+
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]]})))
634632

635633
(rf/reg-event-fx :wallet/clean-just-completed-transaction
636634
(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
@@ -438,16 +438,6 @@
438438
[:screen/wallet.swap-confirmation
439439
:screen/wallet.setup-swap]]]]})))
440440

441-
(defn transaction-approval-required?
442-
[transactions {:keys [swap-proposal approval-transaction-id]}]
443-
(let [approval-transaction (when approval-transaction-id
444-
(get transactions approval-transaction-id))
445-
already-approved? (and approval-transaction
446-
(= (:status approval-transaction)
447-
:confirmed))]
448-
(and (:approval-required swap-proposal)
449-
(not already-approved?))))
450-
451441
(rf/reg-event-fx
452442
:wallet.swap/mark-as-pending
453443
(fn [{:keys [db]} [transaction-id]]
@@ -474,7 +464,7 @@
474464
(-> amount-out
475465
(number/hex->whole receive-token-decimals)
476466
(money/to-fixed receive-token-decimals)))
477-
approval-required? (transaction-approval-required? transactions swap)]
467+
approval-required? (utils/transaction-approval-required? transactions swap)]
478468
{:fx [[:dispatch
479469
[:centralized-metrics/track
480470
(if approval-required?
@@ -502,23 +492,11 @@
502492
[:dispatch [:wallet.swap/mark-as-pending (-> sent-transactions first :hash)]])
503493
(when-not approval-required?
504494
;; just end the whole transaction flow if no approval needed
505-
[:dispatch [:wallet.swap/end-transaction-flow]])
506-
(when-not approval-required?
507-
[:dispatch-later
508-
{:ms 500
509-
:dispatch [:toasts/upsert
510-
{:id :swap-transaction-pending
511-
:icon :i/info
512-
:type :neutral
513-
:text (i18n/label :t/swapping-to
514-
{:pay-amount amount
515-
:pay-token-symbol token-id-from
516-
:receive-token-symbol token-id-to
517-
:receive-amount receive-amount})}]}])]})))
495+
[:dispatch [:wallet.swap/end-transaction-flow]])]})))
518496

519497
(rf/reg-event-fx
520-
:wallet.swap/transaction-failure
521-
(fn [{:keys [db]} [{:keys [details] :as error}]]
498+
:wallet.swap/track-transaction-execution-failed
499+
(fn [{:keys [db]} [error]]
522500
(let [transactions (get-in db [:wallet :transactions])
523501
{:keys [asset-to-pay
524502
asset-to-receive
@@ -527,7 +505,7 @@
527505
swap-chain-id (:chain-id network)
528506
token-id-from (:symbol asset-to-pay)
529507
token-id-to (:symbol asset-to-receive)
530-
approval-required? (transaction-approval-required? transactions swap)]
508+
approval-required? (utils/transaction-approval-required? transactions swap)]
531509
{:fx [[:centralized-metrics/track
532510
(if approval-required?
533511
:metric/swap-approval-execution-failed
@@ -536,20 +514,14 @@
536514
:error error
537515
:pay_token token-id-from}
538516
(not approval-required?)
539-
(assoc :receive_token token-id-to))]
540-
[:dispatch [:wallet.swap/end-transaction-flow]]
541-
[:dispatch
542-
[:toasts/upsert
543-
{:id :send-transaction-error
544-
:type :negative
545-
:text (or details "An error occured")}]]]})))
517+
(assoc :receive_token token-id-to))]]})))
546518

547519
(rf/reg-event-fx
548520
:wallet.swap/clean-up-transaction-flow
549521
(fn [{:keys [db]}]
550522
(let [transactions (get-in db [:wallet :transactions])
551523
swap (get-in db [:wallet :ui :swap])
552-
approval-required? (transaction-approval-required? transactions swap)]
524+
approval-required? (utils/transaction-approval-required? transactions swap)]
553525
{:db (update-in db [:wallet :ui] dissoc :swap)
554526
:fx [[:dispatch
555527
[:dismiss-modal
@@ -583,3 +555,29 @@
583555
[:dispatch
584556
[:navigate-to-within-stack
585557
[:screen/wallet.swap-select-asset-to-pay :screen/wallet.swap-select-account]]]])})))
558+
559+
(rf/reg-event-fx
560+
:wallet.swap/show-transaction-notification
561+
(fn [{:keys [db]} [{:keys [status send-details]}]]
562+
(let [transactions (get-in db [:wallet :transactions])
563+
{:keys [asset-to-pay asset-to-receive] :as swap} (get-in db [:wallet :ui :swap])]
564+
;; show toast when approval is not required
565+
(when (and (= status :sent)
566+
(not (utils/transaction-approval-required? transactions swap)))
567+
{:fx [[:dispatch-later
568+
{:ms 500
569+
:dispatch [:toasts/upsert
570+
{:id :swap-transaction-pending
571+
:icon :i/info
572+
:type :neutral
573+
:text (i18n/label :t/swapping-to
574+
{:pay-amount (-> send-details
575+
:from-amount
576+
(money/token->unit
577+
(:decimals asset-to-pay)))
578+
:receive-amount (-> send-details
579+
:to-amount
580+
(money/token->unit
581+
(:decimals asset-to-receive)))
582+
:pay-token-symbol (:from-asset send-details)
583+
:receive-token-symbol (:to-asset send-details)})}]}]]}))))

0 commit comments

Comments
 (0)