diff --git a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts index 3bd64647059..cfb9daead9d 100644 --- a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts +++ b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts @@ -32,7 +32,7 @@ import { queryClient } from '@/react-query'; import { divWorklet, equalWorklet, greaterThanWorklet, mulWorklet, toFixedWorklet } from '@/__swaps__/safe-math/SafeMath'; function getInitialInputValues(initialSelectedInputAsset: ExtendedAnimatedAssetWithColors | null) { - const initialBalance = Number(initialSelectedInputAsset?.balance.amount) ?? 0; + const initialBalance = Number(initialSelectedInputAsset?.balance.amount) || 0; const initialNiceIncrement = findNiceIncrement(initialBalance); const initialDecimalPlaces = countDecimalPlaces(initialNiceIncrement); diff --git a/src/__swaps__/screens/Swap/providers/swap-provider.tsx b/src/__swaps__/screens/Swap/providers/swap-provider.tsx index b63615582da..f67193d98d6 100644 --- a/src/__swaps__/screens/Swap/providers/swap-provider.tsx +++ b/src/__swaps__/screens/Swap/providers/swap-provider.tsx @@ -127,7 +127,9 @@ export const SwapProvider = ({ children }: SwapProviderProps) => { const selectedOutputChainId = useSharedValue(initialSelectedInputAsset?.chainId || ChainId.mainnet); const quote = useSharedValue(null); - const inputProgress = useSharedValue(NavigationSteps.INPUT_ELEMENT_FOCUSED); + const inputProgress = useSharedValue( + initialSelectedOutputAsset && !initialSelectedInputAsset ? NavigationSteps.TOKEN_LIST_FOCUSED : NavigationSteps.INPUT_ELEMENT_FOCUSED + ); const outputProgress = useSharedValue( initialSelectedOutputAsset ? NavigationSteps.INPUT_ELEMENT_FOCUSED : NavigationSteps.TOKEN_LIST_FOCUSED ); diff --git a/src/__swaps__/utils/swaps.ts b/src/__swaps__/utils/swaps.ts index dc57412ed99..501c15fd7a1 100644 --- a/src/__swaps__/utils/swaps.ts +++ b/src/__swaps__/utils/swaps.ts @@ -149,6 +149,9 @@ export const countDecimalPlaces = (number: number | string): number => { export const findNiceIncrement = (availableBalance: string | number) => { 'worklet'; + if (Number(availableBalance) === 0) { + return 0; + } // We'll use one of these factors to adjust the base increment // These factors are chosen to: diff --git a/src/components/expanded-state/AvailableNetworksv2.tsx b/src/components/expanded-state/AvailableNetworksv2.tsx index d15af381327..bc1967a0d96 100644 --- a/src/components/expanded-state/AvailableNetworksv2.tsx +++ b/src/components/expanded-state/AvailableNetworksv2.tsx @@ -17,6 +17,14 @@ import ContextMenuButton from '@/components/native-context-menu/contextMenu'; import { implementation } from '@/entities/dispersion'; import { RainbowNetworks, getNetworkObj } from '@/networks'; import { EthCoinIcon } from '../coin-icon/EthCoinIcon'; +import { SWAPS_V2, useExperimentalFlag } from '@/config'; +import { useRemoteConfig } from '@/model/remoteConfig'; +import { userAssetsStore } from '@/state/assets/userAssets'; +import { parseSearchAsset } from '@/__swaps__/utils/assets'; +import { AddressOrEth, AssetType } from '@/__swaps__/types/assets'; +import { chainNameFromChainId } from '@/__swaps__/utils/chains'; +import { swapsStore } from '@/state/swaps/swapsStore'; +import { InteractionManager } from 'react-native'; const NOOP = () => null; @@ -33,6 +41,8 @@ const AvailableNetworksv2 = ({ }) => { const { colors } = useTheme(); const { goBack, navigate } = useNavigation(); + const { swaps_v2 } = useRemoteConfig(); + const swapsV2Enabled = useExperimentalFlag(SWAPS_V2); const radialGradientProps = { center: [0, 1], @@ -62,10 +72,63 @@ const AvailableNetworksv2 = ({ // we need to convert the mainnet asset to the selected network's newAsset.mainnet_address = networks?.[ethereumUtils.getChainIdFromNetwork(Network.mainnet)]?.address ?? asset.address; newAsset.address = networks?.[ethereumUtils.getChainIdFromNetwork(chosenNetwork)].address; + newAsset.network = chosenNetwork; + + goBack(); + + if (swapsV2Enabled || swaps_v2) { + const chainId = ethereumUtils.getChainIdFromNetwork(newAsset.network); + const uniqueId = `${newAsset.address}_${chainId}`; + const userAsset = userAssetsStore.getState().userAssets.get(uniqueId); + + const parsedAsset = parseSearchAsset({ + assetWithPrice: { + ...newAsset, + uniqueId, + address: newAsset.address as AddressOrEth, + type: newAsset.type as AssetType, + chainId, + chainName: chainNameFromChainId(chainId), + isNativeAsset: false, + native: {}, + }, + searchAsset: { + ...newAsset, + uniqueId, + chainId, + chainName: chainNameFromChainId(chainId), + address: newAsset.address as AddressOrEth, + highLiquidity: newAsset.highLiquidity ?? false, + isRainbowCurated: newAsset.isRainbowCurated ?? false, + isVerified: newAsset.isVerified ?? false, + mainnetAddress: (newAsset.mainnet_address ?? '') as AddressOrEth, + networks: newAsset.networks ?? [], + type: newAsset.type as AssetType, + }, + userAsset, + }); + + const largestBalanceSameChainUserAsset = userAssetsStore + .getState() + .getUserAssets() + .find(userAsset => userAsset.chainId === chainId && userAsset.address !== newAsset.address); + if (largestBalanceSameChainUserAsset) { + swapsStore.setState({ inputAsset: largestBalanceSameChainUserAsset }); + } else { + swapsStore.setState({ inputAsset: null }); + } + swapsStore.setState({ outputAsset: parsedAsset }); + + InteractionManager.runAfterInteractions(() => { + navigate(Routes.SWAP); + }); + + return; + } + newAsset.uniqueId = `${asset.address}_${chosenNetwork}`; newAsset.type = chosenNetwork; - goBack(); navigate(Routes.EXCHANGE_MODAL, { params: { fromDiscover: true, @@ -81,7 +144,7 @@ const AvailableNetworksv2 = ({ screen: Routes.CURRENCY_SELECT_SCREEN, }); }, - [asset, goBack, navigate, networks, updateInputCurrency] + [asset, goBack, navigate, networks, swapsV2Enabled, swaps_v2, updateInputCurrency] ); const handlePressContextMenu = useCallback( diff --git a/src/components/expanded-state/asset/ChartExpandedState.js b/src/components/expanded-state/asset/ChartExpandedState.js index d947faf4254..e4db1c1c7b2 100644 --- a/src/components/expanded-state/asset/ChartExpandedState.js +++ b/src/components/expanded-state/asset/ChartExpandedState.js @@ -29,7 +29,7 @@ import { useNavigation } from '@/navigation'; import { ETH_ADDRESS } from '@/references'; import Routes from '@/navigation/routesNames'; import styled from '@/styled-thing'; -import { ethereumUtils, safeAreaInsetValues } from '@/utils'; +import { safeAreaInsetValues } from '@/utils'; import AvailableNetworksv2 from '@/components/expanded-state/AvailableNetworksv2'; import AvailableNetworksv1 from '@/components/expanded-state/AvailableNetworks'; import { Box } from '@/design-system'; diff --git a/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx b/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx index 8daef82baa5..8e5123b3394 100644 --- a/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx +++ b/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx @@ -38,11 +38,13 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc const goToSwap = useCallback(() => { if (swapsV2Enabled || swaps_v2) { const chainId = ethereumUtils.getChainIdFromNetwork(asset.network); - const userAsset = userAssetsStore.getState().userAssets.get(`${asset.address}_${chainId}`); + const uniqueId = `${asset.address}_${chainId}`; + const userAsset = userAssetsStore.getState().userAssets.get(uniqueId); const parsedAsset = parseSearchAsset({ assetWithPrice: { ...asset, + uniqueId, address: asset.address as AddressOrEth, type: asset.type as AssetType, chainId, @@ -52,6 +54,7 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc }, searchAsset: { ...asset, + uniqueId, chainId, chainName: chainNameFromChainId(chainId), address: asset.address as AddressOrEth, @@ -74,6 +77,8 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc .find(userAsset => userAsset.chainId === chainId && userAsset.address !== asset.address); if (largestBalanceSameChainUserAsset) { swapsStore.setState({ inputAsset: largestBalanceSameChainUserAsset }); + } else { + swapsStore.setState({ inputAsset: null }); } swapsStore.setState({ outputAsset: parsedAsset }); }