Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions types/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export type Metadata = {
sources: string[];
token: string;
events?: Event[];
unlockEvents?: UnlockEvent[];
notes?: string[];
protocolIds: string[];
incompleteSections?: IncompleteSection[];
Expand All @@ -161,6 +162,27 @@ export type Event = {
category: string;
unlockType: "cliff" | "linear";
};

export type AllocationDetail = {
recipient: string;
category: string;
unlockType: "cliff" | "linear_start" | "linear_rate_change";
amount?: number;
previousRatePerWeek?: number;
newRatePerWeek?: number;
}

export type UnlockEvent = {
timestamp: number;
cliffAllocations: AllocationDetail[];
linearAllocations: AllocationDetail[];
summary: {
totalTokensCliff?: number;
netChangeInWeeklyRate?: number;
totalNewWeeklyRate?: number;
}
}

export type TimeSeriesChainData = {
[block: string]: {
timestamp: number;
Expand Down
36 changes: 34 additions & 2 deletions utils/convertToRawData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export async function createRawSections(
};
let categories: { [category: string]: string[] } = {};

let allCliffAllocations: any[] = [];
let allLinearAllocations: any[] = [];

await Promise.all(
Object.entries(adapter).map(async (a: any[]) => {
if (a[0] == "meta") {
Expand Down Expand Up @@ -69,7 +72,9 @@ export async function createRawSections(
);
adapterResults = await Promise.all(adapterResults);

addResultToEvents(section, metadata, adapterResults, categories);
const { cliffAllocations, linearAllocations } = addResultToEvents(section, metadata, adapterResults, categories);
allCliffAllocations.push(...cliffAllocations);
allLinearAllocations.push(...linearAllocations);

const results: RawResult[] | RawResult[][] = adapterResults
.flat()
Expand Down Expand Up @@ -108,7 +113,34 @@ export async function createRawSections(
if (metadata && metadata.events)
metadata.events.sort((a: Event, b: Event) => a.timestamp - b.timestamp);



const unlockEventMap: { [timestamp: number]: { cliffAllocations: any[]; linearAllocations: any[] } } = {};
allCliffAllocations.forEach((a) => {
if (!unlockEventMap[a.timestamp]) unlockEventMap[a.timestamp] = { cliffAllocations: [], linearAllocations: [] };
unlockEventMap[a.timestamp].cliffAllocations.push(a);
});
allLinearAllocations.forEach((a) => {
if (!unlockEventMap[a.timestamp]) unlockEventMap[a.timestamp] = { cliffAllocations: [], linearAllocations: [] };
unlockEventMap[a.timestamp].linearAllocations.push(a);
});
metadata.unlockEvents = [];
for (const [timestampStr, { cliffAllocations, linearAllocations }] of Object.entries(unlockEventMap)) {
const timestamp = Number(timestampStr);
const totalTokensCliff = cliffAllocations.reduce((sum, a) => sum + (a.amount || 0), 0);
const netChangeInWeeklyRate = linearAllocations.reduce((sum, a) => sum + ((a.newRatePerWeek || 0) - (a.previousRatePerWeek || 0)), 0);
const totalNewWeeklyRate = linearAllocations.reduce((sum, a) => sum + (a.newRatePerWeek || 0), 0);
metadata.unlockEvents.push({
timestamp,
cliffAllocations,
linearAllocations,
summary: {
totalTokensCliff: totalTokensCliff > 0 ? totalTokensCliff : undefined,
netChangeInWeeklyRate: netChangeInWeeklyRate !== 0 ? netChangeInWeeklyRate : undefined,
totalNewWeeklyRate: totalNewWeeklyRate > 0 ? totalNewWeeklyRate : undefined,
}
});
}
metadata.unlockEvents.sort((a, b) => a.timestamp - b.timestamp);

return { rawSections, documented, startTime, endTime, metadata, categories };
}
Expand Down
67 changes: 63 additions & 4 deletions utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function addResultToEvents(
metadata: Metadata,
results: AdapterResult[],
categories: Categories,
): Metadata {
): { cliffAllocations: any[]; linearAllocations: any[] } {
const sectionToCategory: { [section: string]: string } = {};
for (const category in categories) {
categories[category].forEach((s) => {
Expand Down Expand Up @@ -90,8 +90,7 @@ export function addResultToEvents(
metadata.events.push({
description: `On {timestamp} {tokens[0]} of ${section} tokens ${
isFuture(s.start + i * s.stepDuration) ? "will be" : "were"
} unlocked
`,
} unlocked`,
timestamp: s.start + i * s.stepDuration,
noOfTokens: [s.amount],
category: sectionToCategory[section] || 'Uncategorized',
Expand All @@ -100,7 +99,67 @@ export function addResultToEvents(
}
});

return metadata;
const cliffAllocations: any[] = [];
const linearAllocations: any[] = [];

cliffs.forEach((c: CliffAdapterResult) => {
if (c.amount.toFixed(2) == "0.00") return;
cliffAllocations.push({
recipient: section,
category: sectionToCategory[section] || 'Uncategorized',
unlockType: "cliff",
amount: c.amount,
timestamp: c.start
});
});

steps.forEach((s: StepAdapterResult) => {
for (let i = 0; i < s.steps; i++) {
const ts = s.start + i * s.stepDuration;
cliffAllocations.push({
recipient: section,
category: sectionToCategory[section] || 'Uncategorized',
unlockType: "cliff",
amount: s.amount,
timestamp: ts
});
}
});

linears.forEach((l: LinearAdapterResult, i: number) => {

if (i === 0) {
const initialRate = ratePerPeriod(l, precision);
if (initialRate > 0) {
linearAllocations.push({
recipient: section,
category: sectionToCategory[section] || 'Uncategorized',
unlockType: "linear_start",
previousRatePerWeek: 0,
newRatePerWeek: initialRate,
timestamp: l.start
});
}
}

if (i < linears.length - 1) {
const l2 = linears[i + 1];
const thisRate = ratePerPeriod(l, precision);
const nextRate = ratePerPeriod(l2, precision);
if (Math.abs(Number(thisRate) - Number(nextRate)) / (thisRate || 1) >= 0.001) {
linearAllocations.push({
recipient: section,
category: sectionToCategory[section] || 'Uncategorized',
unlockType: "linear_rate_change",
previousRatePerWeek: thisRate,
newRatePerWeek: nextRate,
timestamp: l2.start
});
}
}
});

return { cliffAllocations, linearAllocations };
}
const filterResultsByType = (
results: AdapterResult[],
Expand Down
Loading