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:
parent
c833e41eee
commit
f6576801d4
@ -71,9 +71,8 @@ const INVALID_CHAIN = {
|
|||||||
|
|
||||||
async function getAlerts(pendingApproval) {
|
async function getAlerts(pendingApproval) {
|
||||||
const alerts = [];
|
const alerts = [];
|
||||||
const safeChainsList = await fetchWithCache(
|
const safeChainsList =
|
||||||
'https://chainid.network/chains.json',
|
(await fetchWithCache('https://chainid.network/chains.json')) || [];
|
||||||
);
|
|
||||||
const matchedChain = safeChainsList.find(
|
const matchedChain = safeChainsList.find(
|
||||||
(chain) =>
|
(chain) =>
|
||||||
chain.chainId === parseInt(pendingApproval.requestData.chainId, 16),
|
chain.chainId === parseInt(pendingApproval.requestData.chainId, 16),
|
||||||
|
@ -328,15 +328,14 @@ const NetworksForm = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
safeChainsList = await fetchWithCache(
|
safeChainsList =
|
||||||
'https://chainid.network/chains.json',
|
(await fetchWithCache('https://chainid.network/chains.json')) || [];
|
||||||
);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn('Failed to fetch the chainList from chainid.network', err);
|
log.warn('Failed to fetch the chainList from chainid.network', err);
|
||||||
providerError = err;
|
providerError = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (providerError || !Array.isArray(safeChainsList)) {
|
if (providerError) {
|
||||||
warningKey = 'failedToFetchTickerSymbolData';
|
warningKey = 'failedToFetchTickerSymbolData';
|
||||||
warningMessage = t('failedToFetchTickerSymbolData');
|
warningMessage = t('failedToFetchTickerSymbolData');
|
||||||
} else {
|
} else {
|
||||||
|
@ -157,11 +157,21 @@ export default function Swap() {
|
|||||||
const showSmartTransactionsErrorMessage =
|
const showSmartTransactionsErrorMessage =
|
||||||
currentSmartTransactionsError && !smartTransactionsErrorMessageDismissed;
|
currentSmartTransactionsError && !smartTransactionsErrorMessageDismissed;
|
||||||
|
|
||||||
if (networkAndAccountSupports1559) {
|
useEffect(() => {
|
||||||
// This will pre-load gas fees before going to the View Quote page.
|
const leaveSwaps = async () => {
|
||||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
await dispatch(prepareToLeaveSwaps());
|
||||||
useGasFeeEstimates();
|
// 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 {
|
const {
|
||||||
balance: ethBalance,
|
balance: ethBalance,
|
||||||
@ -223,6 +233,9 @@ export default function Swap() {
|
|||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!isSwapsChain) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
fetchTokens(chainId)
|
fetchTokens(chainId)
|
||||||
.then((tokens) => {
|
.then((tokens) => {
|
||||||
dispatch(setSwapsTokens(tokens));
|
dispatch(setSwapsTokens(tokens));
|
||||||
@ -240,7 +253,7 @@ export default function Swap() {
|
|||||||
return () => {
|
return () => {
|
||||||
dispatch(prepareToLeaveSwaps());
|
dispatch(prepareToLeaveSwaps());
|
||||||
};
|
};
|
||||||
}, [dispatch, chainId, networkAndAccountSupports1559]);
|
}, [dispatch, chainId, networkAndAccountSupports1559, isSwapsChain]);
|
||||||
|
|
||||||
const hardwareWalletUsed = useSelector(isHardwareWallet);
|
const hardwareWalletUsed = useSelector(isHardwareWallet);
|
||||||
const hardwareWalletType = useSelector(getHardwareWalletType);
|
const hardwareWalletType = useSelector(getHardwareWalletType);
|
||||||
@ -353,7 +366,9 @@ export default function Swap() {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
if (!isSwapsChain) {
|
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 =
|
const isStxNotEnoughFundsError =
|
||||||
|
@ -420,11 +420,12 @@ export async function fetchAggregatorMetadata(chainId) {
|
|||||||
|
|
||||||
export async function fetchTopAssets(chainId) {
|
export async function fetchTopAssets(chainId) {
|
||||||
const topAssetsUrl = getBaseApi('topAssets', chainId);
|
const topAssetsUrl = getBaseApi('topAssets', chainId);
|
||||||
const response = await fetchWithCache(
|
const response =
|
||||||
topAssetsUrl,
|
(await fetchWithCache(
|
||||||
{ method: 'GET', headers: clientIdHeader },
|
topAssetsUrl,
|
||||||
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
|
{ method: 'GET', headers: clientIdHeader },
|
||||||
);
|
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
|
||||||
|
)) || [];
|
||||||
const topAssetsMap = response.reduce((_topAssetsMap, asset, index) => {
|
const topAssetsMap = response.reduce((_topAssetsMap, asset, index) => {
|
||||||
if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) {
|
if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) {
|
||||||
return { ..._topAssetsMap, [asset.address]: { index: String(index) } };
|
return { ..._topAssetsMap, [asset.address]: { index: String(index) } };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user