From 463e8a5eecf1458c513afe90996327d98dc78e26 Mon Sep 17 00:00:00 2001 From: Brian Sztamfater Date: Wed, 20 Mar 2024 10:07:02 -0300 Subject: [PATCH] feat: make bridging transactions work after sliding to confirm the transaction (#18986) --- .../components/wallet/summary_info/view.cljs | 8 +- src/status_im/constants.cljs | 1 + .../wallet/account/bridge_to/view.cljs | 2 +- .../contexts/wallet/account/view.cljs | 4 +- src/status_im/contexts/wallet/events.cljs | 2 +- .../contexts/wallet/send/events.cljs | 207 +++++++++++------- .../wallet/send/input_amount/view.cljs | 3 +- .../contexts/wallet/send/routes/view.cljs | 7 +- .../send/transaction_confirmation/view.cljs | 13 +- src/status_im/feature_flags.cljs | 1 - status-go-version.json | 6 +- 11 files changed, 157 insertions(+), 97 deletions(-) diff --git a/src/quo/components/wallet/summary_info/view.cljs b/src/quo/components/wallet/summary_info/view.cljs index 00d8d17984..23593e2877 100644 --- a/src/quo/components/wallet/summary_info/view.cljs +++ b/src/quo/components/wallet/summary_info/view.cljs @@ -30,8 +30,12 @@ (defn networks [values theme] (let [{:keys [ethereum optimism arbitrum]} values - show-optimism? (and optimism (pos? (:amount optimism))) - show-arbitrum? (and arbitrum (pos? (:amount arbitrum)))] + show-optimism? (and optimism + (or (pos? (:amount optimism)) + (= (:amount optimism) "<0.01"))) + show-arbitrum? (and arbitrum + (or (pos? (:amount arbitrum)) + (= (:amount arbitrum) "<0.01")))] [rn/view {:style style/networks-container :accessibility-label :networks} diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index 65a84acd4a..74777a7caa 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -478,6 +478,7 @@ (def ^:const bridge-name-transfer "Transfer") (def ^:const bridge-name-erc-721-transfer "ERC721Transfer") +(def ^:const bridge-name-hop "Hop") (def ^:const alert-banner-height 40) diff --git a/src/status_im/contexts/wallet/account/bridge_to/view.cljs b/src/status_im/contexts/wallet/account/bridge_to/view.cljs index d1e2fe263d..49eb610d09 100644 --- a/src/status_im/contexts/wallet/account/bridge_to/view.cljs +++ b/src/status_im/contexts/wallet/account/bridge_to/view.cljs @@ -31,7 +31,7 @@ :fiat-value fiat-formatted :on-press #(rf/dispatch [:wallet/select-bridge-network {:network-chain-id chain-id - :stack-id :wallet-bridge}])}]))) + :stack-id :screen/wallet.bridge-to}])}]))) (defn- view-internal [] diff --git a/src/status_im/contexts/wallet/account/view.cljs b/src/status_im/contexts/wallet/account/view.cljs index 8a53828c0d..74e10a22e1 100644 --- a/src/status_im/contexts/wallet/account/view.cljs +++ b/src/status_im/contexts/wallet/account/view.cljs @@ -7,7 +7,6 @@ [status-im.contexts.wallet.account.tabs.view :as tabs] [status-im.contexts.wallet.common.account-switcher.view :as account-switcher] [status-im.contexts.wallet.common.temp :as temp] - [status-im.feature-flags :as ff] [utils.i18n :as i18n] [utils.re-frame :as rf])) @@ -55,8 +54,7 @@ :receive-action #(rf/dispatch [:open-modal :screen/wallet.share-address {:status :receive}]) :buy-action #(rf/dispatch [:show-bottom-sheet {:content buy-drawer}]) - :bridge-action #(ff/alert ::ff/wallet.bridge-token - (fn [] (rf/dispatch [:wallet/start-bridge])))}]) + :bridge-action #(rf/dispatch [:wallet/start-bridge])}]) [quo/tabs {:style style/tabs :size 32 diff --git a/src/status_im/contexts/wallet/events.cljs b/src/status_im/contexts/wallet/events.cljs index 96a16fb874..9f26dd86e6 100644 --- a/src/status_im/contexts/wallet/events.cljs +++ b/src/status_im/contexts/wallet/events.cljs @@ -244,7 +244,7 @@ (rf/reg-event-fx :wallet/start-bridge (fn [{:keys [db]}] - {:db (assoc-in db [:wallet :ui :send :type] :bridge) + {:db (assoc-in db [:wallet :ui :send :tx-type] :bridge) :fx [[:dispatch [:open-modal :screen/wallet.bridge]]]})) (rf/reg-event-fx :wallet/select-bridge-network diff --git a/src/status_im/contexts/wallet/send/events.cljs b/src/status_im/contexts/wallet/send/events.cljs index 0a303807d5..b0f239d323 100644 --- a/src/status_im/contexts/wallet/send/events.cljs +++ b/src/status_im/contexts/wallet/send/events.cljs @@ -90,24 +90,24 @@ (rf/reg-event-fx :wallet/clean-selected-token (fn [{:keys [db]}] - {:db (assoc-in db [:wallet :ui :send :token] nil)})) + {:db (update-in db [:wallet :ui :send] dissoc :token :tx-type)})) (rf/reg-event-fx :wallet/clean-selected-collectible (fn [{:keys [db]}] - (let [type (get-in db [:wallet :ui :send :type])] + (let [transaction-type (get-in db [:wallet :ui :send :tx-type])] {:db (update-in db [:wallet :ui :send] dissoc :collectible :amount - (when (= type :collecible) :type))}))) + (when (= transaction-type :collecible) :tx-type))}))) (rf/reg-event-fx :wallet/send-select-collectible (fn [{:keys [db]} [{:keys [collectible stack-id]}]] {:db (-> db (update-in [:wallet :ui :send] dissoc :token) (assoc-in [:wallet :ui :send :collectible] collectible) - (assoc-in [:wallet :ui :send :type] :collectible) + (assoc-in [:wallet :ui :send :tx-type] :collectible) (assoc-in [:wallet :ui :send :amount] 1)) :fx [[:dispatch [:wallet/get-suggested-routes {:amount 1}]] [:navigate-to-within-stack [:screen/wallet.transaction-confirmation stack-id]]]})) @@ -121,7 +121,7 @@ (fn [{:keys [db now]} [{:keys [amount]}]] (let [wallet-address (get-in db [:wallet :current-viewing-account-address]) token (get-in db [:wallet :ui :send :token]) - transaction-type (get-in db [:wallet :ui :send :type]) + transaction-type (get-in db [:wallet :ui :send :tx-type]) collectible (get-in db [:wallet :ui :send :collectible]) to-address (get-in db [:wallet :ui :send :to-address]) test-networks-enabled? (get-in db [:profile/profile :test-networks-enabled?]) @@ -191,47 +191,81 @@ (fn [_] {:fx [[:dispatch [:dismiss-modal :screen/wallet.transaction-progress]]]})) -(defn- transaction-bridge - [{:keys [from-address from-chain-id to-address token-id token-address route data eth-transfer?]}] - (let [{:keys [bridge-name amount-out gas-amount - gas-fees]} route - eip-1559-enabled? (:eip-1559-enabled gas-fees) +(defn- transaction-data + [{:keys [from-address to-address token-address route data eth-transfer?]}] + (let [{:keys [amount-in gas-amount gas-fees]} route + eip-1559-enabled? (:eip-1559-enabled gas-fees) {:keys [gas-price max-fee-per-gas-medium - max-priority-fee-per-gas]} gas-fees - transfer-tx (cond-> {:From from-address - :To (or token-address to-address) - :Gas (money/to-hex gas-amount) - :Value (when eth-transfer? amount-out) - :Nonce nil - :Input "" - :Data (or data "0x")} - eip-1559-enabled? (assoc :TxType "0x02" - :MaxFeePerGas - (money/to-hex - (money/->wei - :gwei - max-fee-per-gas-medium)) - :MaxPriorityFeePerGas - (money/to-hex - (money/->wei - :gwei - max-priority-fee-per-gas))) - (not eip-1559-enabled?) (assoc :TxType "0x00" - :GasPrice (money/to-hex - (money/->wei - :gwei - gas-price))))] - [(cond-> {:BridgeName bridge-name - :ChainID from-chain-id} + max-priority-fee-per-gas]} gas-fees] + (cond-> {:From from-address + :To (or token-address to-address) + :Gas (money/to-hex gas-amount) + :Value (when eth-transfer? amount-in) + :Nonce nil + :Input "" + :Data (or data "0x")} + eip-1559-enabled? (assoc + :TxType "0x02" + :MaxFeePerGas + (money/to-hex + (money/->wei + :gwei + max-fee-per-gas-medium)) + :MaxPriorityFeePerGas + (money/to-hex + (money/->wei + :gwei + max-priority-fee-per-gas))) + (not eip-1559-enabled?) (assoc :TxType "0x00" + :GasPrice + (money/to-hex + (money/->wei + :gwei + gas-price)))))) - (= bridge-name constants/bridge-name-erc-721-transfer) - (assoc :ERC721TransferTx - (assoc transfer-tx - :Recipient to-address - :TokenID token-id)) +(defn- transaction-path + [{:keys [from-address to-address token-id token-address route data eth-transfer?]}] + (let [{:keys [bridge-name amount-in bonder-fees from + to]} route + tx-data (transaction-data {:from-address from-address + :to-address to-address + :token-address token-address + :route route + :data data + :eth-transfer? eth-transfer?}) + to-chain-id (:chain-id to) + from-chain-id (:chain-id from)] + (cond-> {:BridgeName bridge-name + :ChainID from-chain-id} - (= bridge-name constants/bridge-name-transfer) - (assoc :TransferTx transfer-tx))])) + (= bridge-name constants/bridge-name-erc-721-transfer) + (assoc :ERC721TransferTx + (assoc tx-data + :Recipient to-address + :TokenID token-id + :ChainID to-chain-id)) + + (= bridge-name constants/bridge-name-transfer) + (assoc :TransferTx tx-data) + + (= bridge-name constants/bridge-name-hop) + (assoc :HopTx + (assoc tx-data + :ChainID to-chain-id + :Symbol token-id + :Recipient to-address + :Amount amount-in + :BonderFee bonder-fees)) + + (not (or (= bridge-name constants/bridge-name-erc-721-transfer) + (= bridge-name constants/bridge-name-transfer) + (= bridge-name constants/bridge-name-hop))) + (assoc :CbridgeTx + (assoc tx-data + :ChainID to-chain-id + :Symbol token-id + :Recipient to-address + :Amount amount-in))))) (defn- multi-transaction-command [{:keys [from-address to-address from-asset to-asset amount-out transfer-type] @@ -245,41 +279,54 @@ (rf/reg-event-fx :wallet/send-transaction (fn [{:keys [db]} [sha3-pwd]] - (let [route (first (get-in db [:wallet :ui :send :route])) - from-address (get-in db [:wallet :current-viewing-account-address]) - token (get-in db [:wallet :ui :send :token]) - collectible (get-in db [:wallet :ui :send :collectible]) - from-chain-id (get-in route [:from :chain-id]) - token-id (if token - (:symbol token) - (get-in collectible [:id :token-id])) + (let [routes (get-in db [:wallet :ui :send :route]) + first-route (first routes) + from-address (get-in db [:wallet :current-viewing-account-address]) + transaction-type (get-in db [:wallet :ui :send :tx-type]) + transaction-type-param (case transaction-type + :collectible constants/send-type-erc-721-transfer + :bridge constants/send-type-bridge + constants/send-type-transfer) + token (get-in db [:wallet :ui :send :token]) + collectible (get-in db [:wallet :ui :send :collectible]) + first-route-from-chain-id (get-in first-route [:from :chain-id]) + token-id (if token + (:symbol token) + (get-in collectible [:id :token-id])) erc20-transfer? (and token (not= token-id "ETH")) - eth-transfer? (and token (not erc20-transfer?)) - token-address (cond collectible - (get-in collectible - [:id :contract-id :address]) - erc20-transfer? - (get-in token [:balances-per-chain from-chain-id :address])) - to-address (get-in db [:wallet :ui :send :to-address]) - data (when erc20-transfer? - (native-module/encode-transfer (address/normalized-hex to-address) - (:amount-out route))) - request-params [(multi-transaction-command - {:from-address from-address - :to-address to-address - :from-asset token-id - :to-asset token-id - :amount-out (if eth-transfer? (:amount-out route) "0x0")}) - (transaction-bridge {:to-address to-address - :from-address from-address - :route route - :from-chain-id from-chain-id - :token-address token-address - :token-id (when collectible - (money/to-hex (js/parseInt token-id))) - :data data - :eth-transfer? eth-transfer?}) - sha3-pwd]] + eth-transfer? (and token (not erc20-transfer?)) + token-address (cond collectible + (get-in collectible + [:id :contract-id :address]) + erc20-transfer? + (get-in token [:balances-per-chain first-route-from-chain-id :address])) + to-address (get-in db [:wallet :ui :send :to-address]) + transaction-paths (mapv (fn [route] + (let [data (when erc20-transfer? + (native-module/encode-transfer + (address/normalized-hex to-address) + (:amount-in route)))] + (transaction-path {:to-address to-address + :from-address from-address + :route route + :token-address token-address + :token-id (if collectible + (money/to-hex (js/parseInt + token-id)) + token-id) + :data data + :eth-transfer? eth-transfer?}))) + routes) + request-params + [(multi-transaction-command + {:from-address from-address + :to-address to-address + :from-asset token-id + :to-asset token-id + :amount-out (if eth-transfer? (:amount-out first-route) "0x0") + :transfer-type transaction-type-param}) + transaction-paths + sha3-pwd]] {:json-rpc/call [{:method "wallet_createMultiTransaction" :params request-params :on-success (fn [result] @@ -293,4 +340,8 @@ (log/error "failed to send transaction" {:event :wallet/send-transaction :error error - :params request-params}))}]}))) + :params request-params}) + (rf/dispatch [:toasts/upsert + {:id :send-transaction-error + :type :negative + :text (:message error)}]))}]}))) diff --git a/src/status_im/contexts/wallet/send/input_amount/view.cljs b/src/status_im/contexts/wallet/send/input_amount/view.cljs index 0a4cfdb659..c6195c2147 100644 --- a/src/status_im/contexts/wallet/send/input_amount/view.cljs +++ b/src/status_im/contexts/wallet/send/input_amount/view.cljs @@ -206,6 +206,7 @@ conversion-rate (-> token :market-values-per-currency :usd :price) loading-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?]) suggested-routes (rf/sub [:wallet/wallet-send-suggested-routes]) + best-routes (when suggested-routes (or (:best suggested-routes) [])) route (rf/sub [:wallet/wallet-send-route]) to-address (rf/sub [:wallet/wallet-send-to-address]) on-confirm (or default-on-confirm handle-on-confirm) @@ -280,7 +281,7 @@ :limit-crypto crypto-limit})}] [routes/view {:amount amount-text - :routes suggested-routes + :routes best-routes :token token :input-value @input-value :fetch-routes #(fetch-routes % (current-limit))}] diff --git a/src/status_im/contexts/wallet/send/routes/view.cljs b/src/status_im/contexts/wallet/send/routes/view.cljs index 0772b83775..cf6564250c 100644 --- a/src/status_im/contexts/wallet/send/routes/view.cljs +++ b/src/status_im/contexts/wallet/send/routes/view.cljs @@ -122,9 +122,8 @@ (let [selected-networks (rf/sub [:wallet/wallet-send-selected-networks]) loading-networks (find-affordable-networks token input-value selected-networks) loading-suggested-routes? (rf/sub [:wallet/wallet-send-loading-suggested-routes?]) - best-routes (:best routes) - data (if loading-suggested-routes? loading-networks best-routes)] - (if (or (and (not-empty loading-networks) loading-suggested-routes?) (not-empty best-routes)) + data (if loading-suggested-routes? loading-networks routes)] + (if (or (and (not-empty loading-networks) loading-suggested-routes?) (not-empty routes)) [rn/flat-list {:data (if (and (< (count data) 3) (pos? (count data))) (concat data [{:status :add}]) @@ -154,7 +153,7 @@ (utils/id->network (get-in item [:to :chain-id])))}])}] [rn/view {:style style/empty-container} - (when (and (not (nil? best-routes)) (not loading-suggested-routes?)) + (when (and (not (nil? routes)) (not loading-suggested-routes?)) [quo/text (i18n/label :t/no-routes-found)])]))) (def view (quo.theme/with-theme view-internal)) diff --git a/src/status_im/contexts/wallet/send/transaction_confirmation/view.cljs b/src/status_im/contexts/wallet/send/transaction_confirmation/view.cljs index 63c73a8278..91c505675d 100644 --- a/src/status_im/contexts/wallet/send/transaction_confirmation/view.cljs +++ b/src/status_im/contexts/wallet/send/transaction_confirmation/view.cljs @@ -153,7 +153,14 @@ (reduce-kv (fn [acc k v] (assoc acc k {:amount v :token-symbol token-symbol})) {} - network-amounts)] + network-amounts) + network-values-sanitized (into {} + (map (fn [[k v]] + [k + (if (money/equal-to (v :amount) 0) + (assoc v :amount "<0.01") + v)]) + network-values))] [rn/view {:style {:padding-horizontal 20 :padding-bottom 16}} @@ -166,7 +173,7 @@ [quo/summary-info {:type summary-type :networks? true - :values network-values + :values network-values-sanitized :account-props account-props}]]))) (defn- transaction-details @@ -258,7 +265,7 @@ image-url (when collectible (:image-url collectible-data)) amount (:amount send-transaction-data) route (:route send-transaction-data) - transaction-type (:type send-transaction-data) + transaction-type (:tx-type send-transaction-data) estimated-time-min (reduce + (map :estimated-time route)) max-fees "-" to-address (:to-address send-transaction-data) diff --git a/src/status_im/feature_flags.cljs b/src/status_im/feature_flags.cljs index fd2e3b3420..f5f296d34b 100644 --- a/src/status_im/feature_flags.cljs +++ b/src/status_im/feature_flags.cljs @@ -11,7 +11,6 @@ (defonce ^:private feature-flags-config (reagent/atom {::wallet.edit-default-keypair (enabled-in-env? :FLAG_EDIT_DEFAULT_KEYPAIR) - ::wallet.bridge-token (enabled-in-env? :FLAG_BRIDGE_TOKEN_ENABLED) ::wallet.edit-derivation-path (enabled-in-env? :FLAG_EDIT_DERIVATION_PATH) ::wallet.remove-account (enabled-in-env? :FLAG_REMOVE_ACCOUNT_ENABLED) ::wallet.network-filter (enabled-in-env? :FLAG_NETWORK_FILTER_ENABLED) diff --git a/status-go-version.json b/status-go-version.json index 386af1be0f..dc29361e86 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -3,7 +3,7 @@ "_comment": "Instead use: scripts/update-status-go.sh ", "owner": "status-im", "repo": "status-go", - "version": "v0.176.9", - "commit-sha1": "a3ad05db58b8af20bf6b7ab852f8603212abac4e", - "src-sha256": "10c05kgihczbams3i8fkfm1mh5m61bnsjh255c22m4awc073rg12" + "version": "v0.176.10", + "commit-sha1": "f69ee07593e8e35bb029f62de670b5554d71c932", + "src-sha256": "1icm7l1lhqdz5zbfvagxb051rw3hdm8zvwcxwaizyd1lhbx4v8h5" }