diff --git a/adapters/morpho.ts b/adapters/morpho.ts index 4029d9c..dd3be17 100644 --- a/adapters/morpho.ts +++ b/adapters/morpho.ts @@ -3,6 +3,12 @@ import { AdapterResult } from "../types/adapters"; import { manualCliff, manualLinear } from "./manual"; import { periodToSeconds, unixTimestampNow } from "../utils/time"; +interface AggregatedReward { + start: number; + end: number; + amount: number; +} + export default async function morpho(): Promise { const sections: AdapterResult[] = []; const now = unixTimestampNow(); @@ -10,28 +16,58 @@ export default async function morpho(): Promise { r.json(), ); - res.data.map( - ({ start, created_at, end, total_rewards, current_rates, type }: any) => { - if (!end && start > now) return; + const aggregatedRewards: Map = new Map(); + + res.data.forEach( + ({ + start, + created_at, + end, + total_rewards, + current_rates, + type, + asset, + }: any) => { + const effectiveStart = start ?? created_at; + const effectiveEnd = end ?? now; + + if (!end && effectiveStart > now) return; - if (type == "airdrop-reward") { + if (type === "airdrop-reward") { sections.push( - manualCliff(start ?? created_at, Number(total_rewards / 1e18)), + manualCliff(effectiveStart, Number(total_rewards) / 1e18), ); return; } const amount = end ? Number(total_rewards) - : estimateAmount(start, end ?? now, current_rates); + : estimateAmount(effectiveStart, effectiveEnd, current_rates); + if (!amount || amount < 0) return; - sections.push(manualLinear(start, end ?? now, amount / 1e18)); + const assetId = asset?.id ?? "unknown_asset"; + const key = `${effectiveStart}-${effectiveEnd}-${assetId}`; + + const existing = aggregatedRewards.get(key); + if (existing) { + existing.amount += amount; + } else { + aggregatedRewards.set(key, { + start: effectiveStart, + end: effectiveEnd, + amount: amount, + }); + } }, ); - function estimateAmount(start: number, end: number, rates: any[]) { - if (!rates.length) return 0; + aggregatedRewards.forEach((reward) => { + sections.push(manualLinear(reward.start, reward.end, reward.amount / 1e18)); + }); + + function estimateAmount(start: number, end: number, rates: any[] | null) { + if (!rates || !rates.length) return 0; const aggregateRate = rates.reduce( (p: number, c: any) => p + Number(c.per_dollar_per_year), 0,