From 87483b1ef494176c07d18933cb8534237dd8d55f Mon Sep 17 00:00:00 2001 From: Volodymyr Kozieiev Date: Mon, 18 Nov 2024 16:15:14 +0000 Subject: [PATCH] Swap: use correct fee on spending cap screen (#21532) --- .../wallet/bridge/bridge_to/view.cljs | 5 +- .../wallet/common/asset_list/view.cljs | 2 +- .../contexts/wallet/common/utils.cljs | 11 ++-- .../sheets/select_asset/asset_list/view.cljs | 2 +- .../wallet/swap/set_spending_cap/view.cljs | 19 ++++--- .../contexts/wallet/swap/setup_swap/view.cljs | 14 ++++- .../wallet/swap/swap_confirmation/view.cljs | 9 +++- .../subs/wallet/dapps/transactions.cljs | 6 +-- src/status_im/subs/wallet/swap.cljs | 54 +++++++++++-------- src/status_im/subs/wallet/swap_test.cljs | 7 ++- src/status_im/subs/wallet/wallet.cljs | 28 +++++----- 11 files changed, 88 insertions(+), 69 deletions(-) diff --git a/src/status_im/contexts/wallet/bridge/bridge_to/view.cljs b/src/status_im/contexts/wallet/bridge/bridge_to/view.cljs index 3e6df601c5..91b0cc6b88 100644 --- a/src/status_im/contexts/wallet/bridge/bridge_to/view.cljs +++ b/src/status_im/contexts/wallet/bridge/bridge_to/view.cljs @@ -25,9 +25,8 @@ {:currency currency :balance balance :token token}) - fiat-formatted (utils/get-standard-fiat-format crypto-value - currency-symbol - fiat-value) + fiat-formatted (utils/fiat-formatted-for-ui currency-symbol + fiat-value) token-available-on-network? (network-utils/token-available-on-network? supported-networks chain-id)] [quo/network-list diff --git a/src/status_im/contexts/wallet/common/asset_list/view.cljs b/src/status_im/contexts/wallet/common/asset_list/view.cljs index 69a779a0be..692f8f1ca8 100644 --- a/src/status_im/contexts/wallet/common/asset_list/view.cljs +++ b/src/status_im/contexts/wallet/common/asset_list/view.cljs @@ -18,7 +18,7 @@ :balance total-balance :token token}) crypto-formatted (utils/get-standard-crypto-format token total-balance) - fiat-formatted (utils/get-standard-fiat-format crypto-formatted currency-symbol fiat-value)] + fiat-formatted (utils/fiat-formatted-for-ui currency-symbol fiat-value)] [quo/token-network {:token token-symbol :label token-name diff --git a/src/status_im/contexts/wallet/common/utils.cljs b/src/status_im/contexts/wallet/common/utils.cljs index 8ead59a1fd..c6055d5445 100644 --- a/src/status_im/contexts/wallet/common/utils.cljs +++ b/src/status_im/contexts/wallet/common/utils.cljs @@ -251,9 +251,9 @@ (str $ address)) address)) -(defn get-standard-fiat-format - [crypto-value currency-symbol fiat-value] - (if (string/includes? crypto-value "<") +(defn fiat-formatted-for-ui + [currency-symbol fiat-value] + (if (money/less-than fiat-value 0.01) (str "<" currency-symbol "0.01") (prettify-balance currency-symbol fiat-value))) @@ -281,9 +281,8 @@ formatted-token-price (prettify-balance currency-symbol price) percentage-change (prettify-percentage-change change-pct-24hour) crypto-value (get-standard-crypto-format token balance) - fiat-value (get-standard-fiat-format crypto-value - currency-symbol - fiat-unformatted-value)] + fiat-value (fiat-formatted-for-ui currency-symbol + fiat-unformatted-value)] {:token (:symbol token) :token-name (:name token) :state :default diff --git a/src/status_im/contexts/wallet/sheets/select_asset/asset_list/view.cljs b/src/status_im/contexts/wallet/sheets/select_asset/asset_list/view.cljs index 938832e785..ec3b2e0308 100644 --- a/src/status_im/contexts/wallet/sheets/select_asset/asset_list/view.cljs +++ b/src/status_im/contexts/wallet/sheets/select_asset/asset_list/view.cljs @@ -20,7 +20,7 @@ :balance total-balance :token token}) crypto-formatted (utils/get-standard-crypto-format token total-balance) - fiat-formatted (utils/get-standard-fiat-format crypto-formatted currency-symbol fiat-value)] + fiat-formatted (utils/fiat-formatted-for-ui currency-symbol fiat-value)] [rn/view {:style {:padding-horizontal 8}} [quo/token-network {:token token-symbol diff --git a/src/status_im/contexts/wallet/swap/set_spending_cap/view.cljs b/src/status_im/contexts/wallet/swap/set_spending_cap/view.cljs index f03e943920..6932480ac1 100644 --- a/src/status_im/contexts/wallet/swap/set_spending_cap/view.cljs +++ b/src/status_im/contexts/wallet/swap/set_spending_cap/view.cljs @@ -7,7 +7,7 @@ [status-im.common.events-helper :as events-helper] [status-im.common.floating-button-page.view :as floating-button-page] [status-im.common.standard-authentication.core :as standard-auth] - [status-im.constants :as constants] + [status-im.contexts.wallet.common.utils :as utils] [status-im.contexts.wallet.common.utils.external-links :as external-links] [status-im.contexts.wallet.swap.set-spending-cap.style :as style] [utils.address :as address-utils] @@ -185,11 +185,14 @@ (defn- transaction-details [] - (let [network (rf/sub [:wallet/swap-network]) - max-fees (rf/sub [:wallet/wallet-swap-proposal-fee-fiat-formatted - constants/token-for-fees-symbol]) - loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?]) - estimated-time (rf/sub [:wallet/swap-proposal-estimated-time])] + (let [network (rf/sub [:wallet/swap-network]) + approval-fees (rf/sub [:wallet/approval-gas-fees]) + currency-symbol (rf/sub [:profile/currency-symbol]) + approval-fees-formatted (utils/fiat-formatted-for-ui + currency-symbol + approval-fees) + loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?]) + estimated-time (rf/sub [:wallet/swap-proposal-estimated-time])] [rn/view {:style style/details-container} [:<> [data-item @@ -198,7 +201,9 @@ :network-image (:source network)}] [data-item {:title (i18n/label :t/max-fees) - :subtitle (if (and estimated-time max-fees) max-fees (i18n/label :t/unknown)) + :subtitle (if (and estimated-time approval-fees-formatted) + approval-fees-formatted + (i18n/label :t/unknown)) :loading? loading-swap-proposal? :size :small}] [data-item diff --git a/src/status_im/contexts/wallet/swap/setup_swap/view.cljs b/src/status_im/contexts/wallet/swap/setup_swap/view.cljs index 220ebd8fc3..f9ffd1fd42 100644 --- a/src/status_im/contexts/wallet/swap/setup_swap/view.cljs +++ b/src/status_im/contexts/wallet/swap/setup_swap/view.cljs @@ -65,15 +65,25 @@ (defn- transaction-details [] (let [theme (quo.theme/use-theme) - max-fees (rf/sub [:wallet/wallet-swap-proposal-fee-fiat-formatted + swap-proposal-fee (rf/sub [:wallet/wallet-swap-proposal-fee-fiat constants/token-for-fees-symbol]) + approval-gas-fee (rf/sub [:wallet/approval-gas-fees]) max-slippage (rf/sub [:wallet/swap-max-slippage]) loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?]) + currency-symbol (rf/sub [:profile/currency-symbol]) + approval-required? (rf/sub [:wallet/swap-proposal-approval-required]) + approval-status (rf/sub [:wallet/swap-approval-transaction-status]) + max-fee (if (and approval-required? (not= approval-status :confirmed)) + (money/add approval-gas-fee swap-proposal-fee) + swap-proposal-fee) + max-fee-formatted (utils/fiat-formatted-for-ui + currency-symbol + max-fee) error-response (rf/sub [:wallet/swap-error-response])] [rn/view {:style style/details-container} [data-item (cond-> {:title (i18n/label :t/max-fees) - :subtitle max-fees + :subtitle max-fee-formatted :loading? loading-swap-proposal? :size :small} error-response (assoc :subtitle-color diff --git a/src/status_im/contexts/wallet/swap/swap_confirmation/view.cljs b/src/status_im/contexts/wallet/swap/swap_confirmation/view.cljs index 291855887d..72d2b91e85 100644 --- a/src/status_im/contexts/wallet/swap/swap_confirmation/view.cljs +++ b/src/status_im/contexts/wallet/swap/swap_confirmation/view.cljs @@ -7,6 +7,7 @@ [status-im.common.floating-button-page.view :as floating-button-page] [status-im.common.standard-authentication.core :as standard-auth] [status-im.constants :as constants] + [status-im.contexts.wallet.common.utils :as utils] [status-im.contexts.wallet.send.utils :as send-utils] [status-im.contexts.wallet.swap.swap-confirmation.style :as style] [utils.address :as address-utils] @@ -137,8 +138,12 @@ (defn- transaction-details [] - (let [max-fees (rf/sub [:wallet/wallet-swap-proposal-fee-fiat-formatted + (let [max-fees (rf/sub [:wallet/wallet-swap-proposal-fee-fiat constants/token-for-fees-symbol]) + currency-symbol (rf/sub [:profile/currency-symbol]) + max-fees-formatted (utils/fiat-formatted-for-ui + currency-symbol + max-fees) estimated-time (rf/sub [:wallet/swap-proposal-estimated-time]) loading-swap-proposal? (rf/sub [:wallet/swap-loading-swap-proposal?]) max-slippage (rf/sub [:wallet/swap-max-slippage])] @@ -152,7 +157,7 @@ :loading? loading-swap-proposal?}] [data-item {:title (i18n/label :t/max-fees) - :subtitle (if (and estimated-time max-fees) max-fees (i18n/label :t/unknown)) + :subtitle (if (and estimated-time max-fees-formatted) max-fees-formatted (i18n/label :t/unknown)) :loading? loading-swap-proposal?}] [data-item {:title (i18n/label :t/max-slippage) diff --git a/src/status_im/subs/wallet/dapps/transactions.cljs b/src/status_im/subs/wallet/dapps/transactions.cljs index 796718d1bd..e36d7f530c 100644 --- a/src/status_im/subs/wallet/dapps/transactions.cljs +++ b/src/status_im/subs/wallet/dapps/transactions.cljs @@ -58,10 +58,8 @@ max-fees-fiat (wallet-utils/calculate-token-fiat-value {:currency currency :balance max-fees-ether :token eth-token}) - max-fees-fiat-formatted (-> (wallet-utils/get-standard-crypto-format eth-token - max-fees-ether) - (wallet-utils/get-standard-fiat-format currency-symbol - max-fees-fiat)) + max-fees-fiat-formatted (wallet-utils/fiat-formatted-for-ui currency-symbol + max-fees-fiat) balance (-> eth-token (get-in [:balances-per-chain chain-id :raw-balance]) money/bignumber) diff --git a/src/status_im/subs/wallet/swap.cljs b/src/status_im/subs/wallet/swap.cljs index 95e1716a67..a57672d35c 100644 --- a/src/status_im/subs/wallet/swap.cljs +++ b/src/status_im/subs/wallet/swap.cljs @@ -90,9 +90,8 @@ :balance total-balance :token token}) crypto-formatted (utils/get-standard-crypto-format token total-balance) - fiat-formatted (utils/get-standard-fiat-format crypto-formatted - currency-symbol - fiat-value)] + fiat-formatted (utils/fiat-formatted-for-ui currency-symbol + fiat-value)] {:crypto (str crypto-formatted " " token-symbol) :fiat fiat-formatted}))) @@ -239,30 +238,22 @@ :-> :estimated-time) (rf/reg-sub - :wallet/wallet-swap-proposal-fee-fiat-formatted + :wallet/wallet-swap-proposal-fee-fiat :<- [:wallet/current-viewing-account] :<- [:wallet/swap-proposal] :<- [:profile/currency] - :<- [:profile/currency-symbol] - (fn [[account swap-proposal currency currency-symbol] [_ token-symbol-for-fees]] + (fn [[account swap-proposal currency] [_ token-symbol-for-fees]] (when token-symbol-for-fees - (let [tokens (:tokens account) - token-for-fees (first (filter #(= (string/lower-case (:symbol %)) - (string/lower-case token-symbol-for-fees)) - tokens)) - fee-in-native-token (send-utils/calculate-full-route-gas-fee [swap-proposal]) - fee-in-crypto-formatted (utils/get-standard-crypto-format - token-for-fees - fee-in-native-token) - fee-in-fiat (utils/calculate-token-fiat-value - {:currency currency - :balance fee-in-native-token - :token token-for-fees}) - fee-formatted (utils/get-standard-fiat-format - fee-in-crypto-formatted - currency-symbol - fee-in-fiat)] - fee-formatted)))) + (let [tokens (:tokens account) + token-for-fees (first (filter #(= (string/lower-case (:symbol %)) + (string/lower-case token-symbol-for-fees)) + tokens)) + fee-in-native-token (send-utils/calculate-full-route-gas-fee [swap-proposal]) + fee-in-fiat (utils/calculate-token-fiat-value + {:currency currency + :balance fee-in-native-token + :token token-for-fees})] + fee-in-fiat)))) (rf/reg-sub :wallet/swap-asset-to-pay-balance-for-chain-data @@ -302,3 +293,20 @@ :currency-symbol currency-symbol :balance (or amount 0) :token asset-to-pay-with-current-account-balance}))) + +(rf/reg-sub + :wallet/approval-gas-fees + :<- [:wallet/current-viewing-account] + :<- [:wallet/swap-proposal] + :<- [:profile/currency] + (fn [[account {:keys [approval-gas-fees]} currency]] + (let [tokens (:tokens account) + token-for-fees (first (filter #(= (string/lower-case (:symbol %)) + (string/lower-case constants/token-for-fees-symbol)) + tokens)) + fee-in-fiat (utils/calculate-token-fiat-value + {:currency currency + :balance approval-gas-fees + :token token-for-fees})] + fee-in-fiat))) + diff --git a/src/status_im/subs/wallet/swap_test.cljs b/src/status_im/subs/wallet/swap_test.cljs index 1d406dd701..779d8c56a1 100644 --- a/src/status_im/subs/wallet/swap_test.cljs +++ b/src/status_im/subs/wallet/swap_test.cljs @@ -257,7 +257,7 @@ swap-data) (is (match? "0x10000" (rf/sub [sub-name]))))) -(h/deftest-sub :wallet/wallet-swap-proposal-fee-fiat-formatted +(h/deftest-sub :wallet/wallet-swap-proposal-fee-fiat [sub-name] (testing "wallet send fee calculated and formatted in fiat" (swap! rf-db/app-db @@ -266,9 +266,8 @@ (assoc-in [:wallet :current-viewing-account-address] "0x1") (assoc-in [:wallet :ui :swap] swap-data) (assoc-in [:currencies] currencies) - (assoc-in [:profile/profile :currency] :usd) - (assoc-in [:profile/profile :currency-symbol] "$"))) + (assoc-in [:profile/profile :currency] :usd))) (let [token-symbol-for-fees "ETH" result (rf/sub [sub-name token-symbol-for-fees])] - (is (match? result "$1.00"))))) + (is (match? result 1))))) diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 9c906f7fca..a1a708b3d5 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -768,22 +768,18 @@ :<- [:profile/currency-symbol] (fn [[account route currency currency-symbol] [_ token-symbol-for-fees]] (when token-symbol-for-fees - (let [tokens (:tokens account) - token-for-fees (first (filter #(= (string/lower-case (:symbol %)) - (string/lower-case token-symbol-for-fees)) - tokens)) - fee-in-native-token (send-utils/calculate-full-route-gas-fee route) - fee-in-crypto-formatted (utils/get-standard-crypto-format - token-for-fees - fee-in-native-token) - fee-in-fiat (utils/calculate-token-fiat-value - {:currency currency - :balance fee-in-native-token - :token token-for-fees}) - fee-formatted (utils/get-standard-fiat-format - fee-in-crypto-formatted - currency-symbol - fee-in-fiat)] + (let [tokens (:tokens account) + token-for-fees (first (filter #(= (string/lower-case (:symbol %)) + (string/lower-case token-symbol-for-fees)) + tokens)) + fee-in-native-token (send-utils/calculate-full-route-gas-fee route) + fee-in-fiat (utils/calculate-token-fiat-value + {:currency currency + :balance fee-in-native-token + :token token-for-fees}) + fee-formatted (utils/fiat-formatted-for-ui + currency-symbol + fee-in-fiat)] fee-formatted)))) (rf/reg-sub