1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-21 17:37:01 +01:00

fix(20316): enrich error message for fetch-with-cache (#20742)

This commit is contained in:
Danica Shen 2023-09-06 15:02:22 +01:00 committed by GitHub
parent 0f938c5dc8
commit 38876dc450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 164 additions and 115 deletions

View File

@ -172,11 +172,12 @@ export default class SwapsController {
} }
async fetchSwapsNetworkConfig(chainId) { async fetchSwapsNetworkConfig(chainId) {
const response = await fetchWithCache( const response = await fetchWithCache({
getBaseApi('network', chainId), url: getBaseApi('network', chainId),
{ method: 'GET' }, fetchOptions: { method: 'GET' },
{ cacheRefreshTime: 600000 }, cacheOptions: { cacheRefreshTime: 600000 },
); functionName: 'fetchSwapsNetworkConfig',
});
const { refreshRates, parameters = {} } = response || {}; const { refreshRates, parameters = {} } = response || {};
if ( if (
!refreshRates || !refreshRates ||

View File

@ -21,9 +21,11 @@ describe('Fetch with cache', () => {
.get('/price') .get('/price')
.reply(200, '{"average": 1}'); .reply(200, '{"average": 1}');
const response = await fetchWithCache( const response = await fetchWithCache({
'https://fetchwithcache.metamask.io/price', url: 'https://fetchwithcache.metamask.io/price',
); functionName: 'fetchPrice',
});
expect(response).toStrictEqual({ expect(response).toStrictEqual({
average: 1, average: 1,
}); });
@ -39,9 +41,10 @@ describe('Fetch with cache', () => {
cachedTime: Date.now(), cachedTime: Date.now(),
}); });
const response = await fetchWithCache( const response = await fetchWithCache({
'https://fetchwithcache.metamask.io/price', url: 'https://fetchwithcache.metamask.io/price',
); functionName: 'fetchPrice',
});
expect(response).toStrictEqual({ expect(response).toStrictEqual({
average: 1, average: 1,
}); });
@ -57,11 +60,11 @@ describe('Fetch with cache', () => {
cachedTime: Date.now() - 1000, cachedTime: Date.now() - 1000,
}); });
const response = await fetchWithCache( const response = await fetchWithCache({
'https://fetchwithcache.metamask.io/price', url: 'https://fetchwithcache.metamask.io/price',
{}, cacheOptions: { cacheRefreshTime: 123 },
{ cacheRefreshTime: 123 }, functionName: 'fetchPrice',
); });
expect(response).toStrictEqual({ expect(response).toStrictEqual({
average: 3, average: 3,
}); });
@ -74,11 +77,11 @@ describe('Fetch with cache', () => {
.reply(200, '{"average": 4}'); .reply(200, '{"average": 4}');
await expect(() => await expect(() =>
fetchWithCache( fetchWithCache({
'https://fetchwithcache.metamask.io/price', url: 'https://fetchwithcache.metamask.io/price',
{}, cacheOptions: { timeout: 20 },
{ timeout: 20 }, functionName: 'fetchPrice',
), }),
).rejects.toThrow({ ).rejects.toThrow({
name: 'AbortError', name: 'AbortError',
message: 'The user aborted a request.', message: 'The user aborted a request.',
@ -91,7 +94,10 @@ describe('Fetch with cache', () => {
.reply(500, '{"average": 6}'); .reply(500, '{"average": 6}');
await expect(() => await expect(() =>
fetchWithCache('https://fetchwithcache.metamask.io/price'), fetchWithCache({
url: 'https://fetchwithcache.metamask.io/price',
functionName: 'fetchPrice',
}),
).rejects.toThrow(''); ).rejects.toThrow('');
}); });
@ -101,8 +107,10 @@ describe('Fetch with cache', () => {
.reply(200, '{"average": 7}'); .reply(200, '{"average": 7}');
await expect(() => await expect(() =>
fetchWithCache('https://fetchwithcache.metamask.io/price', { fetchWithCache({
method: 'POST', url: 'https://fetchwithcache.metamask.io/price',
fetchOptions: { method: 'POST' },
functionName: 'fetchPrice',
}), }),
).rejects.toThrow(''); ).rejects.toThrow('');
}); });
@ -113,7 +121,11 @@ describe('Fetch with cache', () => {
.reply(200, '{"average": 8}'); .reply(200, '{"average": 8}');
await expect(() => await expect(() =>
fetchWithCache('https://fetchwithcache.metamask.io/price', { body: 1 }), fetchWithCache({
url: 'https://fetchwithcache.metamask.io/price',
fetchOptions: { body: 1 },
functionName: 'fetchPrice',
}),
).rejects.toThrow(''); ).rejects.toThrow('');
}); });
@ -123,8 +135,10 @@ describe('Fetch with cache', () => {
.reply(200, '{"average": 9}'); .reply(200, '{"average": 9}');
await expect(() => await expect(() =>
fetchWithCache('https://fetchwithcache.metamask.io/price', { fetchWithCache({
headers: { 'Content-Type': 'text/plain' }, url: 'https://fetchwithcache.metamask.io/price',
fetchOptions: { headers: { 'Content-Type': 'text/plain' } },
functionName: 'fetchPrice',
}), }),
).rejects.toThrow({ ).rejects.toThrow({
message: 'fetchWithCache only supports JSON responses', message: 'fetchWithCache only supports JSON responses',
@ -147,16 +161,16 @@ describe('Fetch with cache', () => {
}); });
await Promise.all([ await Promise.all([
fetchWithCache( fetchWithCache({
'https://fetchwithcache.metamask.io/foo', url: 'https://fetchwithcache.metamask.io/foo',
{}, cacheOptions: { cacheRefreshTime: 123 },
{ cacheRefreshTime: 123 }, functionName: 'fetchFoo',
), }),
fetchWithCache( fetchWithCache({
'https://fetchwithcache.metamask.io/bar', url: 'https://fetchwithcache.metamask.io/bar',
{}, cacheOptions: { cacheRefreshTime: 123 },
{ cacheRefreshTime: 123 }, functionName: 'fetchFoo',
), }),
]); ]);
expect( expect(

View File

@ -2,11 +2,17 @@ import { MINUTE, SECOND } from '../constants/time';
import getFetchWithTimeout from '../modules/fetch-with-timeout'; import getFetchWithTimeout from '../modules/fetch-with-timeout';
import { getStorageItem, setStorageItem } from './storage-helpers'; import { getStorageItem, setStorageItem } from './storage-helpers';
const fetchWithCache = async ( const fetchWithCache = async ({
url, url,
fetchOptions = {}, fetchOptions = {},
{ cacheRefreshTime = MINUTE * 6, timeout = SECOND * 30 } = {}, cacheOptions: { cacheRefreshTime = MINUTE * 6, timeout = SECOND * 30 } = {},
) => { functionName = '',
}: {
url: string;
fetchOptions?: Record<string, any>;
cacheOptions?: Record<string, any>;
functionName: string;
}) => {
if ( if (
fetchOptions.body || fetchOptions.body ||
(fetchOptions.method && fetchOptions.method !== 'GET') (fetchOptions.method && fetchOptions.method !== 'GET')
@ -40,7 +46,7 @@ const fetchWithCache = async (
}); });
if (!response.ok) { if (!response.ok) {
throw new Error( throw new Error(
`Fetch failed with status '${response.status}': '${response.statusText}'`, `Fetch with cache failed within function ${functionName} with status'${response.status}': '${response.statusText}'`,
); );
} }
const responseJson = await response.json(); const responseJson = await response.json();

View File

@ -274,11 +274,12 @@ export async function fetchTradesInfo(
const queryString = new URLSearchParams(urlParams).toString(); const queryString = new URLSearchParams(urlParams).toString();
const tradeURL = `${getBaseApi('trade', chainId)}${queryString}`; const tradeURL = `${getBaseApi('trade', chainId)}${queryString}`;
const tradesResponse = await fetchWithCache( const tradesResponse = await fetchWithCache({
tradeURL, url: tradeURL,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: 0, timeout: SECOND * 15 }, cacheOptions: { cacheRefreshTime: 0, timeout: SECOND * 15 },
); functionName: 'fetchTradesInfo',
});
const newQuotes = tradesResponse.reduce((aggIdTradeMap, quote) => { const newQuotes = tradesResponse.reduce((aggIdTradeMap, quote) => {
if ( if (
quote.trade && quote.trade &&

View File

@ -38,8 +38,10 @@ export default function TransactionDecoding({ to = '', inputData: data = '' }) {
(async () => { (async () => {
setLoading(true); setLoading(true);
try { try {
const networks = await fetchWithCache(FETCH_SUPPORTED_NETWORKS_URI, { const networks = await fetchWithCache({
method: 'GET', url: FETCH_SUPPORTED_NETWORKS_URI,
fetchOptions: { method: 'GET' },
functionName: 'fetchSupportedNetworks',
}); });
if ( if (
@ -57,7 +59,11 @@ export default function TransactionDecoding({ to = '', inputData: data = '' }) {
'network-id': network, 'network-id': network,
})}`; })}`;
const response = await fetchWithCache(requestUrl, { method: 'GET' }); const response = await fetchWithCache({
url: requestUrl,
fetchOptions: { method: 'GET' },
functionName: 'fetchProject',
});
const { info: projectInfo, fetchedVia, address } = response; const { info: projectInfo, fetchedVia, address } = response;

View File

@ -49,9 +49,10 @@ const NewNetworkInfo = () => {
}; };
const getIsTokenDetectionSupported = async () => { const getIsTokenDetectionSupported = async () => {
const fetchedTokenData = await fetchWithCache( const fetchedTokenData = await fetchWithCache({
`${TOKEN_API_METASWAP_CODEFI_URL}${providerConfig.chainId}`, url: `${TOKEN_API_METASWAP_CODEFI_URL}${providerConfig.chainId}`,
); functionName: 'getIsTokenDetectionSupported',
});
return !fetchedTokenData.error; return !fetchedTokenData.error;
}; };

View File

@ -33,9 +33,10 @@ describe('NewNetworkInfo', () => {
'[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]', '[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]',
); );
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x1', url: 'https://token-api.metaswap.codefi.network/tokens/0x1',
); functionName: 'getTokenDetectionSupportStatus',
});
const store = configureMockStore()( const store = configureMockStore()(
state, state,
@ -54,9 +55,10 @@ describe('NewNetworkInfo', () => {
'[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]', '[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]',
); );
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x1', url: 'https://token-api.metaswap.codefi.network/tokens/0x1',
); functionName: 'getTokenDetectionSupportStatus',
});
state.metamask.nativeCurrency = ''; state.metamask.nativeCurrency = '';
@ -77,10 +79,10 @@ describe('NewNetworkInfo', () => {
200, 200,
'[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]', '[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]',
); );
const updateTokenDetectionSupportStatus = await fetchWithCache({
const updateTokenDetectionSupportStatus = await fetchWithCache( url: 'https://token-api.metaswap.codefi.network/tokens/0x1',
'https://token-api.metaswap.codefi.network/tokens/0x1', functionName: 'getTokenDetectionSupportStatus',
); });
const store = configureMockStore()( const store = configureMockStore()(
state, state,
@ -99,9 +101,10 @@ describe('NewNetworkInfo', () => {
'[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]', '[{"address":"0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f","symbol":"SNX","decimals":18,"name":"Synthetix Network Token","iconUrl":"https://assets.coingecko.com/coins/images/3406/large/SNX.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","synthetix","zapper","zerion","zeroEx"],"occurrences":12},{"address":"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984","symbol":"UNI","decimals":18,"name":"Uniswap","iconUrl":"https://images.prismic.io/token-price-prod/d0352dd9-5de8-4633-839d-bc3422c44d9c_UNI%404x.png","aggregators":["aave","bancor","cmc","cryptocom","coinGecko","oneInch","paraswap","pmm","zapper","zerion","zeroEx"],"occurrences":11}]',
); );
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x1', url: 'https://token-api.metaswap.codefi.network/tokens/0x1',
); functionName: 'getTokenDetectionSupportStatus',
});
const store = configureMockStore()( const store = configureMockStore()(
state, state,
@ -117,9 +120,10 @@ describe('NewNetworkInfo', () => {
.get('/tokens/0x3') .get('/tokens/0x3')
.reply(200, '{"error":"ChainId 0x3 is not supported"}'); .reply(200, '{"error":"ChainId 0x3 is not supported"}');
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x3', url: 'https://token-api.metaswap.codefi.network/tokens/0x3',
); functionName: 'getTokenDetectionSupportStatus',
});
const store = configureMockStore()( const store = configureMockStore()(
state, state,
@ -135,9 +139,10 @@ describe('NewNetworkInfo', () => {
.get('/tokens/0x3') .get('/tokens/0x3')
.reply(200, '{"error":"ChainId 0x3 is not supported"}'); .reply(200, '{"error":"ChainId 0x3 is not supported"}');
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x3', url: 'https://token-api.metaswap.codefi.network/tokens/0x3',
); functionName: 'getTokenDetectionSupportStatus',
});
state.metamask.providerConfig.ticker = null; state.metamask.providerConfig.ticker = null;
@ -156,9 +161,10 @@ describe('NewNetworkInfo', () => {
.get('/tokens/0x3') .get('/tokens/0x3')
.reply(200, '{"error":"ChainId 0x3 is not supported"}'); .reply(200, '{"error":"ChainId 0x3 is not supported"}');
const updateTokenDetectionSupportStatus = await fetchWithCache( const updateTokenDetectionSupportStatus = await fetchWithCache({
'https://token-api.metaswap.codefi.network/tokens/0x3', url: 'https://token-api.metaswap.codefi.network/tokens/0x3',
); functionName: 'getTokenDetectionSupportStatus',
});
const store = configureMockStore()( const store = configureMockStore()(
state, state,

View File

@ -26,15 +26,16 @@ import fetchWithCache from '../../../shared/lib/fetch-with-cache';
*/ */
async function getMethodFrom4Byte(fourBytePrefix) { async function getMethodFrom4Byte(fourBytePrefix) {
const fourByteResponse = await fetchWithCache( const fourByteResponse = await fetchWithCache({
`https://www.4byte.directory/api/v1/signatures/?hex_signature=${fourBytePrefix}`, url: `https://www.4byte.directory/api/v1/signatures/?hex_signature=${fourBytePrefix}`,
{ fetchOptions: {
referrerPolicy: 'no-referrer-when-downgrade', referrerPolicy: 'no-referrer-when-downgrade',
body: null, body: null,
method: 'GET', method: 'GET',
mode: 'cors', mode: 'cors',
}, },
); functionName: 'getMethodFrom4Byte',
});
fourByteResponse.results.sort((a, b) => { fourByteResponse.results.sort((a, b) => {
return new Date(a.created_at).getTime() < new Date(b.created_at).getTime() return new Date(a.created_at).getTime() < new Date(b.created_at).getTime()
? -1 ? -1

View File

@ -134,7 +134,10 @@ const ERROR_CONNECTING_TO_RPC = {
async function getAlerts(pendingApproval) { async function getAlerts(pendingApproval) {
const alerts = []; const alerts = [];
const safeChainsList = const safeChainsList =
(await fetchWithCache('https://chainid.network/chains.json')) || []; (await fetchWithCache({
url: 'https://chainid.network/chains.json',
functionName: 'getSafeChainsList',
})) || [];
const matchedChain = safeChainsList.find( const matchedChain = safeChainsList.find(
(chain) => (chain) =>
chain.chainId === parseInt(pendingApproval.requestData.chainId, 16), chain.chainId === parseInt(pendingApproval.requestData.chainId, 16),

View File

@ -356,7 +356,10 @@ const NetworksForm = ({
try { try {
safeChainsList = safeChainsList =
(await fetchWithCache('https://chainid.network/chains.json')) || []; (await fetchWithCache({
url: 'https://chainid.network/chains.json',
functionName: 'getSafeChainsList',
})) || [];
} 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;

View File

@ -117,11 +117,12 @@ export async function fetchToken(
chainId: any, chainId: any,
): Promise<Json> { ): Promise<Json> {
const tokenUrl = getBaseApi('token', chainId); const tokenUrl = getBaseApi('token', chainId);
return await fetchWithCache( return await fetchWithCache({
`${tokenUrl}?address=${contractAddress}`, url: `${tokenUrl}?address=${contractAddress}`,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES }, cacheOptions: { cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
); functionName: 'fetchToken',
});
} }
type Token = { symbol: string; address: string }; type Token = { symbol: string; address: string };
@ -129,11 +130,12 @@ export async function fetchTokens(
chainId: keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP, chainId: keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP,
): Promise<SwapsTokenObject[]> { ): Promise<SwapsTokenObject[]> {
const tokensUrl = getBaseApi('tokens', chainId); const tokensUrl = getBaseApi('tokens', chainId);
const tokens = await fetchWithCache( const tokens = await fetchWithCache({
tokensUrl, url: tokensUrl,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES }, cacheOptions: { cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
); functionName: 'fetchTokens',
});
const logError = false; const logError = false;
const tokenObject = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId] || null; const tokenObject = SWAPS_CHAINID_DEFAULT_TOKEN_MAP[chainId] || null;
return [ return [
@ -152,11 +154,12 @@ export async function fetchTokens(
export async function fetchAggregatorMetadata(chainId: any): Promise<object> { export async function fetchAggregatorMetadata(chainId: any): Promise<object> {
const aggregatorMetadataUrl = getBaseApi('aggregatorMetadata', chainId); const aggregatorMetadataUrl = getBaseApi('aggregatorMetadata', chainId);
const aggregators = await fetchWithCache( const aggregators = await fetchWithCache({
aggregatorMetadataUrl, url: aggregatorMetadataUrl,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES }, cacheOptions: { cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
); functionName: 'fetchAggregatorMetadata',
});
const filteredAggregators = {} as any; const filteredAggregators = {} as any;
for (const aggKey in aggregators) { for (const aggKey in aggregators) {
if ( if (
@ -175,11 +178,12 @@ export async function fetchAggregatorMetadata(chainId: any): Promise<object> {
export async function fetchTopAssets(chainId: any): Promise<object> { export async function fetchTopAssets(chainId: any): Promise<object> {
const topAssetsUrl = getBaseApi('topAssets', chainId); const topAssetsUrl = getBaseApi('topAssets', chainId);
const response = const response =
(await fetchWithCache( (await fetchWithCache({
topAssetsUrl, url: topAssetsUrl,
{ method: 'GET', headers: clientIdHeader }, functionName: 'fetchTopAssets',
{ cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES }, fetchOptions: { method: 'GET', headers: clientIdHeader },
)) || []; cacheOptions: { cacheRefreshTime: CACHE_REFRESH_FIVE_MINUTES },
})) || [];
const topAssetsMap = response.reduce( const topAssetsMap = response.reduce(
(_topAssetsMap: any, asset: { address: string }, index: number) => { (_topAssetsMap: any, asset: { address: string }, index: number) => {
if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) { if (validateData(TOP_ASSET_VALIDATORS, asset, topAssetsUrl)) {
@ -196,21 +200,23 @@ export async function fetchSwapsFeatureFlags(): Promise<any> {
const v2ApiBaseUrl = process.env.SWAPS_USE_DEV_APIS const v2ApiBaseUrl = process.env.SWAPS_USE_DEV_APIS
? SWAPS_DEV_API_V2_BASE_URL ? SWAPS_DEV_API_V2_BASE_URL
: SWAPS_API_V2_BASE_URL; : SWAPS_API_V2_BASE_URL;
return await fetchWithCache( return await fetchWithCache({
`${v2ApiBaseUrl}/featureFlags`, url: `${v2ApiBaseUrl}/featureFlags`,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: 600000 }, cacheOptions: { cacheRefreshTime: 600000 },
); functionName: 'fetchSwapsFeatureFlags',
});
} }
export async function fetchTokenPrice(address: string): Promise<any> { export async function fetchTokenPrice(address: string): Promise<any> {
const query = `contract_addresses=${address}&vs_currencies=eth`; const query = `contract_addresses=${address}&vs_currencies=eth`;
const prices = await fetchWithCache( const prices = await fetchWithCache({
`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`, url: `https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`,
{ method: 'GET' }, fetchOptions: { method: 'GET' },
{ cacheRefreshTime: 60000 }, cacheOptions: { cacheRefreshTime: 60000 },
); functionName: 'fetchTokenPrice',
});
return prices?.[address]?.eth; return prices?.[address]?.eth;
} }
@ -223,11 +229,12 @@ export async function fetchSwapsGasPrices(chainId: any): Promise<
} }
> { > {
const gasPricesUrl = getBaseApi('gasPrices', chainId); const gasPricesUrl = getBaseApi('gasPrices', chainId);
const response = await fetchWithCache( const response = await fetchWithCache({
gasPricesUrl, url: gasPricesUrl,
{ method: 'GET', headers: clientIdHeader }, fetchOptions: { method: 'GET', headers: clientIdHeader },
{ cacheRefreshTime: 30000 }, cacheOptions: { cacheRefreshTime: 30000 },
); functionName: 'fetchSwapsGasPrices',
});
const responseIsValid = validateData( const responseIsValid = validateData(
SWAP_GAS_PRICE_VALIDATOR, SWAP_GAS_PRICE_VALIDATOR,
response, response,