diff --git a/pages/api/getAllPoolsVolume/index.js b/pages/api/getAllPoolsVolume/index.js index e2497235..69e5aa45 100644 --- a/pages/api/getAllPoolsVolume/index.js +++ b/pages/api/getAllPoolsVolume/index.js @@ -3,12 +3,25 @@ import { fn } from 'utils/api'; export default fn(async ({ blockchainId }) => { if (typeof blockchainId === 'undefined') blockchainId = 'ethereum'; // Default value - const { data: { totalVolume, cryptoShare } } = await (await fetch(`https://api.curve.fi/api/getSubgraphData/${blockchainId}`)).json(); + const { success, data } = await (await fetch(`https://api.curve.fi/api/getSubgraphData/${blockchainId}`)).json(); - return { - totalVolume, - cryptoShare - }; + if (success) { + const { totalVolume, cryptoShare } = data; + + return { + totalVolume, + cryptoShare + }; + } else { + // Fallback for chains without subgraph available; this won't be necessary anymore once we've moved + // to curve-prices for all chains + const { data } = await (await fetch(`https://api.curve.fi/api/getFactoryAPYs-${blockchainId}`)).json(); + + return { + totalVolume: data.totalVolume, + cryptoShare: undefined, + }; + } }, { maxAge: 10 * 60, // 10m }); diff --git a/pages/api/getPools/index.js b/pages/api/getPools/index.js index f1eba274..23ff46af 100644 --- a/pages/api/getPools/index.js +++ b/pages/api/getPools/index.js @@ -12,7 +12,7 @@ import Web3 from 'web3'; import BN from 'bignumber.js'; import groupBy from 'lodash.groupby'; -import { fn } from 'utils/api'; +import { fn, ParamError } from 'utils/api'; import factoryV2RegistryAbi from 'constants/abis/factory-v2-registry.json'; import factoryPoolAbi from 'constants/abis/factory-v2/Plain2Balances.json'; import factoryCryptoRegistryAbi from 'constants/abis/factory-crypto-registry.json'; @@ -119,7 +119,6 @@ const getEthereumOnlyData = async ({ preventQueryingFactoData, blockchainId }) = (await import('utils/data/getFactoryV2GaugeRewards')).default : (await import('utils/data/getFactoryV2SidechainGaugeRewards')).default ); - console.log('retrieve gaugesData...') /** * Here we want getGauges data (which itself calls getPools) to be available * whether api edge caches are hot or cold. This makes sure data is called @@ -127,7 +126,6 @@ const getEthereumOnlyData = async ({ preventQueryingFactoData, blockchainId }) = * caches are hot. */ gaugesData = (await (await fetch(BASE_API_DOMAIN + '/api/getAllGauges?blockchainId=' + blockchainId)).json()).data; - console.log('retrieved gaugesData!') if (blockchainId === 'ethereum') { const factoryGauges = Array.from(Object.values(gaugesData)).filter(({ side_chain }) => !side_chain); @@ -202,7 +200,7 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData }) const config = configs[blockchainId]; if (typeof config === 'undefined') { - throw new Error(`No config data for blockchainId "${blockchainId}"`); + throw new ParamError(`No config data for blockchainId "${blockchainId}"`); } if (config.hasNoMainRegistry && registryId === 'main') { @@ -232,7 +230,7 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData }) } = config; if (registryId !== 'factory' && registryId !== 'main' && registryId !== 'crypto' && registryId !== 'factory-crypto' && registryId !== 'factory-crvusd' && registryId !== 'factory-tricrypto' && registryId !== 'factory-eywa') { - throw new Error('registryId must be \'factory\'|\'main\'|\'crypto\'|\'factory-crypto\'|\'factory-crvusd\'|\'factory-tricrypto\'|\'factory-eywa\''); + throw new ParamError('registryId must be \'factory\'|\'main\'|\'crypto\'|\'factory-crypto\'|\'factory-crvusd\'|\'factory-tricrypto\'|\'factory-eywa\''); } const platformRegistries = (await getPlatformRegistries(blockchainId)).registryIds; diff --git a/pages/api/getSubgraphData/index.js b/pages/api/getSubgraphData/index.js index 202b5d38..7b886bfd 100644 --- a/pages/api/getSubgraphData/index.js +++ b/pages/api/getSubgraphData/index.js @@ -44,7 +44,6 @@ export default fn(async ({ blockchainId }) => { // If the newest, more accurate method of retrieving volumes is available // for this chain, return it instead with backward-compatible data structure if (AVAILABLE_CHAIN_IDS_FOR_GET_VOLUMES.includes(blockchainId)) { - console.log('USE GETVOLUMES') const data = await getVolumes.straightCall({ blockchainId }); return { diff --git a/utils/api.js b/utils/api.js index 8f29cb68..89090556 100644 --- a/utils/api.js +++ b/utils/api.js @@ -33,6 +33,13 @@ const logRuntime = async (fn, name, query, silenceParamsLog) => { return res; }; +class ParamError extends Error { + constructor(message) { + super(message); + this.name = this.constructor.name; + } +} + const fn = (cb, options = {}) => { const { maxAge: maxAgeSec = null, // Caching duration, in seconds @@ -61,10 +68,10 @@ const fn = (cb, options = {}) => { }) .catch((err) => { if (IS_DEV) { - console.log('ERROR that would be caught and served with success=false on prod', err); throw err; } else { - res.status(500).json(formatJsonError(err)); + const code = (err instanceof ParamError) ? 200 : 500; + res.status(code).json(formatJsonError(err)); } }) ); @@ -77,4 +84,5 @@ const fn = (cb, options = {}) => { export { fn, formatJsonError, + ParamError, };