Skip to content

Commit ff65daf

Browse files
committed
Swap and bridge improvements
1 parent f6e38e7 commit ff65daf

File tree

5 files changed

+292
-114
lines changed

5 files changed

+292
-114
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
@@ -488,6 +488,90 @@
488488
(some positive-balance-in-any-chain? tokens))
489489
operable-account))))
490490

491+
(defn send-details-map
492+
"Generates a new map with all the sen details using the original
493+
details we've received from `status-go`. The new one contains only
494+
the keys we need."
495+
[send-details]
496+
(let [{:keys [uuid
497+
sendType
498+
fromAddress
499+
toAddress
500+
fromChain
501+
toChain
502+
fromAmount
503+
toAmount
504+
fromAsset
505+
toAsset
506+
username
507+
publicKey
508+
packId]} send-details]
509+
{:uuid uuid
510+
:send-type sendType
511+
:address-from fromAddress
512+
:address-to toAddress
513+
:tx-to ""
514+
:from-chain fromChain
515+
:to-chain toChain
516+
:from-amount fromAmount
517+
:to-amount toAmount
518+
:from-asset fromAsset
519+
:to-asset toAsset
520+
:username username
521+
:public-key publicKey
522+
:pack-id packId
523+
:tx-hash ""
524+
:approval-tx? false}))
525+
526+
(defn details-map
527+
"Generates map with all the transaction details based on what we've
528+
got from status-go as `send-details` and `sent-transaction`."
529+
[send-details sent-transaction]
530+
(let [send-details (send-details-map send-details)
531+
{:keys [toAddress fromChain toChain amountIn
532+
amountOut fromToken toToken hash
533+
approvalTx]} sent-transaction
534+
amount-in (money/from-hex amountIn)
535+
amount-out (money/from-hex amountOut)
536+
sent-transaction? (and sent-transaction (> (-> sent-transaction :hash count) 0))]
537+
(if sent-transaction?
538+
(cond-> send-details
539+
true (assoc :tx-to toAddress)
540+
(> fromChain 0) (assoc :from-chain fromChain)
541+
(> toChain 0) (assoc :to-chain toChain)
542+
(not= amount-in "0") (assoc :from-amount amount-in)
543+
(not= amount-out "0") (assoc :to-amount amount-out)
544+
(> (count fromToken) 0) (assoc :from-asset fromToken)
545+
(> (count toToken) 0) (assoc :to-asset toToken)
546+
true (assoc :tx-hash hash)
547+
true (assoc :approval-tx? approvalTx))
548+
send-details)))
549+
550+
(defn contact-name-by-address
551+
[db address]
552+
(or (get-in db [:wallet :accounts address :name])
553+
(get-in db [:contacts/contacts address :primary-name])))
554+
555+
(defn tx-to-name
556+
"Returns the transaction name that will be used for certain types of transactions."
557+
[send-type]
558+
(cond
559+
(= send-type constants/send-type-bridge) "Hop"
560+
(= send-type constants/send-type-swap) "ParaSwap"
561+
:else nil))
562+
563+
(defn transaction-approval-required?
564+
"Indicates whether the transaction needs approval based on the information
565+
from the database."
566+
[transactions {:keys [swap-proposal approval-transaction-id]}]
567+
(let [approval-transaction (when approval-transaction-id
568+
(get transactions approval-transaction-id))
569+
already-approved? (and approval-transaction
570+
(= (:status approval-transaction)
571+
:confirmed))]
572+
(and (:approval-required swap-proposal)
573+
(not already-approved?))))
574+
491575
(defn on-paste-address-or-ens
492576
"Check if the clipboard has any valid address and extract the address without any chain info.
493577
If it does not contain an valid address or it is ENS, return the clipboard text as it is"

src/status_im/contexts/wallet/events.cljs

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -756,22 +756,75 @@
756756

757757
(rf/reg-event-fx
758758
:wallet/transactions-sent-signal-received
759-
(fn [{:keys [db]}
759+
(fn [_
760+
[{sent-transactions :sentTransactions
761+
send-details :sendDetails}]]
762+
{:fx [[:dispatch
763+
[:wallet/show-transaction-notification
764+
{:status :sent
765+
:send-details send-details
766+
:sent-transaction (first sent-transactions)}]]
767+
[:dispatch
768+
(if (:errorResponse send-details)
769+
[:wallet/transaction-failure send-details]
770+
[(if (= (:sendType send-details) constants/send-type-swap)
771+
:wallet.swap/transaction-success
772+
:wallet/transaction-success)
773+
sent-transactions])]]}))
774+
775+
(rf/reg-event-fx
776+
:wallet/sending-transactions-started-signal-received
777+
(fn [_
760778
[{sent-transactions :sentTransactions
761779
send-details :sendDetails}]]
762-
(let [swap? (-> db
763-
(get-in db-path/swap)
764-
seq)]
765-
{:fx [[:dispatch
766-
(if-let [error-response (:errorResponse send-details)]
767-
[(if swap?
768-
:wallet.swap/transaction-failure
769-
:wallet/transaction-failure)
770-
error-response]
771-
[(if swap?
772-
:wallet.swap/transaction-success
773-
:wallet/transaction-success)
774-
sent-transactions])]]})))
780+
{:fx [[:dispatch
781+
[:wallet/show-transaction-notification
782+
{:status :sending
783+
:send-details send-details
784+
:sent-transaction (first sent-transactions)}]]]}))
785+
786+
(rf/reg-event-fx
787+
:wallet/status-changed-signal-received
788+
(fn [_
789+
[{sent-transactions :sentTransactions
790+
send-details :sendDetails}]]
791+
{:fx [[:dispatch
792+
[:wallet/show-transaction-notification
793+
{:status :status-changed
794+
:send-details send-details
795+
:sent-transaction (first sent-transactions)}]]]}))
796+
797+
(rf/reg-event-fx
798+
:wallet/show-transaction-notification
799+
(fn [{:keys [db]} [{:keys [status send-details sent-transaction]}]]
800+
(let [{:keys [error send-type]
801+
:as details}
802+
(as-> (utils/details-map send-details sent-transaction) $
803+
(assoc $ :error (get-in send-details [:ErrorResponse :details]))
804+
(assoc $ :account-from-name (utils/contact-name-by-address db (:address-from $)))
805+
(assoc $ :account-to-name (utils/contact-name-by-address db (:address-to $)))
806+
(assoc $
807+
:tx-to-name
808+
(or (utils/tx-to-name (:send-type $))
809+
(utils/contact-name-by-address db (:tx-to $)))))]
810+
(cond
811+
;; handle errors and show notifications about errors
812+
error
813+
(do
814+
(log/warn "Error when sending transaction" details)
815+
{:fx [[:dispatch
816+
[:toasts/upsert
817+
{:id (keyword (str status "-failure"))
818+
:type :negative
819+
:text error}]]]})
820+
821+
;; handle swap notifications in a separate fx
822+
(= send-type constants/send-type-swap)
823+
{:fx [[:dispatch
824+
[:wallet.swap/show-transaction-notification
825+
{:status status
826+
:send-details details}]]]}))))
827+
775828

776829
(rf/reg-event-fx
777830
:wallet/retrieve-new-chain-indicator

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

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@
560560
token-decimal (when token (:decimals token))
561561
token-id (utils/format-token-id token collectible)
562562
to-token-id ""
563-
gas-rates constants/gas-rate-medium
564563
to-hex (fn [v] (send-utils/amount-in-hex v (if token token-decimal 0)))
565564
amount-in (to-hex amount)
566565
amount-out (to-hex amount-out)
@@ -596,10 +595,11 @@
596595
:amountIn amount-in
597596
:amountOut amount-out
598597
:tokenID token-id
598+
:tokenIDIsOwnerToken false
599599
:toTokenID to-token-id
600600
:disabledFromChainIDs disabled-from-chain-ids
601601
:disabledToChainIDs disabled-to-chain-ids
602-
:gasFeeMode gas-rates
602+
:gasFeeMode constants/gas-rate-medium
603603
:fromLockedAmount {}
604604
:username (:username args)
605605
:publicKey (:publicKey args)
@@ -637,19 +637,57 @@
637637
{:event :wallet/stop-get-suggested-routes
638638
:error error}))}]]]}))
639639

