Skip to content

Commit 0df67a0

Browse files
committed
create unlockEvents, goal is to deprecate events later on
1 parent c5775d9 commit 0df67a0

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

types/adapters.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export type Metadata = {
143143
sources: string[];
144144
token: string;
145145
events?: Event[];
146+
unlockEvents?: UnlockEvent[];
146147
notes?: string[];
147148
protocolIds: string[];
148149
incompleteSections?: IncompleteSection[];
@@ -161,6 +162,27 @@ export type Event = {
161162
category: string;
162163
unlockType: "cliff" | "linear";
163164
};
165+
166+
export type AllocationDetail = {
167+
recipient: string;
168+
category: string;
169+
unlockType: "cliff" | "linear_start" | "linear_rate_change";
170+
amount?: number;
171+
previousRatePerWeek?: number;
172+
newRatePerWeek?: number;
173+
}
174+
175+
export type UnlockEvent = {
176+
timestamp: number;
177+
cliffAllocations: AllocationDetail[];
178+
linearAllocations: AllocationDetail[];
179+
summary: {
180+
totalTokensCliff?: number;
181+
netChangeInWeeklyRate?: number;
182+
totalNewWeeklyRate?: number;
183+
}
184+
}
185+
164186
export type TimeSeriesChainData = {
165187
[block: string]: {
166188
timestamp: number;

utils/convertToRawData.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export async function createRawSections(
2929
};
3030
let categories: { [category: string]: string[] } = {};
3131

32+
let allCliffAllocations: any[] = [];
33+
let allLinearAllocations: any[] = [];
34+
3235
await Promise.all(
3336
Object.entries(adapter).map(async (a: any[]) => {
3437
if (a[0] == "meta") {
@@ -69,7 +72,9 @@ export async function createRawSections(
6972
);
7073
adapterResults = await Promise.all(adapterResults);
7174

72-
addResultToEvents(section, metadata, adapterResults, categories);
75+
const { cliffAllocations, linearAllocations } = addResultToEvents(section, metadata, adapterResults, categories);
76+
allCliffAllocations.push(...cliffAllocations);
77+
allLinearAllocations.push(...linearAllocations);
7378

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

111-
116+
117+
const unlockEventMap: { [timestamp: number]: { cliffAllocations: any[]; linearAllocations: any[] } } = {};
118+
allCliffAllocations.forEach((a) => {
119+
if (!unlockEventMap[a.timestamp]) unlockEventMap[a.timestamp] = { cliffAllocations: [], linearAllocations: [] };
120+
unlockEventMap[a.timestamp].cliffAllocations.push(a);
121+
});
122+
allLinearAllocations.forEach((a) => {
123+
if (!unlockEventMap[a.timestamp]) unlockEventMap[a.timestamp] = { cliffAllocations: [], linearAllocations: [] };
124+
unlockEventMap[a.timestamp].linearAllocations.push(a);
125+
});
126+
metadata.unlockEvents = [];
127+
for (const [timestampStr, { cliffAllocations, linearAllocations }] of Object.entries(unlockEventMap)) {
128+
const timestamp = Number(timestampStr);
129+
const totalTokensCliff = cliffAllocations.reduce((sum, a) => sum + (a.amount || 0), 0);
130+
const netChangeInWeeklyRate = linearAllocations.reduce((sum, a) => sum + ((a.newRatePerWeek || 0) - (a.previousRatePerWeek || 0)), 0);
131+
const totalNewWeeklyRate = linearAllocations.reduce((sum, a) => sum + (a.newRatePerWeek || 0), 0);
132+
metadata.unlockEvents.push({
133+
timestamp,
134+
cliffAllocations,
135+
linearAllocations,
136+
summary: {
137+
totalTokensCliff: totalTokensCliff > 0 ? totalTokensCliff : undefined,
138+
netChangeInWeeklyRate: netChangeInWeeklyRate !== 0 ? netChangeInWeeklyRate : undefined,
139+
totalNewWeeklyRate: totalNewWeeklyRate > 0 ? totalNewWeeklyRate : undefined,
140+
}
141+
});
142+
}
143+
metadata.unlockEvents.sort((a, b) => a.timestamp - b.timestamp);
112144

113145
return { rawSections, documented, startTime, endTime, metadata, categories };
114146
}

utils/events.ts

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function addResultToEvents(
1515
metadata: Metadata,
1616
results: AdapterResult[],
1717
categories: Categories,
18-
): Metadata {
18+
): { cliffAllocations: any[]; linearAllocations: any[] } {
1919
const sectionToCategory: { [section: string]: string } = {};
2020
for (const category in categories) {
2121
categories[category].forEach((s) => {
@@ -90,8 +90,7 @@ export function addResultToEvents(
9090
metadata.events.push({
9191
description: `On {timestamp} {tokens[0]} of ${section} tokens ${
9292
isFuture(s.start + i * s.stepDuration) ? "will be" : "were"
93-
} unlocked
94-
`,
93+
} unlocked`,
9594
timestamp: s.start + i * s.stepDuration,
9695
noOfTokens: [s.amount],
9796
category: sectionToCategory[section] || 'Uncategorized',
@@ -100,7 +99,67 @@ export function addResultToEvents(
10099
}
101100
});
102101

103-
return metadata;
102+
const cliffAllocations: any[] = [];
103+
const linearAllocations: any[] = [];
104+
105+
cliffs.forEach((c: CliffAdapterResult) => {
106+
if (c.amount.toFixed(2) == "0.00") return;
107+
cliffAllocations.push({
108+
recipient: section,
109+
category: sectionToCategory[section] || 'Uncategorized',
110+
unlockType: "cliff",
111+
amount: c.amount,
112+
timestamp: c.start
113+
});
114+
});
115+
116+
steps.forEach((s: StepAdapterResult) => {
117+
for (let i = 0; i < s.steps; i++) {
118+
const ts = s.start + i * s.stepDuration;
119+
cliffAllocations.push({
120+
recipient: section,
121+
category: sectionToCategory[section] || 'Uncategorized',
122+
unlockType: "cliff",
123+
amount: s.amount,
124+
timestamp: ts
125+
});
126+
}
127+
});
128+
129+
linears.forEach((l: LinearAdapterResult, i: number) => {
130+
131+
if (i === 0) {
132+
const initialRate = ratePerPeriod(l, precision);
133+
if (initialRate > 0) {
134+
linearAllocations.push({
135+
recipient: section,
136+
category: sectionToCategory[section] || 'Uncategorized',
137+
unlockType: "linear_start",
138+
previousRatePerWeek: 0,
139+
newRatePerWeek: initialRate,
140+
timestamp: l.start
141+
});
142+
}
143+
}
144+
145+
if (i < linears.length - 1) {
146+
const l2 = linears[i + 1];
147+
const thisRate = ratePerPeriod(l, precision);
148+
const nextRate = ratePerPeriod(l2, precision);
149+
if (Math.abs(Number(thisRate) - Number(nextRate)) / (thisRate || 1) >= 0.001) {
150+
linearAllocations.push({
151+
recipient: section,
152+
category: sectionToCategory[section] || 'Uncategorized',
153+
unlockType: "linear_rate_change",
154+
previousRatePerWeek: thisRate,
155+
newRatePerWeek: nextRate,
156+
timestamp: l2.start
157+
});
158+
}
159+
}
160+
});
161+
162+
return { cliffAllocations, linearAllocations };
104163
}
105164
const filterResultsByType = (
106165
results: AdapterResult[],

0 commit comments

Comments
 (0)