Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add entry point for other networks #5824

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 = initialSelectedInputAsset ? Number(initialSelectedInputAsset?.balance.amount) : 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was NaN when no initial inputAsset was set.

walmat marked this conversation as resolved.
Show resolved Hide resolved
const initialNiceIncrement = findNiceIncrement(initialBalance);
const initialDecimalPlaces = countDecimalPlaces(initialNiceIncrement);

Expand Down
4 changes: 3 additions & 1 deletion src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
const selectedOutputChainId = useSharedValue<ChainId>(initialSelectedInputAsset?.chainId || ChainId.mainnet);
const quote = useSharedValue<Quote | CrosschainQuote | QuoteError | null>(null);

const inputProgress = useSharedValue(NavigationSteps.INPUT_ELEMENT_FOCUSED);
const inputProgress = useSharedValue(
initialSelectedOutputAsset && !initialSelectedInputAsset ? NavigationSteps.TOKEN_LIST_FOCUSED : NavigationSteps.INPUT_ELEMENT_FOCUSED
);
Comment on lines +130 to +132
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for cases where we have an output asset but no initial input asset

const outputProgress = useSharedValue(
initialSelectedOutputAsset ? NavigationSteps.INPUT_ELEMENT_FOCUSED : NavigationSteps.TOKEN_LIST_FOCUSED
);
Expand Down
3 changes: 3 additions & 0 deletions src/__swaps__/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export const countDecimalPlaces = (number: number | string): number => {

export const findNiceIncrement = (availableBalance: string | number) => {
'worklet';
if (Number(availableBalance) === 0) {
return 0;
}
Comment on lines +152 to +154
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might not be needed with the NaN check I added, but does this hurt anything to have here?


// We'll use one of these factors to adjust the base increment
// These factors are chosen to:
Expand Down
69 changes: 67 additions & 2 deletions src/components/expanded-state/AvailableNetworksv2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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],
Expand Down Expand Up @@ -62,10 +72,65 @@ 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);

console.log(chainId, uniqueId);
walmat marked this conversation as resolved.
Show resolved Hide resolved

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 });
}
Comment on lines +117 to +119
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@christianbaroni I can remove this, but I noticed for cases where we don't have a largest balance on the same chain we still default to the largest overall balance. Is that intended? My thinking is it'd make more sense for users to explicitly select an asset so that we aren't making an initial quote fetch that they (probably) don't want.

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,
Expand All @@ -81,7 +146,7 @@ const AvailableNetworksv2 = ({
screen: Routes.CURRENCY_SELECT_SCREEN,
});
},
[asset, goBack, navigate, networks, updateInputCurrency]
[asset, goBack, navigate, networks, swapsV2Enabled, swaps_v2, updateInputCurrency]
);

const handlePressContextMenu = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion src/components/expanded-state/asset/ChartExpandedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -52,6 +54,7 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc
},
searchAsset: {
...asset,
uniqueId,
chainId,
chainName: chainNameFromChainId(chainId),
address: asset.address as AddressOrEth,
Expand All @@ -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 });
Comment on lines +80 to +81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was happening here too, so i added it here as well

}
swapsStore.setState({ outputAsset: parsedAsset });
}
Expand Down
Loading