Skip to content

Commit

Permalink
[APP-946]: Fixes swap input currencylist value bug (#5221)
Browse files Browse the repository at this point in the history
* Revert "[APP-379]: Update client to use new token search aggregator across networks (#5190)"

This reverts commit e85c3fe.

* fix useEffect not fetching proper trade options on input

* [APP-379]: Update client to use new token search aggregator across networks (#5190)

* update rainbow fetch to be typed and update token search endpoint

* fix swap currency list

* update interfaces and transforms

* idkwtf i'm doing

* remove unused mapping

* revert swap changes and fix icon_url

* fix lint

* Update src/hooks/useSwappableUserAssets.ts

* fix prettier
  • Loading branch information
walmat committed Dec 5, 2023
1 parent e85c3fe commit 4788d34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 57 deletions.
36 changes: 19 additions & 17 deletions src/hooks/useSwappableUserAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ETH_ADDRESS as ETH_ADDRESS_AGGREGATORS,
} from '@rainbow-me/swaps';
import { ethereumUtils } from '@/utils';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { RainbowNetworks, getNetworkObj } from '@/networks';

type SwappableAddresses = {
Expand All @@ -26,7 +26,8 @@ export const useSwappableUserAssets = (params: {
const { data: sortedAssets } = useSortedUserAssets();
const assetsInWallet = sortedAssets as SwappableAsset[];
const { hiddenCoinsObj } = useCoinListEditOptions();
const [swappableAssets, setSwappableAssets] = useState<SwappableAddresses>({

const swappableAssetsRef = useRef<SwappableAddresses>({
[Network.mainnet]: [],
[Network.optimism]: [],
[Network.polygon]: [],
Expand All @@ -49,7 +50,7 @@ export const useSwappableUserAssets = (params: {
);

const getSwappableAddressesForNetwork = useCallback(
async (addresses: EthereumAddress[], network: Network) => {
async (addresses: EthereumAddress[], network: keyof SwappableAddresses) => {
try {
if (outputCurrency) {
const outputNetwork =
Expand All @@ -64,16 +65,14 @@ export const useSwappableUserAssets = (params: {
outputNetwork as Network
),
})) as string[];
setSwappableAssets(state => ({
...state,
[network]: swappableAddresses,
}));

swappableAssetsRef.current[network] = swappableAddresses;
} else {
setSwappableAssets(state => ({ ...state, [network]: addresses }));
swappableAssetsRef.current[network] = addresses;
}
}
} catch (e) {
setSwappableAssets(state => ({ ...state, [network]: addresses }));
swappableAssetsRef.current[network] = addresses;
}
},
[outputCurrency]
Expand All @@ -95,7 +94,10 @@ export const useSwappableUserAssets = (params: {
);
if (assetsAddressesOnChain.length) {
walletFilterRequests.push(
getSwappableAddressesForNetwork(assetsAddressesOnChain, network)
getSwappableAddressesForNetwork(
assetsAddressesOnChain,
network as keyof SwappableAddresses
)
);
}
});
Expand All @@ -112,12 +114,12 @@ export const useSwappableUserAssets = (params: {
: asset?.address;
// we place our testnets (goerli) in the Network type which creates this type issue
// @ts-ignore
const isSwappable = swappableAssets[assetNetwork]?.includes(
const isSwappable = swappableAssetsRef.current[assetNetwork]?.includes(
assetAddress
);
return isSwappable;
}),
[filteredAssetsInWallet, swappableAssets]
[filteredAssetsInWallet]
);

const unswappableUserAssets = useMemo(
Expand All @@ -130,17 +132,17 @@ export const useSwappableUserAssets = (params: {
: asset?.address;
// we place our testnets (goerli) in the Network type which creates this type issue
// @ts-ignore
const isNotSwappable = !swappableAssets[assetNetwork]?.includes(
assetAddress
);
const isNotSwappable = !swappableAssetsRef.current[
assetNetwork
]?.includes(assetAddress);
return isNotSwappable;
}),
[filteredAssetsInWallet, swappableAssets]
[filteredAssetsInWallet]
);

useEffect(() => {
getSwappableAddressesInWallet();
}, []);
}, [getSwappableAddressesInWallet]);

if (!outputCurrency) {
return {
Expand Down
8 changes: 4 additions & 4 deletions src/resources/assets/assetSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ const sortAssetsByNativeAmount = (
} = groupAssetsByMarketValue(assetsNativePrices);

const sortedAssetsNoShitcoins = hasValue.sort((a: any, b: any) => {
let itemA = Number(a.native?.balance?.amount) ?? 0;
let itemB = Number(b.native?.balance?.amount) ?? 0;
const itemA = Number(a.native?.balance?.amount) ?? 0;
const itemB = Number(b.native?.balance?.amount) ?? 0;

return itemA < itemB ? 1 : -1;
});

const sortedShitcoins = noValue.sort((a: any, b: any) => {
let itemA = a.name;
let itemB = b.name;
const itemA = a.name;
const itemB = b.name;

return itemA > itemB ? 1 : -1;
});
Expand Down
38 changes: 2 additions & 36 deletions src/screens/CurrencySelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export default function CurrencySelectModal() {
const [searchQueryForSearch] = useDebounce(searchQuery, 350);
const { data: sortedAssets } = useSortedUserAssets();
const assetsInWallet = sortedAssets as SwappableAsset[];
const { hiddenCoinsObj } = useCoinListEditOptions();

const [currentChainId, setCurrentChainId] = useState(chainId);
const prevChainId = usePrevious(currentChainId);
Expand All @@ -169,34 +168,6 @@ export default function CurrencySelectModal() {

const { inputCurrency, outputCurrency } = useSwapCurrencies();

// TODO: remove this value when crosschain swaps is released
const filteredAssetsInWallet = useMemo(() => {
if (type === CurrencySelectionTypes.input) {
let filteredAssetsInWallet = assetsInWallet?.filter(
asset => !hiddenCoinsObj[asset.uniqueId]
);
// TODO: remove this once BACK-219 is fixed
if (fromDiscover && defaultOutputAsset?.implementations) {
const outputTokenNetworks = Object.keys(
defaultOutputAsset?.implementations
);

filteredAssetsInWallet = filteredAssetsInWallet.filter(asset => {
const network = ethereumUtils.getNetworkFromType(asset.type);
return outputTokenNetworks.includes(network);
});
}
return filteredAssetsInWallet;
}
return [];
}, [
type,
assetsInWallet,
fromDiscover,
defaultOutputAsset?.implementations,
hiddenCoinsObj,
]);

const {
crosschainExactMatches,
swapCurrencyList,
Expand Down Expand Up @@ -250,14 +221,11 @@ export default function CurrencySelectModal() {
}, []);

const getWalletCurrencyList = useCallback(() => {
const listToUse = crosschainSwapsEnabled
? swappableUserAssets
: filteredAssetsInWallet;
let walletCurrencyList;
if (type === CurrencySelectionTypes.input) {
if (searchQueryForSearch !== '') {
const searchResults = searchWalletCurrencyList(
listToUse,
swappableUserAssets,
searchQueryForSearch
);
walletCurrencyList = headerlessSection(searchResults);
Expand All @@ -279,7 +247,7 @@ export default function CurrencySelectModal() {
}
return walletCurrencyList;
} else {
walletCurrencyList = headerlessSection(listToUse);
walletCurrencyList = headerlessSection(swappableUserAssets);
let unswappableAssets = unswappableUserAssets;
if (IS_TEST) {
unswappableAssets = unswappableAssets.concat({
Expand Down Expand Up @@ -308,7 +276,6 @@ export default function CurrencySelectModal() {
}
}
}, [
filteredAssetsInWallet,
searchQueryForSearch,
type,
crosschainSwapsEnabled,
Expand Down Expand Up @@ -493,7 +460,6 @@ export default function CurrencySelectModal() {
checkForRequiredAssets,
currentChainId,
type,
chainId,
checkForSameNetwork,
dispatch,
callback,
Expand Down

0 comments on commit 4788d34

Please sign in to comment.