diff --git a/src/adaptors/pareto-credit/index.js b/src/adaptors/pareto-credit/index.js index 012106caee..d72a7cff83 100644 --- a/src/adaptors/pareto-credit/index.js +++ b/src/adaptors/pareto-credit/index.js @@ -12,16 +12,30 @@ const LEGACY_VAULTS = [ '0x433D5B175148dA32Ffe1e1A37a939E1b7e79be4d', ]; -const multiCall = async (abi, calls) => +const multiCall = async (abi, calls, block) => ( await sdk.api.abi.multiCall({ chain: CHAIN, abi, calls, + block, permitFailure: true, }) ).output.map((o) => o.output); +// Some legacy CDOs (e.g. Fasanara) return getApr() == 0 while their tranche +// price keeps rising. For those, fall back to the realised 30-day growth of +// virtualPrice. 30 days, not 7: tranche prices step once per epoch, so a 7-day +// window often lands between two steps and reads 0 as well. +const FALLBACK_DAYS = 30; + +const realisedApr = (priceNow, pricePrior) => { + if (!priceNow || !pricePrior || Number(pricePrior) <= 0) return null; + const growth = Number(priceNow) / Number(pricePrior) - 1; + if (!(growth > 0)) return null; + return growth * (365 / FALLBACK_DAYS) * 100; +}; + const vaultName = (trancheName, tokenSymbol) => { if (!trancheName) return undefined; let name = trancheName.split(' - ').pop(); @@ -70,6 +84,33 @@ const apy = async () => { multiCall('erc20:decimals', tokens.map((target) => ({ target }))), ]); + // Only fetch past prices for the vaults that need the fallback. + const zeroAprIdx = vaults + .map((_, i) => i) + .filter((i) => tranches[i] && aprs[i] != null && Number(aprs[i]) === 0); + const fallbackAprs = {}; + if (zeroAprIdx.length) { + // Optional: if the historical lookup fails, every pool still reports its + // current getApr() and the zero-APR vaults simply keep 0. + try { + const ts = Math.floor(Date.now() / 1000) - 86400 * FALLBACK_DAYS; + const { height: priorBlock } = await utils.getPriceApiData( + `/block/${CHAIN}/${ts}` + ); + const priorPrices = await multiCall( + 'function virtualPrice(address) view returns (uint256)', + zeroAprIdx.map((i) => ({ target: vaults[i], params: [tranches[i]] })), + priorBlock + ); + zeroAprIdx.forEach((i, j) => { + const apr = realisedApr(sharePrices[i], priorPrices[j]); + if (apr !== null) fallbackAprs[i] = apr; + }); + } catch (e) { + console.log('pareto-credit: 30d virtualPrice fallback skipped', e.message); + } + } + const { pricesByAddress: prices } = await utils.getPrices( [...new Set(tokens.filter(Boolean))], CHAIN @@ -89,7 +130,7 @@ const apy = async () => { symbol: symbols[i], poolMeta: vaultName(trancheNames[i], symbols[i]), tvlUsd, - apyBase: aprs[i] / 1e18, + apyBase: fallbackAprs[i] ?? aprs[i] / 1e18, token: tranches[i].toLowerCase(), underlyingTokens: [token], pricePerShare: sharePrices[i] / 10 ** decimals[i],