Skip to content

Commit

Permalink
Make getAllPoolsVolume work for chains w/o subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
philippe-git committed Oct 16, 2023
1 parent 20df1c5 commit 5290ae0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
23 changes: 18 additions & 5 deletions pages/api/getAllPoolsVolume/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
8 changes: 3 additions & 5 deletions pages/api/getPools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -119,15 +119,13 @@ 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
* every time, but takes advantages of returning cached data very quickly once
* 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);
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion pages/api/getSubgraphData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
})
);
Expand All @@ -77,4 +84,5 @@ const fn = (cb, options = {}) => {
export {
fn,
formatJsonError,
ParamError,
};

1 comment on commit 5290ae0

@vercel
Copy link

@vercel vercel bot commented on 5290ae0 Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.