Skip to content

Commit

Permalink
Improve curve-prices usage
Browse files Browse the repository at this point in the history
Only use prices that arent stale
  • Loading branch information
philippe-git committed Dec 1, 2023
1 parent 3b72ef4 commit 388fe9e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
35 changes: 33 additions & 2 deletions pages/api/getPools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ const IGNORED_COINS = {
polygon: [
'0x8dacf090f8803f53ee3c44f0d7a07b9d70453c42', // spam
].map(lc),
ethereum: [
'0xc7D9c108D4E1dD1484D3e2568d7f74bfD763d356', // depegged stable, incorrect price on defillama
].map(lc),
};

// Tokens for which to use Defillama as external price oracle
Expand Down Expand Up @@ -437,6 +440,24 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData })
poolUsdTotalB > poolUsdTotalA ? 1 : 0
))[0].usdPrice,
]));
} else {
const METAPOOL_REGISTRIES_DEPENDENCIES = {
main: [],
default: ['main'],
};

const metapoolRegistryDependencies = (
METAPOOL_REGISTRIES_DEPENDENCIES[registryId] ||
METAPOOL_REGISTRIES_DEPENDENCIES.default
);

otherRegistryPoolsData = await sequentialPromiseFlatMap(metapoolRegistryDependencies, async (id) => (
// eslint-disable-next-line no-use-before-define
(await getPoolsFn.straightCall({ blockchainId, registryId: id, preventQueryingFactoData: true })).poolData.map((poolData) => ({
...poolData,
registryId: id,
}))
));
}

const poolDataWithTries = await multiCall(flattenArray(poolAddresses.map((address, i) => {
Expand Down Expand Up @@ -996,7 +1017,13 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData })

let missingCoinPrices;
if (USE_CURVE_PRICES_DATA) {
const coinsAddressesWithMissingPrices = uniq(Array.from(Object.values(mergedCoinData)).filter(({ usdPrice }) => usdPrice === null).map(({ address }) => lc(address)));
const coinsAddressesWithMissingPrices = uniq(Array.from(Object.values(mergedCoinData)).filter(({
address,
usdPrice,
}) => (
!IGNORED_COINS[blockchainId].includes(lc(address)) &&
usdPrice === null
)).map(({ address }) => lc(address)));
missingCoinPrices = await getTokensPrices(coinsAddressesWithMissingPrices, blockchainId);
}

Expand Down Expand Up @@ -1123,7 +1150,7 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData })
// here need to be able to retrieve from getPools/ethereum/base-pools, a special endpoint that returns only base pools, so it can be a cheap dependency
const underlyingPool = (
isMetaPool ? (
[...wipMergedPoolData, ...(USE_CURVE_PRICES_DATA ? [] : otherRegistryPoolsData)].find(({ lpTokenAddress, address }) => (
[...wipMergedPoolData, ...otherRegistryPoolsData].find(({ lpTokenAddress, address }) => (
(lpTokenAddress || address).toLowerCase() === metaPoolBasePoolLpToken.address.toLowerCase()
))
) : undefined
Expand Down Expand Up @@ -1241,6 +1268,10 @@ const getPools = async ({ blockchainId, registryId, preventQueryingFactoData })
const usesRateOracle = Number(poolInfo.oracleMethod) !== 0;
const ethereumLSDAPYs = (blockchainId === 'ethereum') ? (await getETHLSDAPYs()) : {};

if (isMetaPool && typeof underlyingPool === 'undefined') {
throw new Error(`Pool ${poolInfo.address} is a meta pool, yet we couldn’t retrieve its underlying pool. Please check METAPOOL_REGISTRIES_DEPENDENCIES, its base pool’s registry is likely missing.`)
}

const augmentedPool = {
...poolInfo,
poolUrls: detailedPoolUrls,
Expand Down
19 changes: 15 additions & 4 deletions utils/data/curve-prices.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import memoize from 'memoizee';
import Request from 'utils/Request';
import { arrayToHashmap } from 'utils/Array';
import { lc } from 'utils/String';
import { getNowTimestamp } from 'utils/Date';

const IGNORED_TOKEN_ADDRESSES = {
ethereum: [
Expand All @@ -25,10 +26,20 @@ const getCurvePrices = memoize(async (blockchainId) => {
const { data } = await (await Request.get(`https://prices.curve.fi/v1/usd_price/${blockchainId}`)).json();

return arrayToHashmap(
data.map(({ address, usd_price }) => [
lc(address),
usd_price,
]).filter(([lcAddress]) => !(IGNORED_TOKEN_ADDRESSES[blockchainId] || []).includes(lcAddress))
data
// Only use fresh prices (last updated in the past 7d, i.e. had a trade in the past 7d)
.filter(({ last_updated }) => (
// Append 'Z' because this is a UTC datetime string
(Date.parse(`${last_updated}Z`) / 1000) > (getNowTimestamp() - (7 * 86400))
))
.map(({
address,
usd_price,
}) => [
lc(address),
usd_price,
])
.filter(([lcAddress]) => !(IGNORED_TOKEN_ADDRESSES[blockchainId] || []).includes(lcAddress))
);
}, {
promise: true,
Expand Down

0 comments on commit 388fe9e

Please sign in to comment.