Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/adaptors/pareto-credit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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],
Expand Down
Loading