1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Fix an edge case with missing top assets in Swaps (#14688)

* If there is no array of top assets in a response, use an empty array

* Set a default empty array for 2 functions, remove an unnecessary condition

* Redirect a user from Swaps to the homepage if they switch to a chain that is not supported in Swaps

* Fix errors in the UI Console when it's not a swaps chain
This commit is contained in:
Daniel 2022-05-24 18:30:46 +02:00 committed by GitHub
parent c833e41eee
commit f6576801d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 19 deletions

View File

@ -71,9 +71,8 @@ const INVALID_CHAIN = {
async function getAlerts(pendingApproval) {
const alerts = [];
const safeChainsList = await fetchWithCache(
'https://chainid.network/chains.json',
);
const safeChainsList =
(await fetchWithCache('https://chainid.network/chains.json')) || [];
const matchedChain = safeChainsList.find(
(chain) =>
chain.chainId === parseInt(pendingApproval.requestData.chainId, 16),

View File

@ -328,15 +328,14 @@ const NetworksForm = ({
}
try {
safeChainsList = await fetchWithCache(
'https://chainid.network/chains.json',
);
safeChainsList =
(await fetchWithCache('https://chainid.network/chains.json')) || [];
} catch (err) {
log.warn('Failed to fetch the chainList from chainid.network', err);
providerError = err;
}
if (providerError || !Array.isArray(safeChainsList)) {
if (providerError) {
warningKey = 'failedToFetchTickerSymbolData';
warningMessage = t('failedToFetchTickerSymbolData');
} else {

View File

@ -157,11 +157,21 @@ export default function Swap() {
const showSmartTransactionsErrorMessage =
currentSmartTransactionsError && !smartTransactionsErrorMessageDismissed;
if (networkAndAccountSupports1559) {
// This will pre-load gas fees before going to the View Quote page.
// eslint-disable-next-line react-hooks/rules-of-hooks
useGasFeeEstimates();
}
useEffect(() => {
const leaveSwaps = async () => {
await dispatch(prepareToLeaveSwaps());
// We need to wait until "prepareToLeaveSwaps" is done, because otherwise
// a user would be redirected from DEFAULT_ROUTE back to Swaps.
history.push(DEFAULT_ROUTE);
};
if (!isSwapsChain) {
leaveSwaps();
}
}, [isSwapsChain, dispatch, history]);
// This will pre-load gas fees before going to the View Quote page.
useGasFeeEstimates();
const {
balance: ethBalance,
@ -223,6 +233,9 @@ export default function Swap() {
// eslint-disable-next-line
useEffect(() => {
if (!isSwapsChain) {
return undefined;
}
fetchTokens(chainId)
.then((tokens) => {
dispatch(setSwapsTokens(tokens));
@ -240,7 +253,7 @@ export default function Swap() {
return () => {
dispatch(prepareToLeaveSwaps());
};
}, [dispatch, chainId, networkAndAccountSupports1559]);
}, [dispatch, chainId, networkAndAccountSupports1559, isSwapsChain]);
const hardwareWalletUsed = useSelector(isHardwareWallet);
const hardwareWalletType = useSelector(getHardwareWalletType);
@ -353,7 +366,9 @@ export default function Swap() {
]);
if (!isSwapsChain) {
return <Redirect to={{ pathname: DEFAULT_ROUTE }} />;
// A user is being redirected outside of Swaps via the async "leaveSwaps" function above. In the meantime
// we have to prevent the code below this condition, which wouldn't work on an unsupported chain.
return <></>;
}
const isStxNotEnoughFundsError =

View File

@ -420,11 +420,12 @@ export async function fetchAggregatorMetadata(chainId) {
export async function fetchTopAssets(chainId) {
const topAssetsUrl = getBaseApi('topAssets', chainId);
const response = await fetchWithCache(
topAssetsUrl,
{ method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
);
const response =
(await fetchWithCache(
topAssetsUrl,
{ method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
)) || [];
const topAssetsMap = response.reduce((_topAssetsMap, asset, index) => {
if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) {
return { ..._topAssetsMap, [asset.address]: { index: String(index) } };