Skip to content

Commit

Permalink
remove multiple getUniqueIds and only keep worklet version
Browse files Browse the repository at this point in the history
  • Loading branch information
walmat committed Oct 16, 2024
1 parent f349082 commit a57eb89
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SearchAsset } from '@/__swaps__/types/search';
import { SwapAssetType } from '@/__swaps__/types/swap';
import { parseSearchAsset } from '@/__swaps__/utils/assets';
import { getChainColorWorklet } from '@/__swaps__/utils/swaps';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { getUniqueId } from '@/utils/ethereumUtils';
import { analyticsV2 } from '@/analytics';
import { AnimatedTextIcon } from '@/components/AnimatedComponents/AnimatedTextIcon';
import { TIMING_CONFIGS } from '@/components/animations/animationConfigs';
Expand Down Expand Up @@ -100,10 +100,7 @@ export const TokenToBuyList = () => {
const handleSelectToken = useCallback(
(token: SearchAsset) => {
runOnUI(() => {
if (
internalSelectedInputAsset.value &&
getUniqueIdWorklet(token.address, token.chainId) !== internalSelectedOutputAsset.value?.uniqueId
) {
if (internalSelectedInputAsset.value && getUniqueId(token.address, token.chainId) !== internalSelectedOutputAsset.value?.uniqueId) {
isQuoteStale.value = 1;
isFetching.value = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ListEmpty } from '@/__swaps__/screens/Swap/components/TokenList/ListEmp
import { useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider';
import { ParsedSearchAsset } from '@/__swaps__/types/assets';
import { SwapAssetType } from '@/__swaps__/types/swap';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { getUniqueId } from '@/utils/ethereumUtils';
import { analyticsV2 } from '@/analytics';
import { useDelayedMount } from '@/hooks/useDelayedMount';
import * as i18n from '@/languages';
Expand Down Expand Up @@ -45,10 +45,7 @@ const TokenToSellListComponent = () => {
if (!token) return;

runOnUI(() => {
if (
internalSelectedOutputAsset.value &&
getUniqueIdWorklet(token.address, token.chainId) !== internalSelectedInputAsset.value?.uniqueId
) {
if (internalSelectedOutputAsset.value && getUniqueId(token.address, token.chainId) !== internalSelectedInputAsset.value?.uniqueId) {
isQuoteStale.value = 1;
isFetching.value = true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/screens/Swap/hooks/useSearchCurrencyLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChainId } from '@/chains/types';
import { SearchAsset, TokenSearchAssetKey, TokenSearchThreshold } from '@/__swaps__/types/search';
import { addHexPrefix } from '@/handlers/web3';
import { isLowerCaseMatch, filterList } from '@/utils';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { getUniqueId } from '@/utils/ethereumUtils';
import { useFavorites } from '@/resources/favorites';
import { useSwapsStore } from '@/state/swaps/swapsStore';
import { isAddress } from '@ethersproject/address';
Expand Down Expand Up @@ -346,7 +346,7 @@ export function useSearchCurrencyLists() {
chainId: state.toChainId,
favorite: true,
mainnetAddress: favToken.networks?.[ChainId.mainnet]?.address || favToken.mainnet_address,
uniqueId: getUniqueIdWorklet(favToken.networks[state.toChainId]?.address || favToken.address, state.toChainId),
uniqueId: getUniqueId(favToken.networks[state.toChainId]?.address || favToken.address, state.toChainId),
})) as SearchAsset[];
}, [favorites, state.toChainId]);

Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { inputKeys } from '../types/swap';
import { valueBasedDecimalFormatter } from './decimalFormatter';
import { convertAmountToRawAmount } from '@/helpers/utilities';
import { chainsName } from '@/chains';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { getUniqueId } from '@/utils/ethereumUtils';

// /---- 🎨 Color functions 🎨 ----/ //
//
Expand Down Expand Up @@ -548,7 +548,7 @@ export const parseAssetAndExtend = ({
colors: (isAssetEth ? ETH_COLORS : asset.colors) as TokenColors,
});

const uniqueId = getUniqueIdWorklet(asset.address, asset.chainId);
const uniqueId = getUniqueId(asset.address, asset.chainId);
const balance = insertUserAssetBalance ? userAssetsStore.getState().getUserAsset(uniqueId)?.balance || asset.balance : asset.balance;

return {
Expand Down
6 changes: 3 additions & 3 deletions src/migrations/migrations/migrateFavorites.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AddressOrEth, UniqueId } from '@/__swaps__/types/assets';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { UniqueId } from '@/__swaps__/types/assets';
import { getUniqueId } from '@/utils/ethereumUtils';
import { EthereumAddress, RainbowToken } from '@/entities';
import { createQueryKey, persistOptions, queryClient } from '@/react-query';
import { favoritesQueryKey } from '@/resources/favorites';
Expand All @@ -22,7 +22,7 @@ export function migrateFavoritesV2(): Migration {

const migratedFavorites: Record<UniqueId, RainbowToken> = {};
for (const favorite of Object.values(v1Data)) {
const uniqueId = getUniqueIdWorklet(favorite.address, favorite.chainId);
const uniqueId = getUniqueId(favorite.address, favorite.chainId);
favorite.uniqueId = uniqueId; // v2 unique uses chainId instead of Network
migratedFavorites[uniqueId] = favorite;
}
Expand Down
10 changes: 5 additions & 5 deletions src/resources/favorites.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddressOrEth, UniqueId } from '@/__swaps__/types/assets';
import { ChainId, Network } from '@/chains/types';
import { getUniqueIdWorklet } from '@/utils/ethereumUtils';
import { getUniqueId } from '@/utils/ethereumUtils';
import { NativeCurrencyKeys, RainbowToken } from '@/entities';
import { createQueryKey, queryClient } from '@/react-query';
import { DAI_ADDRESS, ETH_ADDRESS, SOCKS_ADDRESS, WBTC_ADDRESS, WETH_ADDRESS } from '@/references';
Expand Down Expand Up @@ -34,7 +34,7 @@ async function fetchMetadata(addresses: string[], chainId = ChainId.mainnet) {
);

if (externalAsset) {
const uniqueId = getUniqueIdWorklet(externalAsset?.networks[chainId]?.address, chainId);
const uniqueId = getUniqueId(externalAsset?.networks[chainId]?.address, chainId);
newFavoritesMeta[uniqueId] = {
...externalAsset,
chainId,
Expand All @@ -54,10 +54,10 @@ async function fetchMetadata(addresses: string[], chainId = ChainId.mainnet) {
const ethIsFavorited = addresses.includes(ETH_ADDRESS);
const wethIsFavorited = addresses.includes(WETH_ADDRESS);
if (newFavoritesMeta) {
const WETH_uniqueId = getUniqueIdWorklet(WETH_ADDRESS, ChainId.mainnet);
const WETH_uniqueId = getUniqueId(WETH_ADDRESS, ChainId.mainnet);
if (newFavoritesMeta[WETH_uniqueId] && ethIsFavorited) {
const favorite = newFavoritesMeta[WETH_uniqueId];
const uniqueId = getUniqueIdWorklet(ETH_ADDRESS, ChainId.mainnet);
const uniqueId = getUniqueId(ETH_ADDRESS, ChainId.mainnet);
newFavoritesMeta[uniqueId] = {
...favorite,
address: ETH_ADDRESS,
Expand Down Expand Up @@ -117,7 +117,7 @@ export async function refreshFavorites() {
export async function toggleFavorite(address: string, chainId = ChainId.mainnet) {
const favorites = queryClient.getQueryData<Record<UniqueId, RainbowToken>>(favoritesQueryKey);
const lowercasedAddress = address.toLowerCase() as AddressOrEth;
const uniqueId = getUniqueIdWorklet(lowercasedAddress, chainId);
const uniqueId = getUniqueId(lowercasedAddress, chainId);
if (Object.keys(favorites || {}).includes(uniqueId)) {
queryClient.setQueryData(favoritesQueryKey, omit(favorites, uniqueId));
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/utils/ethereumUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,7 @@ async function parseEthereumUrl(data: string) {

export const getUniqueIdNetwork = (address: EthereumAddress, network: Network) => `${address}_${network}`;

export const getUniqueId = (address: EthereumAddress, chainId: ChainId) => `${address}_${chainId}`;

export const getUniqueIdWorklet = (address: EthereumAddress, chainId: ChainId) => {
export const getUniqueId = (address: EthereumAddress, chainId: ChainId) => {
'worklet';
return `${address}_${chainId}`;
};
Expand Down

0 comments on commit a57eb89

Please sign in to comment.