640+
(defn- bridge-amount-greater-than-bonder-fees?
641+
[{{token-decimals :decimals} :from-token
642+
bonder-fees :tx-bonder-fees
643+
amount-in :amount-in}]
644+
(let [bonder-fees (utils.money/token->unit bonder-fees token-decimals)
645+
amount-to-bridge (utils.money/token->unit amount-in token-decimals)]
646+
(> amount-to-bridge bonder-fees)))
647+
648+
(defn- remove-multichain-routes
649+
[routes]
650+
(if (> (count routes) 1)
651+
[] ;; if route is multichain, we remove it
652+
routes))
653+
654+
(defn- remove-invalid-bonder-fees-routes
655+
[routes]
656+
(filter bridge-amount-greater-than-bonder-fees? routes))
657+
658+
(defn- ->old-route-paths
659+
[routes]
660+
(map data-store/new->old-route-path routes))
661+
662+
(def ^:private best-routes-fix
663+
(comp ->old-route-paths
664+
remove-invalid-bonder-fees-routes
665+
remove-multichain-routes))
666+
667+
(def ^:private candidates-fix
668+
(comp ->old-route-paths remove-invalid-bonder-fees-routes))
669+
670+
(defn- fix-routes
671+
[data]
672+
(-> data
673+
(data-store/rpc->suggested-routes)
674+
(update :best best-routes-fix)
675+
(update :candidates candidates-fix)))
676+
677+
640678
(rf/reg-event-fx
641679
:wallet/handle-suggested-routes
642680
(fn [{:keys [db]} [data]]
643-
(let [{send :send swap? :swap} (-> db :wallet :ui)
644-
skip-processing-routes? (:skip-processing-suggested-routes? send)
645-
clean-user-tx-settings? (get-in
646-
db
647-
(conj db-path/send :user-tx-settings :delete-on-routes-update?))]
648-
(when (or swap? (not skip-processing-routes?))
681+
(let [{:keys [send swap]} (-> db :wallet :ui)
682+
skip-processing-routes? (:skip-processing-suggested-routes? send)
683+
clean-user-tx-settings? (get-in db
684+
(conj db-path/send :user-tx-settings
685+
:delete-on-routes-update?))]
686+
(when (or swap (not skip-processing-routes?))
649687
(let [{error-code :code
650688
:as error} (:ErrorResponse data)
651689
enough-assets? (not (and (:Best data) (= error-code "WR-002")))
652-
failure? (and error enough-assets? (not swap?))
690+
failure? (and error enough-assets? (not swap))
653691
error-message (if (zero? error-code) "An error occurred" (:details error))]
654692
(when failure?
655693
(log/error "failed to get suggested routes (async)"
@@ -660,11 +698,11 @@
660698
{:db (update-in db db-path/send dissoc :user-tx-settings)})
661699
{:fx [[:dispatch
662700
(cond
663-
(and failure? swap?) [:wallet/swap-proposal-error error]
664-
failure? [:wallet/suggested-routes-error error-message]
665-
swap? [:wallet/swap-proposal-success (data-store/fix-routes data)]
666-
:else [:wallet/suggested-routes-success (data-store/fix-routes data)
667-
enough-assets?])]]}))))))
701+
(and failure? swap) [:wallet/swap-proposal-error error]
702+
failure? [:wallet/suggested-routes-error error-message]
703+
swap [:wallet/swap-proposal-success (data-store/fix-routes data)]
704+
:else [:wallet/suggested-routes-success (data-store/fix-routes data)
705+
enough-assets?])]]}))))))
668706

669707
(rf/reg-event-fx
670708
:wallet/transaction-success
@@ -690,16 +728,14 @@
690728

691729
(rf/reg-event-fx
692730
:wallet/transaction-failure
693-
(fn [_ [{:keys [details]}]]
694-
{:fx [[:dispatch [:wallet/end-transaction-flow]]
695-
[:dispatch-later
696-
[{:ms 2000
697-
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]
698-
[:dispatch
699-
[:toasts/upsert
700-
{:id :send-transaction-failure
701-
:type :negative
702-
:text (or details "An error occured")}]]]}))
731+
(fn [_ [send-details]]
732+
(if (= (:sendType send-details) constants/send-type-swap)
733+
{:fx [[:dispatch [:wallet.swap/track-transaction-execution-failed (:errorResponse send-details)]]
734+
[:dispatch [:wallet.swap/end-transaction-flow]]]}
735+
{:fx [[:dispatch [:wallet/end-transaction-flow]]
736+
[:dispatch-later
737+
[{:ms 2000
738+
:dispatch [:wallet/stop-and-clean-suggested-routes]}]]]})))
703739

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

0 commit comments

Comments
 (0)