From 47a4718435b6928ed5d267a36bfdb316c2344e20 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Wed, 11 Dec 2024 22:10:42 +0100 Subject: [PATCH 1/4] fix: fix swaps button on asset overview page for multichain feature --- app/components/UI/Swaps/utils/index.js | 6 +++++- app/components/Views/Asset/index.js | 9 ++++++--- app/reducers/swaps/index.js | 5 +++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/components/UI/Swaps/utils/index.js b/app/components/UI/Swaps/utils/index.js index 665562c3f55..9ce1831d881 100644 --- a/app/components/UI/Swaps/utils/index.js +++ b/app/components/UI/Swaps/utils/index.js @@ -4,6 +4,7 @@ import { swapsUtils } from '@metamask/swaps-controller'; import { strings } from '../../../../../locales/i18n'; import AppConstants from '../../../../core/AppConstants'; import { NETWORKS_CHAIN_ID } from '../../../../constants/network'; +import { isPortfolioViewEnabled } from '../../../../util/networks'; const { ETH_CHAIN_ID, @@ -40,13 +41,16 @@ if (__DEV__) { allowedChainIds.push(...allowedTestnetChainIds); } -export function isSwapsAllowed(chainId) { +export function isSwapsAllowed(chainId, tokenChainId = undefined) { if (!AppConstants.SWAPS.ACTIVE) { return false; } if (!AppConstants.SWAPS.ONLY_MAINNET) { allowedChainIds.push(SWAPS_TESTNET_CHAIN_ID); } + if (isPortfolioViewEnabled() && tokenChainId) { + return allowedChainIds.includes(tokenChainId); + } return allowedChainIds.includes(chainId); } diff --git a/app/components/Views/Asset/index.js b/app/components/Views/Asset/index.js index bc6e24b35cd..112c2856834 100644 --- a/app/components/Views/Asset/index.js +++ b/app/components/Views/Asset/index.js @@ -18,6 +18,7 @@ import { } from '../../../constants/transaction'; import AppConstants from '../../../core/AppConstants'; import { + swapsLivenessMultichainSelector, swapsLivenessSelector, swapsTokensMultiChainObjectSelector, swapsTokensObjectSelector, @@ -480,7 +481,7 @@ class Asset extends PureComponent { const styles = createStyles(colors); const asset = navigation && params; const isSwapsFeatureLive = this.props.swapsIsLive; - const isNetworkAllowed = isSwapsAllowed(chainId); + const isNetworkAllowed = isSwapsAllowed(chainId, asset.chainId); const isAssetAllowed = asset.isETH || asset.address?.toLowerCase() in this.props.swapsTokens; @@ -533,8 +534,10 @@ class Asset extends PureComponent { Asset.contextType = ThemeContext; -const mapStateToProps = (state) => ({ - swapsIsLive: swapsLivenessSelector(state), +const mapStateToProps = (state, { route }) => ({ + swapsIsLive: isPortfolioViewEnabled() + ? swapsLivenessMultichainSelector(state, route.params.chainId) + : swapsLivenessSelector(state), swapsTokens: isPortfolioViewEnabled() ? swapsTokensMultiChainObjectSelector(state) : swapsTokensObjectSelector(state), diff --git a/app/reducers/swaps/index.js b/app/reducers/swaps/index.js index 31aa743daa6..af7fbe11cae 100644 --- a/app/reducers/swaps/index.js +++ b/app/reducers/swaps/index.js @@ -69,6 +69,11 @@ export const swapsLivenessSelector = createSelector( (swapsState, chainId) => swapsState[chainId]?.isLive || false, ); +export const swapsLivenessMultichainSelector = createSelector( + [swapsStateSelector, (_state, chainId) => chainId], + (swapsState, chainId) => swapsState[chainId]?.isLive || false, +); + /** * Returns if smart transactions are enabled in feature flags */ From 90fe809642b8fb9f9e14436e7544229afc1f7d78 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Wed, 11 Dec 2024 22:35:33 +0100 Subject: [PATCH 2/4] fix: refactor --- app/components/UI/Swaps/utils/index.js | 5 +---- app/components/Views/Asset/index.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/components/UI/Swaps/utils/index.js b/app/components/UI/Swaps/utils/index.js index 9ce1831d881..981069bbd59 100644 --- a/app/components/UI/Swaps/utils/index.js +++ b/app/components/UI/Swaps/utils/index.js @@ -41,16 +41,13 @@ if (__DEV__) { allowedChainIds.push(...allowedTestnetChainIds); } -export function isSwapsAllowed(chainId, tokenChainId = undefined) { +export function isSwapsAllowed(chainId) { if (!AppConstants.SWAPS.ACTIVE) { return false; } if (!AppConstants.SWAPS.ONLY_MAINNET) { allowedChainIds.push(SWAPS_TESTNET_CHAIN_ID); } - if (isPortfolioViewEnabled() && tokenChainId) { - return allowedChainIds.includes(tokenChainId); - } return allowedChainIds.includes(chainId); } diff --git a/app/components/Views/Asset/index.js b/app/components/Views/Asset/index.js index 112c2856834..7a7ead2dea0 100644 --- a/app/components/Views/Asset/index.js +++ b/app/components/Views/Asset/index.js @@ -481,7 +481,7 @@ class Asset extends PureComponent { const styles = createStyles(colors); const asset = navigation && params; const isSwapsFeatureLive = this.props.swapsIsLive; - const isNetworkAllowed = isSwapsAllowed(chainId, asset.chainId); + const isNetworkAllowed = isSwapsAllowed(asset.chainId); const isAssetAllowed = asset.isETH || asset.address?.toLowerCase() in this.props.swapsTokens; From ffadc04620c16d5a54d78cce47acb0e3b7c7c783 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Wed, 11 Dec 2024 22:36:45 +0100 Subject: [PATCH 3/4] fix: lint --- app/components/UI/Swaps/utils/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/components/UI/Swaps/utils/index.js b/app/components/UI/Swaps/utils/index.js index 981069bbd59..665562c3f55 100644 --- a/app/components/UI/Swaps/utils/index.js +++ b/app/components/UI/Swaps/utils/index.js @@ -4,7 +4,6 @@ import { swapsUtils } from '@metamask/swaps-controller'; import { strings } from '../../../../../locales/i18n'; import AppConstants from '../../../../core/AppConstants'; import { NETWORKS_CHAIN_ID } from '../../../../constants/network'; -import { isPortfolioViewEnabled } from '../../../../util/networks'; const { ETH_CHAIN_ID, From 3233475d99bd09849c6c7884d2f194f52af87cf1 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Wed, 11 Dec 2024 22:57:21 +0100 Subject: [PATCH 4/4] fix: fix --- app/components/Views/Asset/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/components/Views/Asset/index.js b/app/components/Views/Asset/index.js index 7a7ead2dea0..35abfa07df7 100644 --- a/app/components/Views/Asset/index.js +++ b/app/components/Views/Asset/index.js @@ -481,7 +481,9 @@ class Asset extends PureComponent { const styles = createStyles(colors); const asset = navigation && params; const isSwapsFeatureLive = this.props.swapsIsLive; - const isNetworkAllowed = isSwapsAllowed(asset.chainId); + const isNetworkAllowed = isPortfolioViewEnabled() + ? isSwapsAllowed(asset.chainId) + : isSwapsAllowed(chainId); const isAssetAllowed = asset.isETH || asset.address?.toLowerCase() in this.props.swapsTokens;