@alphafi/alphafi-sdk 0.0.27 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"fetchAutoCompoundingEvents.d.ts","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAwB,MAAM,uBAAuB,CAAC;AAEvE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,EAEjC,MAAM,YAAY,CAAC;AAEpB,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,gCAAgC,GACvC,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAkHrC;AAED,wBAAsB,uBAAuB,CAC3C,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,CA2IjB;AA6CD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAWnC"}
1
+ {"version":3,"file":"fetchAutoCompoundingEvents.d.ts","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAwB,MAAM,uBAAuB,CAAC;AAEvE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,EAEjC,MAAM,YAAY,CAAC;AAEpB,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,gCAAgC,GACvC,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAkHrC;AAED,wBAAsB,uBAAuB,CAC3C,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,CAoJjB;AA6CD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAWnC"}
@@ -113,130 +113,138 @@ async function fetchAutoCompoundingEvents(params) {
113
113
  return autoCompoundingEvents;
114
114
  }
115
115
  async function calculateAprForInvestor(events) {
116
- // Sort events by timestamp to process them in order
117
- events.sort((a, b) => a.timestamp - b.timestamp);
118
116
  let totalGrowth = 0;
119
117
  let totalTimeSpan = 0;
120
- let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event
121
- let previousGrowthRate = 0;
122
- let previousTimeDiff = 0;
123
- let prevCompoundA = 0n;
124
- let prevCompoundB = 0n;
125
- const investorPoolMap = await (0, maps_js_1.getInvestorPoolMap)();
126
- // const matchInvestor =
127
- // "0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43";
128
- // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {
129
- // console.log(
130
- // "Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)",
131
- // );
132
- // }
133
- for (const event of events) {
134
- // Calculate the time difference from the previous event
135
- let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);
136
- // Calculate growth rate
137
- let growthRate = 0;
138
- if ("total_amount_a" in event && "total_amount_b" in event) {
139
- let growthA = 0;
140
- let growthB = 0;
141
- if (Number(event.total_amount_a) === 0) {
142
- prevCompoundA += event.compound_amount_a;
143
- }
144
- else {
145
- prevCompoundA = 0n;
146
- }
147
- if (prevCompoundA > 0n) {
148
- growthA =
149
- Number(event.total_amount_a) === 0
150
- ? 0
151
- : Number(event.compound_amount_a + prevCompoundA) /
152
- Number(event.total_amount_a - prevCompoundA);
153
- }
154
- else {
155
- growthA =
156
- Number(event.total_amount_a) === 0
157
- ? 0
158
- : Number(event.compound_amount_a) / Number(event.total_amount_a);
159
- }
160
- if (Number(event.total_amount_b) === 0) {
161
- prevCompoundB += event.compound_amount_b;
118
+ try {
119
+ // Sort events by timestamp to process them in order
120
+ events.sort((a, b) => a.timestamp - b.timestamp);
121
+ let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event
122
+ let previousGrowthRate = 0;
123
+ let previousTimeDiff = 0;
124
+ let prevCompoundA = 0n;
125
+ let prevCompoundB = 0n;
126
+ const investorPoolMap = await (0, maps_js_1.getInvestorPoolMap)();
127
+ // const matchInvestor =
128
+ // "0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43";
129
+ // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {
130
+ // console.log(
131
+ // "Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)",
132
+ // );
133
+ // }
134
+ for (const event of events) {
135
+ // Calculate the time difference from the previous event
136
+ let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);
137
+ // Calculate growth rate
138
+ let growthRate = 0;
139
+ if ("total_amount_a" in event && "total_amount_b" in event) {
140
+ let growthA = 0;
141
+ let growthB = 0;
142
+ if (Number(event.total_amount_a) === 0) {
143
+ prevCompoundA += event.compound_amount_a;
144
+ }
145
+ else {
146
+ prevCompoundA = 0n;
147
+ }
148
+ if (prevCompoundA > 0n) {
149
+ growthA =
150
+ Number(event.total_amount_a) === 0
151
+ ? 0
152
+ : Number(event.compound_amount_a + prevCompoundA) /
153
+ Number(event.total_amount_a - prevCompoundA);
154
+ }
155
+ else {
156
+ growthA =
157
+ Number(event.total_amount_a) === 0
158
+ ? 0
159
+ : Number(event.compound_amount_a) / Number(event.total_amount_a);
160
+ }
161
+ if (Number(event.total_amount_b) === 0) {
162
+ prevCompoundB += event.compound_amount_b;
163
+ }
164
+ else {
165
+ prevCompoundB = 0n;
166
+ }
167
+ if (prevCompoundB > 0n) {
168
+ growthB =
169
+ Number(event.total_amount_b) == 0
170
+ ? 0
171
+ : Number(event.compound_amount_b + prevCompoundB) /
172
+ Number(event.total_amount_b - prevCompoundB);
173
+ }
174
+ else {
175
+ growthB =
176
+ Number(event.total_amount_b) == 0
177
+ ? 0
178
+ : Number(event.compound_amount_b) / Number(event.total_amount_b);
179
+ }
180
+ // if (
181
+ // event.investor_id ===
182
+ // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
183
+ // ) {
184
+ // console.log(
185
+ // timeDiff / (1000 * 60),
186
+ // event.timestamp,
187
+ // event.timestamp - previousTimestamp,
188
+ // );
189
+ // }
190
+ // if (
191
+ // event.investor_id ===
192
+ // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
193
+ // ) {
194
+ // console.log(
195
+ // event.compound_amount_a,
196
+ // event.total_amount_a,
197
+ // growthA,
198
+ // event.compound_amount_b,
199
+ // event.total_amount_b,
200
+ // growthB,
201
+ // );
202
+ // }
203
+ growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B
204
+ // if (event.investor_id === matchInvestor) {
205
+ // console.log(
206
+ // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,
207
+ // );
208
+ // }
209
+ if (Math.abs(growthA - growthB) > 0.5) {
210
+ // skip row, fill with previous event
211
+ growthRate = previousGrowthRate;
212
+ timeDiff = previousTimeDiff;
213
+ }
214
+ if (growthRate > 0.005) {
215
+ growthRate = 0;
216
+ }
162
217
  }
163
- else {
164
- prevCompoundB = 0n;
165
- }
166
- if (prevCompoundB > 0n) {
167
- growthB =
168
- Number(event.total_amount_b) == 0
169
- ? 0
170
- : Number(event.compound_amount_b + prevCompoundB) /
171
- Number(event.total_amount_b - prevCompoundB);
172
- }
173
- else {
174
- growthB =
175
- Number(event.total_amount_b) == 0
176
- ? 0
177
- : Number(event.compound_amount_b) / Number(event.total_amount_b);
178
- }
179
- // if (
180
- // event.investor_id ===
181
- // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
182
- // ) {
183
- // console.log(
184
- // timeDiff / (1000 * 60),
185
- // event.timestamp,
186
- // event.timestamp - previousTimestamp,
187
- // );
188
- // }
189
- // if (
190
- // event.investor_id ===
191
- // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
192
- // ) {
193
- // console.log(
194
- // event.compound_amount_a,
195
- // event.total_amount_a,
196
- // growthA,
197
- // event.compound_amount_b,
198
- // event.total_amount_b,
199
- // growthB,
200
- // );
201
- // }
202
- growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B
203
- // if (event.investor_id === matchInvestor) {
204
- // console.log(
205
- // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,
206
- // );
207
- // }
208
- if (Math.abs(growthA - growthB) > 0.5) {
209
- // skip row, fill with previous event
210
- growthRate = previousGrowthRate;
211
- timeDiff = previousTimeDiff;
212
- }
213
- if (growthRate > 0.005) {
214
- growthRate = 0;
218
+ else if ("total_amount" in event) {
219
+ let compoundAmount = Number(event.compound_amount);
220
+ let totalAmount = Number(event.total_amount);
221
+ if ("cur_total_debt" in event && "accrued_interest" in event) {
222
+ compoundAmount = Number(event.compound_amount - event.accrued_interest);
223
+ totalAmount = Number(event.total_amount - event.cur_total_debt);
224
+ }
225
+ // NaviAutoCompoundingEvent
226
+ growthRate = isNaN(compoundAmount / totalAmount)
227
+ ? 0
228
+ : compoundAmount / totalAmount;
229
+ const poolName = investorPoolMap.get(event.investor_id);
230
+ const coinName = maps_js_1.poolCoinMap[poolName];
231
+ growthRate = growthRate * Math.pow(10, 9 - coins_js_1.coins[coinName].expo);
215
232
  }
233
+ // Accumulate the time-weighted growth
234
+ totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;
235
+ previousGrowthRate = growthRate;
236
+ // Accumulate the total time span
237
+ totalTimeSpan += timeDiff;
238
+ previousTimeDiff = timeDiff;
239
+ // Update the previous timestamp to the current event's timestamp
240
+ previousTimestamp = event.timestamp;
216
241
  }
217
- else if ("total_amount" in event) {
218
- let compoundAmount = Number(event.compound_amount);
219
- let totalAmount = Number(event.total_amount);
220
- if ("cur_total_debt" in event && "accrued_interest" in event) {
221
- compoundAmount = Number(event.compound_amount - event.accrued_interest);
222
- totalAmount = Number(event.total_amount - event.cur_total_debt);
223
- }
224
- // NaviAutoCompoundingEvent
225
- growthRate = isNaN(compoundAmount / totalAmount)
226
- ? 0
227
- : compoundAmount / totalAmount;
228
- const poolName = investorPoolMap.get(event.investor_id);
229
- const coinName = maps_js_1.poolCoinMap[poolName];
230
- growthRate = growthRate * Math.pow(10, 9 - coins_js_1.coins[coinName].expo);
242
+ }
243
+ catch (error) {
244
+ console.error("Error calculating apr from events.");
245
+ if (events.length > 0) {
246
+ console.error("Investor-ID: ", events[0].investor_id);
231
247
  }
232
- // Accumulate the time-weighted growth
233
- totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;
234
- previousGrowthRate = growthRate;
235
- // Accumulate the total time span
236
- totalTimeSpan += timeDiff;
237
- previousTimeDiff = timeDiff;
238
- // Update the previous timestamp to the current event's timestamp
239
- previousTimestamp = event.timestamp;
240
248
  }
241
249
  const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;
242
250
  return apr;
@@ -1 +1 @@
1
- {"version":3,"file":"fetchAutoCompoundingEvents.js","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":";;AAcA,gEAoHC;AAED,0DA6IC;AA6CD,oDAaC;AA3UD,oDAA8C;AAC9C,kDAI8B;AAE9B,qDAA+C;AAOxC,KAAK,UAAU,0BAA0B,CAC9C,MAAwC;IAExC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IACxC,oDAAoD;IACpD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,MAAM,CAAC,SAAS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EAAE,CAAC;QACxC,MAAM,iBAAiB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QACrD,gGAAgG;QAChG,WAAW,CAAC,IAAI,CACd,uBAAuB,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACxD,UAAU;YACV,GAAG;SACJ,CAAC,CAAC,CACJ,CAAC;IACJ,CAAC;IAED,6DAA6D;IAC7D,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAE5C,wEAAwE;IACxE,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CACxB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;QACd,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;QACpC,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAA4B,CAC7B,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAEM,KAAK,UAAU,oBAAoB,CACxC,MAAkC;IAElC,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,mBAAmB,GAAG,MAAM,IAAA,4BAAkB,GAAE,CAAC;IACvD,MAAM,cAAc,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;IAC9D,KAAK,MAAM,UAAU,IAAI,cAAc,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACrD,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { coins } from \"../../common/coins.js\";\nimport {\n getInvestorPoolMap,\n poolCoinMap,\n poolInfo,\n} from \"../../common/maps.js\";\nimport { PoolName, SingleAssetPoolNames } from \"../../common/types.js\";\nimport { fetchEvents } from \"./fetchEvents.js\";\nimport {\n AutoCompoundingEventNode,\n FetchAutoCompoundingEventsParams,\n //RebalanceEventNode,\n} from \"./types.js\";\n\nexport async function fetchAutoCompoundingEvents(\n params: FetchAutoCompoundingEventsParams,\n): Promise<AutoCompoundingEventNode[]> {\n const eventTypesSet = new Set<string>();\n // const rebalanceEventTypesSet = new Set<string>();\n if (params.poolNames) {\n params.poolNames.forEach((poolName) => {\n const eventType = poolInfo[poolName].autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n } else {\n // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n Object.values(poolInfo).forEach((info) => {\n const eventType = info.autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n }\n // for rebalance\n // if (params.poolNames) {\n // params.poolNames.forEach((poolName) => {\n // const eventType = poolInfo[poolName].rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // } else {\n // // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n // Object.values(poolInfo).forEach((info) => {\n // const eventType = info.rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // }\n\n const eventTypes = Array.from(eventTypesSet);\n // const rebalanceEventTypes = Array.from(rebalanceEventTypesSet);\n\n const eventsPromises = eventTypes.map(async (eventType) => {\n const events = (\n await fetchEvents({\n startTime: params.startTime,\n endTime: params.endTime,\n eventTypes: [eventType],\n })\n ).filter((e) => {\n if (params.poolNames) {\n return params.poolNames\n .map((poolName) => poolInfo[poolName].investorId)\n .includes((e as AutoCompoundingEventNode).investor_id);\n } else {\n return true;\n }\n });\n return events;\n });\n // const rebalanceEventsPromises = rebalanceEventTypes.map(async (eventType) => {\n // const events = (\n // await fetchEvents({\n // startTime: params.startTime,\n // endTime: params.endTime,\n // eventTypes: [eventType],\n // })\n // ).filter((e) => {\n // if (params.poolNames) {\n // return params.poolNames\n // .map((poolName) => poolInfo[poolName].investorId)\n // .includes((e as AutoCompoundingEventNode).investor_id);\n // } else {\n // return true;\n // }\n // });\n // return events;\n // });\n\n const events = (await Promise.all(eventsPromises)).flat();\n\n const autoCompoundingEvents = events.map((e) => {\n return e as AutoCompoundingEventNode;\n });\n\n // const rebalanceEvents = (await Promise.all(rebalanceEventsPromises)).flat();\n\n // const rebalancingEvents = rebalanceEvents.map((e) => {\n // return e as RebalanceEventNode;\n // });\n\n // const investorPoolMap = await getInvestorPoolMap();\n\n // for (const x of autoCompoundingEvents) {\n // if (\n // investorPoolMap.get(x.investor_id) &&\n // poolInfo[investorPoolMap.get(x.investor_id)!].parentProtocolName ==\n // \"CETUS\"\n // ) {\n // if (\n // \"total_amount_a\" in x &&\n // x.total_amount_a == 0n &&\n // x.total_amount_b == 0n\n // ) {\n // for (const y of rebalancingEvents) {\n // if (y.investor_id == x.investor_id && y.timestamp == x.timestamp) {\n // // works if its guaranteed that for one timestamp there is only one rebalance for one pool\n // x.total_amount_a = BigInt(y.amount_a_before);\n // x.total_amount_b = BigInt(y.amount_b_before);\n // }\n // }\n // }\n // }\n // }\n\n return autoCompoundingEvents;\n}\n\nexport async function calculateAprForInvestor(\n events: AutoCompoundingEventNode[],\n): Promise<number> {\n // Sort events by timestamp to process them in order\n events.sort((a, b) => a.timestamp - b.timestamp);\n\n let totalGrowth = 0;\n let totalTimeSpan = 0;\n let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event\n let previousGrowthRate = 0;\n let previousTimeDiff = 0;\n let prevCompoundA = 0n;\n let prevCompoundB = 0n;\n\n const investorPoolMap = await getInvestorPoolMap();\n\n // const matchInvestor =\n // \"0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43\";\n // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {\n // console.log(\n // \"Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)\",\n // );\n // }\n for (const event of events) {\n // Calculate the time difference from the previous event\n let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);\n\n // Calculate growth rate\n let growthRate = 0;\n if (\"total_amount_a\" in event && \"total_amount_b\" in event) {\n let growthA = 0;\n let growthB = 0;\n\n if (Number(event.total_amount_a) === 0) {\n prevCompoundA += event.compound_amount_a;\n } else {\n prevCompoundA = 0n;\n }\n if (prevCompoundA > 0n) {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a + prevCompoundA) /\n Number(event.total_amount_a - prevCompoundA);\n } else {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a) / Number(event.total_amount_a);\n }\n\n if (Number(event.total_amount_b) === 0) {\n prevCompoundB += event.compound_amount_b;\n } else {\n prevCompoundB = 0n;\n }\n\n if (prevCompoundB > 0n) {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b + prevCompoundB) /\n Number(event.total_amount_b - prevCompoundB);\n } else {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b) / Number(event.total_amount_b);\n }\n\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // timeDiff / (1000 * 60),\n // event.timestamp,\n // event.timestamp - previousTimestamp,\n // );\n // }\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // event.compound_amount_a,\n // event.total_amount_a,\n // growthA,\n // event.compound_amount_b,\n // event.total_amount_b,\n // growthB,\n // );\n // }\n growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B\n // if (event.investor_id === matchInvestor) {\n // console.log(\n // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,\n // );\n // }\n if (Math.abs(growthA - growthB) > 0.5) {\n // skip row, fill with previous event\n growthRate = previousGrowthRate;\n timeDiff = previousTimeDiff;\n }\n if (growthRate > 0.005) {\n growthRate = 0;\n }\n } else if (\"total_amount\" in event) {\n let compoundAmount: number = Number(event.compound_amount);\n let totalAmount: number = Number(event.total_amount);\n if (\"cur_total_debt\" in event && \"accrued_interest\" in event) {\n compoundAmount = Number(event.compound_amount - event.accrued_interest);\n totalAmount = Number(event.total_amount - event.cur_total_debt);\n }\n // NaviAutoCompoundingEvent\n growthRate = isNaN(compoundAmount / totalAmount)\n ? 0\n : compoundAmount / totalAmount;\n const poolName = investorPoolMap.get(\n event.investor_id,\n ) as SingleAssetPoolNames;\n const coinName = poolCoinMap[poolName];\n\n growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);\n }\n\n // Accumulate the time-weighted growth\n totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;\n previousGrowthRate = growthRate;\n\n // Accumulate the total time span\n totalTimeSpan += timeDiff;\n previousTimeDiff = timeDiff;\n\n // Update the previous timestamp to the current event's timestamp\n previousTimestamp = event.timestamp;\n }\n\n const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;\n\n return apr;\n}\n\nasync function calculateAprForInvestors(\n events: AutoCompoundingEventNode[],\n): Promise<Record<string, number>> {\n const investorEvents: Record<string, AutoCompoundingEventNode[]> = {};\n\n // Step 1: Segregate events by investor_id\n for (const event of events) {\n const investorId = event.investor_id;\n if (!investorEvents[investorId]) {\n investorEvents[investorId] = [];\n }\n investorEvents[investorId].push(event);\n }\n\n // Step 2: Calculate APR for each event and aggregate for each investor\n const aprPromises: Promise<{ investorId: string; apr: number }>[] = [];\n\n for (const investorId in investorEvents) {\n const investorEventList = investorEvents[investorId];\n // Push a promise that resolves to an object containing the investorId and the corresponding APR\n aprPromises.push(\n calculateAprForInvestor(investorEventList).then((apr) => ({\n investorId,\n apr,\n })),\n );\n }\n\n // Resolve all promises and map them to the corresponding APR\n const aprs = await Promise.all(aprPromises);\n\n // Convert the array of results into an object mapping investorId to APR\n const aprMap = aprs.reduce(\n (map, result) => {\n map[result.investorId] = result.apr;\n return map;\n },\n {} as Record<string, number>,\n );\n\n return aprMap;\n}\n\nexport async function calculateAprForPools(\n events: AutoCompoundingEventNode[],\n): Promise<Record<PoolName, number>> {\n const aprMap: Record<string, number> = {};\n const investorPoolNameMap = await getInvestorPoolMap();\n const investorAprMap = await calculateAprForInvestors(events);\n for (const investorId in investorAprMap) {\n const poolName = investorPoolNameMap.get(investorId);\n if (poolName) {\n aprMap[poolName] = investorAprMap[investorId];\n }\n }\n return aprMap;\n}\n"]}
1
+ {"version":3,"file":"fetchAutoCompoundingEvents.js","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":";;AAcA,gEAoHC;AAED,0DAsJC;AA6CD,oDAaC;AApVD,oDAA8C;AAC9C,kDAI8B;AAE9B,qDAA+C;AAOxC,KAAK,UAAU,0BAA0B,CAC9C,MAAwC;IAExC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IACxC,oDAAoD;IACpD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACpC,MAAM,SAAS,GAAG,kBAAQ,CAAC,QAAQ,CAAC,CAAC,wBAAwB,CAAC;YAC9D,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;gBACtE,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,C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E,KAAK,MAAM,UAAU,IAAI,cAAc,EAAE,CAAC;QACxC,MAAM,iBAAiB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QACrD,gGAAgG;QAChG,WAAW,CAAC,IAAI,CACd,uBAAuB,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;YACxD,UAAU;YACV,GAAG;SACJ,CAAC,CAAC,CACJ,CAAC;IACJ,CAAC;IAED,6DAA6D;IAC7D,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAE5C,wEAAwE;IACxE,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CACxB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;QACd,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;QACpC,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAA4B,CAC7B,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAEM,KAAK,UAAU,oBAAoB,CACxC,MAAkC;IAElC,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,mBAAmB,GAAG,MAAM,IAAA,4BAAkB,GAAE,CAAC;IACvD,MAAM,cAAc,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;IAC9D,KAAK,MAAM,UAAU,IAAI,cAAc,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACrD,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { coins } from \"../../common/coins.js\";\nimport {\n getInvestorPoolMap,\n poolCoinMap,\n poolInfo,\n} from \"../../common/maps.js\";\nimport { PoolName, SingleAssetPoolNames } from \"../../common/types.js\";\nimport { fetchEvents } from \"./fetchEvents.js\";\nimport {\n AutoCompoundingEventNode,\n FetchAutoCompoundingEventsParams,\n //RebalanceEventNode,\n} from \"./types.js\";\n\nexport async function fetchAutoCompoundingEvents(\n params: FetchAutoCompoundingEventsParams,\n): Promise<AutoCompoundingEventNode[]> {\n const eventTypesSet = new Set<string>();\n // const rebalanceEventTypesSet = new Set<string>();\n if (params.poolNames) {\n params.poolNames.forEach((poolName) => {\n const eventType = poolInfo[poolName].autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n } else {\n // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n Object.values(poolInfo).forEach((info) => {\n const eventType = info.autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n }\n // for rebalance\n // if (params.poolNames) {\n // params.poolNames.forEach((poolName) => {\n // const eventType = poolInfo[poolName].rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // } else {\n // // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n // Object.values(poolInfo).forEach((info) => {\n // const eventType = info.rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // }\n\n const eventTypes = Array.from(eventTypesSet);\n // const rebalanceEventTypes = Array.from(rebalanceEventTypesSet);\n\n const eventsPromises = eventTypes.map(async (eventType) => {\n const events = (\n await fetchEvents({\n startTime: params.startTime,\n endTime: params.endTime,\n eventTypes: [eventType],\n })\n ).filter((e) => {\n if (params.poolNames) {\n return params.poolNames\n .map((poolName) => poolInfo[poolName].investorId)\n .includes((e as AutoCompoundingEventNode).investor_id);\n } else {\n return true;\n }\n });\n return events;\n });\n // const rebalanceEventsPromises = rebalanceEventTypes.map(async (eventType) => {\n // const events = (\n // await fetchEvents({\n // startTime: params.startTime,\n // endTime: params.endTime,\n // eventTypes: [eventType],\n // })\n // ).filter((e) => {\n // if (params.poolNames) {\n // return params.poolNames\n // .map((poolName) => poolInfo[poolName].investorId)\n // .includes((e as AutoCompoundingEventNode).investor_id);\n // } else {\n // return true;\n // }\n // });\n // return events;\n // });\n\n const events = (await Promise.all(eventsPromises)).flat();\n\n const autoCompoundingEvents = events.map((e) => {\n return e as AutoCompoundingEventNode;\n });\n\n // const rebalanceEvents = (await Promise.all(rebalanceEventsPromises)).flat();\n\n // const rebalancingEvents = rebalanceEvents.map((e) => {\n // return e as RebalanceEventNode;\n // });\n\n // const investorPoolMap = await getInvestorPoolMap();\n\n // for (const x of autoCompoundingEvents) {\n // if (\n // investorPoolMap.get(x.investor_id) &&\n // poolInfo[investorPoolMap.get(x.investor_id)!].parentProtocolName ==\n // \"CETUS\"\n // ) {\n // if (\n // \"total_amount_a\" in x &&\n // x.total_amount_a == 0n &&\n // x.total_amount_b == 0n\n // ) {\n // for (const y of rebalancingEvents) {\n // if (y.investor_id == x.investor_id && y.timestamp == x.timestamp) {\n // // works if its guaranteed that for one timestamp there is only one rebalance for one pool\n // x.total_amount_a = BigInt(y.amount_a_before);\n // x.total_amount_b = BigInt(y.amount_b_before);\n // }\n // }\n // }\n // }\n // }\n\n return autoCompoundingEvents;\n}\n\nexport async function calculateAprForInvestor(\n events: AutoCompoundingEventNode[],\n): Promise<number> {\n let totalGrowth = 0;\n let totalTimeSpan = 0;\n\n try {\n // Sort events by timestamp to process them in order\n events.sort((a, b) => a.timestamp - b.timestamp);\n\n let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event\n let previousGrowthRate = 0;\n let previousTimeDiff = 0;\n let prevCompoundA = 0n;\n let prevCompoundB = 0n;\n\n const investorPoolMap = await getInvestorPoolMap();\n\n // const matchInvestor =\n // \"0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43\";\n // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {\n // console.log(\n // \"Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)\",\n // );\n // }\n for (const event of events) {\n // Calculate the time difference from the previous event\n let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);\n\n // Calculate growth rate\n let growthRate = 0;\n if (\"total_amount_a\" in event && \"total_amount_b\" in event) {\n let growthA = 0;\n let growthB = 0;\n\n if (Number(event.total_amount_a) === 0) {\n prevCompoundA += event.compound_amount_a;\n } else {\n prevCompoundA = 0n;\n }\n if (prevCompoundA > 0n) {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a + prevCompoundA) /\n Number(event.total_amount_a - prevCompoundA);\n } else {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a) / Number(event.total_amount_a);\n }\n\n if (Number(event.total_amount_b) === 0) {\n prevCompoundB += event.compound_amount_b;\n } else {\n prevCompoundB = 0n;\n }\n\n if (prevCompoundB > 0n) {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b + prevCompoundB) /\n Number(event.total_amount_b - prevCompoundB);\n } else {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b) / Number(event.total_amount_b);\n }\n\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // timeDiff / (1000 * 60),\n // event.timestamp,\n // event.timestamp - previousTimestamp,\n // );\n // }\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // event.compound_amount_a,\n // event.total_amount_a,\n // growthA,\n // event.compound_amount_b,\n // event.total_amount_b,\n // growthB,\n // );\n // }\n growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B\n // if (event.investor_id === matchInvestor) {\n // console.log(\n // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,\n // );\n // }\n if (Math.abs(growthA - growthB) > 0.5) {\n // skip row, fill with previous event\n growthRate = previousGrowthRate;\n timeDiff = previousTimeDiff;\n }\n if (growthRate > 0.005) {\n growthRate = 0;\n }\n } else if (\"total_amount\" in event) {\n let compoundAmount: number = Number(event.compound_amount);\n let totalAmount: number = Number(event.total_amount);\n if (\"cur_total_debt\" in event && \"accrued_interest\" in event) {\n compoundAmount = Number(\n event.compound_amount - event.accrued_interest,\n );\n totalAmount = Number(event.total_amount - event.cur_total_debt);\n }\n // NaviAutoCompoundingEvent\n growthRate = isNaN(compoundAmount / totalAmount)\n ? 0\n : compoundAmount / totalAmount;\n const poolName = investorPoolMap.get(\n event.investor_id,\n ) as SingleAssetPoolNames;\n const coinName = poolCoinMap[poolName];\n\n growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);\n }\n\n // Accumulate the time-weighted growth\n totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;\n previousGrowthRate = growthRate;\n\n // Accumulate the total time span\n totalTimeSpan += timeDiff;\n previousTimeDiff = timeDiff;\n\n // Update the previous timestamp to the current event's timestamp\n previousTimestamp = event.timestamp;\n }\n } catch (error) {\n console.error(\"Error calculating apr from events.\");\n if (events.length > 0) {\n console.error(\"Investor-ID: \", events[0].investor_id);\n }\n }\n const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;\n\n return apr;\n}\n\nasync function calculateAprForInvestors(\n events: AutoCompoundingEventNode[],\n): Promise<Record<string, number>> {\n const investorEvents: Record<string, AutoCompoundingEventNode[]> = {};\n\n // Step 1: Segregate events by investor_id\n for (const event of events) {\n const investorId = event.investor_id;\n if (!investorEvents[investorId]) {\n investorEvents[investorId] = [];\n }\n investorEvents[investorId].push(event);\n }\n\n // Step 2: Calculate APR for each event and aggregate for each investor\n const aprPromises: Promise<{ investorId: string; apr: number }>[] = [];\n\n for (const investorId in investorEvents) {\n const investorEventList = investorEvents[investorId];\n // Push a promise that resolves to an object containing the investorId and the corresponding APR\n aprPromises.push(\n calculateAprForInvestor(investorEventList).then((apr) => ({\n investorId,\n apr,\n })),\n );\n }\n\n // Resolve all promises and map them to the corresponding APR\n const aprs = await Promise.all(aprPromises);\n\n // Convert the array of results into an object mapping investorId to APR\n const aprMap = aprs.reduce(\n (map, result) => {\n map[result.investorId] = result.apr;\n return map;\n },\n {} as Record<string, number>,\n );\n\n return aprMap;\n}\n\nexport async function calculateAprForPools(\n events: AutoCompoundingEventNode[],\n): Promise<Record<PoolName, number>> {\n const aprMap: Record<string, number> = {};\n const investorPoolNameMap = await getInvestorPoolMap();\n const investorAprMap = await calculateAprForInvestors(events);\n for (const investorId in investorAprMap) {\n const poolName = investorPoolNameMap.get(investorId);\n if (poolName) {\n aprMap[poolName] = investorAprMap[investorId];\n }\n }\n return aprMap;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"fetchAutoCompoundingEvents.d.ts","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAwB,MAAM,uBAAuB,CAAC;AAEvE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,EAEjC,MAAM,YAAY,CAAC;AAEpB,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,gCAAgC,GACvC,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAkHrC;AAED,wBAAsB,uBAAuB,CAC3C,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,CA2IjB;AA6CD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAWnC"}
1
+ {"version":3,"file":"fetchAutoCompoundingEvents.d.ts","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAwB,MAAM,uBAAuB,CAAC;AAEvE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,EAEjC,MAAM,YAAY,CAAC;AAEpB,wBAAsB,0BAA0B,CAC9C,MAAM,EAAE,gCAAgC,GACvC,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAkHrC;AAED,wBAAsB,uBAAuB,CAC3C,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,CAoJjB;AA6CD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,wBAAwB,EAAE,GACjC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAWnC"}
@@ -108,130 +108,138 @@ export async function fetchAutoCompoundingEvents(params) {
108
108
  return autoCompoundingEvents;
109
109
  }
110
110
  export async function calculateAprForInvestor(events) {
111
- // Sort events by timestamp to process them in order
112
- events.sort((a, b) => a.timestamp - b.timestamp);
113
111
  let totalGrowth = 0;
114
112
  let totalTimeSpan = 0;
115
- let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event
116
- let previousGrowthRate = 0;
117
- let previousTimeDiff = 0;
118
- let prevCompoundA = 0n;
119
- let prevCompoundB = 0n;
120
- const investorPoolMap = await getInvestorPoolMap();
121
- // const matchInvestor =
122
- // "0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43";
123
- // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {
124
- // console.log(
125
- // "Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)",
126
- // );
127
- // }
128
- for (const event of events) {
129
- // Calculate the time difference from the previous event
130
- let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);
131
- // Calculate growth rate
132
- let growthRate = 0;
133
- if ("total_amount_a" in event && "total_amount_b" in event) {
134
- let growthA = 0;
135
- let growthB = 0;
136
- if (Number(event.total_amount_a) === 0) {
137
- prevCompoundA += event.compound_amount_a;
138
- }
139
- else {
140
- prevCompoundA = 0n;
141
- }
142
- if (prevCompoundA > 0n) {
143
- growthA =
144
- Number(event.total_amount_a) === 0
145
- ? 0
146
- : Number(event.compound_amount_a + prevCompoundA) /
147
- Number(event.total_amount_a - prevCompoundA);
148
- }
149
- else {
150
- growthA =
151
- Number(event.total_amount_a) === 0
152
- ? 0
153
- : Number(event.compound_amount_a) / Number(event.total_amount_a);
154
- }
155
- if (Number(event.total_amount_b) === 0) {
156
- prevCompoundB += event.compound_amount_b;
113
+ try {
114
+ // Sort events by timestamp to process them in order
115
+ events.sort((a, b) => a.timestamp - b.timestamp);
116
+ let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event
117
+ let previousGrowthRate = 0;
118
+ let previousTimeDiff = 0;
119
+ let prevCompoundA = 0n;
120
+ let prevCompoundB = 0n;
121
+ const investorPoolMap = await getInvestorPoolMap();
122
+ // const matchInvestor =
123
+ // "0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43";
124
+ // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {
125
+ // console.log(
126
+ // "Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)",
127
+ // );
128
+ // }
129
+ for (const event of events) {
130
+ // Calculate the time difference from the previous event
131
+ let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);
132
+ // Calculate growth rate
133
+ let growthRate = 0;
134
+ if ("total_amount_a" in event && "total_amount_b" in event) {
135
+ let growthA = 0;
136
+ let growthB = 0;
137
+ if (Number(event.total_amount_a) === 0) {
138
+ prevCompoundA += event.compound_amount_a;
139
+ }
140
+ else {
141
+ prevCompoundA = 0n;
142
+ }
143
+ if (prevCompoundA > 0n) {
144
+ growthA =
145
+ Number(event.total_amount_a) === 0
146
+ ? 0
147
+ : Number(event.compound_amount_a + prevCompoundA) /
148
+ Number(event.total_amount_a - prevCompoundA);
149
+ }
150
+ else {
151
+ growthA =
152
+ Number(event.total_amount_a) === 0
153
+ ? 0
154
+ : Number(event.compound_amount_a) / Number(event.total_amount_a);
155
+ }
156
+ if (Number(event.total_amount_b) === 0) {
157
+ prevCompoundB += event.compound_amount_b;
158
+ }
159
+ else {
160
+ prevCompoundB = 0n;
161
+ }
162
+ if (prevCompoundB > 0n) {
163
+ growthB =
164
+ Number(event.total_amount_b) == 0
165
+ ? 0
166
+ : Number(event.compound_amount_b + prevCompoundB) /
167
+ Number(event.total_amount_b - prevCompoundB);
168
+ }
169
+ else {
170
+ growthB =
171
+ Number(event.total_amount_b) == 0
172
+ ? 0
173
+ : Number(event.compound_amount_b) / Number(event.total_amount_b);
174
+ }
175
+ // if (
176
+ // event.investor_id ===
177
+ // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
178
+ // ) {
179
+ // console.log(
180
+ // timeDiff / (1000 * 60),
181
+ // event.timestamp,
182
+ // event.timestamp - previousTimestamp,
183
+ // );
184
+ // }
185
+ // if (
186
+ // event.investor_id ===
187
+ // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
188
+ // ) {
189
+ // console.log(
190
+ // event.compound_amount_a,
191
+ // event.total_amount_a,
192
+ // growthA,
193
+ // event.compound_amount_b,
194
+ // event.total_amount_b,
195
+ // growthB,
196
+ // );
197
+ // }
198
+ growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B
199
+ // if (event.investor_id === matchInvestor) {
200
+ // console.log(
201
+ // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,
202
+ // );
203
+ // }
204
+ if (Math.abs(growthA - growthB) > 0.5) {
205
+ // skip row, fill with previous event
206
+ growthRate = previousGrowthRate;
207
+ timeDiff = previousTimeDiff;
208
+ }
209
+ if (growthRate > 0.005) {
210
+ growthRate = 0;
211
+ }
157
212
  }
158
- else {
159
- prevCompoundB = 0n;
160
- }
161
- if (prevCompoundB > 0n) {
162
- growthB =
163
- Number(event.total_amount_b) == 0
164
- ? 0
165
- : Number(event.compound_amount_b + prevCompoundB) /
166
- Number(event.total_amount_b - prevCompoundB);
167
- }
168
- else {
169
- growthB =
170
- Number(event.total_amount_b) == 0
171
- ? 0
172
- : Number(event.compound_amount_b) / Number(event.total_amount_b);
173
- }
174
- // if (
175
- // event.investor_id ===
176
- // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
177
- // ) {
178
- // console.log(
179
- // timeDiff / (1000 * 60),
180
- // event.timestamp,
181
- // event.timestamp - previousTimestamp,
182
- // );
183
- // }
184
- // if (
185
- // event.investor_id ===
186
- // "0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6"
187
- // ) {
188
- // console.log(
189
- // event.compound_amount_a,
190
- // event.total_amount_a,
191
- // growthA,
192
- // event.compound_amount_b,
193
- // event.total_amount_b,
194
- // growthB,
195
- // );
196
- // }
197
- growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B
198
- // if (event.investor_id === matchInvestor) {
199
- // console.log(
200
- // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,
201
- // );
202
- // }
203
- if (Math.abs(growthA - growthB) > 0.5) {
204
- // skip row, fill with previous event
205
- growthRate = previousGrowthRate;
206
- timeDiff = previousTimeDiff;
207
- }
208
- if (growthRate > 0.005) {
209
- growthRate = 0;
213
+ else if ("total_amount" in event) {
214
+ let compoundAmount = Number(event.compound_amount);
215
+ let totalAmount = Number(event.total_amount);
216
+ if ("cur_total_debt" in event && "accrued_interest" in event) {
217
+ compoundAmount = Number(event.compound_amount - event.accrued_interest);
218
+ totalAmount = Number(event.total_amount - event.cur_total_debt);
219
+ }
220
+ // NaviAutoCompoundingEvent
221
+ growthRate = isNaN(compoundAmount / totalAmount)
222
+ ? 0
223
+ : compoundAmount / totalAmount;
224
+ const poolName = investorPoolMap.get(event.investor_id);
225
+ const coinName = poolCoinMap[poolName];
226
+ growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);
210
227
  }
228
+ // Accumulate the time-weighted growth
229
+ totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;
230
+ previousGrowthRate = growthRate;
231
+ // Accumulate the total time span
232
+ totalTimeSpan += timeDiff;
233
+ previousTimeDiff = timeDiff;
234
+ // Update the previous timestamp to the current event's timestamp
235
+ previousTimestamp = event.timestamp;
211
236
  }
212
- else if ("total_amount" in event) {
213
- let compoundAmount = Number(event.compound_amount);
214
- let totalAmount = Number(event.total_amount);
215
- if ("cur_total_debt" in event && "accrued_interest" in event) {
216
- compoundAmount = Number(event.compound_amount - event.accrued_interest);
217
- totalAmount = Number(event.total_amount - event.cur_total_debt);
218
- }
219
- // NaviAutoCompoundingEvent
220
- growthRate = isNaN(compoundAmount / totalAmount)
221
- ? 0
222
- : compoundAmount / totalAmount;
223
- const poolName = investorPoolMap.get(event.investor_id);
224
- const coinName = poolCoinMap[poolName];
225
- growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);
237
+ }
238
+ catch (error) {
239
+ console.error("Error calculating apr from events.");
240
+ if (events.length > 0) {
241
+ console.error("Investor-ID: ", events[0].investor_id);
226
242
  }
227
- // Accumulate the time-weighted growth
228
- totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;
229
- previousGrowthRate = growthRate;
230
- // Accumulate the total time span
231
- totalTimeSpan += timeDiff;
232
- previousTimeDiff = timeDiff;
233
- // Update the previous timestamp to the current event's timestamp
234
- previousTimestamp = event.timestamp;
235
243
  }
236
244
  const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;
237
245
  return apr;
@@ -1 +1 @@
1
- {"version":3,"file":"fetchAutoCompoundingEvents.js","sourceRoot":"","sources":["../../../../src/sui-sdk/events/fetchAutoCompoundingEvents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,QAAQ,GACT,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAO/C,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,MAAwC;IAExC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IACxC,oDAAoD;IACpD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACpC,MAAM,SAAS,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC,wBAAwB,CAAC;YAC9D,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;gBACtE,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,2FAA2F;QAC3F,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACvC,MAAM,SAAS,GAAG,IAAI,CAAC,wBAAwB,CAAC;YAChD,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;gBACtE,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC/B,CAAC;QACH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KAAK,UAAU,oBAAoB,CACxC,MAAkC;IAElC,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,mBAAmB,GAAG,MAAM,kBAAkB,EAAE,CAAC;IACvD,MAAM,cAAc,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;IAC9D,KAAK,MAAM,UAAU,IAAI,cAAc,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACrD,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { coins } from \"../../common/coins.js\";\nimport {\n getInvestorPoolMap,\n poolCoinMap,\n poolInfo,\n} from \"../../common/maps.js\";\nimport { PoolName, SingleAssetPoolNames } from \"../../common/types.js\";\nimport { fetchEvents } from \"./fetchEvents.js\";\nimport {\n AutoCompoundingEventNode,\n FetchAutoCompoundingEventsParams,\n //RebalanceEventNode,\n} from \"./types.js\";\n\nexport async function fetchAutoCompoundingEvents(\n params: FetchAutoCompoundingEventsParams,\n): Promise<AutoCompoundingEventNode[]> {\n const eventTypesSet = new Set<string>();\n // const rebalanceEventTypesSet = new Set<string>();\n if (params.poolNames) {\n params.poolNames.forEach((poolName) => {\n const eventType = poolInfo[poolName].autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n } else {\n // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n Object.values(poolInfo).forEach((info) => {\n const eventType = info.autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n }\n // for rebalance\n // if (params.poolNames) {\n // params.poolNames.forEach((poolName) => {\n // const eventType = poolInfo[poolName].rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // } else {\n // // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n // Object.values(poolInfo).forEach((info) => {\n // const eventType = info.rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // }\n\n const eventTypes = Array.from(eventTypesSet);\n // const rebalanceEventTypes = Array.from(rebalanceEventTypesSet);\n\n const eventsPromises = eventTypes.map(async (eventType) => {\n const events = (\n await fetchEvents({\n startTime: params.startTime,\n endTime: params.endTime,\n eventTypes: [eventType],\n })\n ).filter((e) => {\n if (params.poolNames) {\n return params.poolNames\n .map((poolName) => poolInfo[poolName].investorId)\n .includes((e as AutoCompoundingEventNode).investor_id);\n } else {\n return true;\n }\n });\n return events;\n });\n // const rebalanceEventsPromises = rebalanceEventTypes.map(async (eventType) => {\n // const events = (\n // await fetchEvents({\n // startTime: params.startTime,\n // endTime: params.endTime,\n // eventTypes: [eventType],\n // })\n // ).filter((e) => {\n // if (params.poolNames) {\n // return params.poolNames\n // .map((poolName) => poolInfo[poolName].investorId)\n // .includes((e as AutoCompoundingEventNode).investor_id);\n // } else {\n // return true;\n // }\n // });\n // return events;\n // });\n\n const events = (await Promise.all(eventsPromises)).flat();\n\n const autoCompoundingEvents = events.map((e) => {\n return e as AutoCompoundingEventNode;\n });\n\n // const rebalanceEvents = (await Promise.all(rebalanceEventsPromises)).flat();\n\n // const rebalancingEvents = rebalanceEvents.map((e) => {\n // return e as RebalanceEventNode;\n // });\n\n // const investorPoolMap = await getInvestorPoolMap();\n\n // for (const x of autoCompoundingEvents) {\n // if (\n // investorPoolMap.get(x.investor_id) &&\n // poolInfo[investorPoolMap.get(x.investor_id)!].parentProtocolName ==\n // \"CETUS\"\n // ) {\n // if (\n // \"total_amount_a\" in x &&\n // x.total_amount_a == 0n &&\n // x.total_amount_b == 0n\n // ) {\n // for (const y of rebalancingEvents) {\n // if (y.investor_id == x.investor_id && y.timestamp == x.timestamp) {\n // // works if its guaranteed that for one timestamp there is only one rebalance for one pool\n // x.total_amount_a = BigInt(y.amount_a_before);\n // x.total_amount_b = BigInt(y.amount_b_before);\n // }\n // }\n // }\n // }\n // }\n\n return autoCompoundingEvents;\n}\n\nexport async function calculateAprForInvestor(\n events: AutoCompoundingEventNode[],\n): Promise<number> {\n // Sort events by timestamp to process them in order\n events.sort((a, b) => a.timestamp - b.timestamp);\n\n let totalGrowth = 0;\n let totalTimeSpan = 0;\n let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event\n let previousGrowthRate = 0;\n let previousTimeDiff = 0;\n let prevCompoundA = 0n;\n let prevCompoundB = 0n;\n\n const investorPoolMap = await getInvestorPoolMap();\n\n // const matchInvestor =\n // \"0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43\";\n // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {\n // console.log(\n // \"Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)\",\n // );\n // }\n for (const event of events) {\n // Calculate the time difference from the previous event\n let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);\n\n // Calculate growth rate\n let growthRate = 0;\n if (\"total_amount_a\" in event && \"total_amount_b\" in event) {\n let growthA = 0;\n let growthB = 0;\n\n if (Number(event.total_amount_a) === 0) {\n prevCompoundA += event.compound_amount_a;\n } else {\n prevCompoundA = 0n;\n }\n if (prevCompoundA > 0n) {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a + prevCompoundA) /\n Number(event.total_amount_a - prevCompoundA);\n } else {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a) / Number(event.total_amount_a);\n }\n\n if (Number(event.total_amount_b) === 0) {\n prevCompoundB += event.compound_amount_b;\n } else {\n prevCompoundB = 0n;\n }\n\n if (prevCompoundB > 0n) {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b + prevCompoundB) /\n Number(event.total_amount_b - prevCompoundB);\n } else {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b) / Number(event.total_amount_b);\n }\n\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // timeDiff / (1000 * 60),\n // event.timestamp,\n // event.timestamp - previousTimestamp,\n // );\n // }\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // event.compound_amount_a,\n // event.total_amount_a,\n // growthA,\n // event.compound_amount_b,\n // event.total_amount_b,\n // growthB,\n // );\n // }\n growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B\n // if (event.investor_id === matchInvestor) {\n // console.log(\n // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,\n // );\n // }\n if (Math.abs(growthA - growthB) > 0.5) {\n // skip row, fill with previous event\n growthRate = previousGrowthRate;\n timeDiff = previousTimeDiff;\n }\n if (growthRate > 0.005) {\n growthRate = 0;\n }\n } else if (\"total_amount\" in event) {\n let compoundAmount: number = Number(event.compound_amount);\n let totalAmount: number = Number(event.total_amount);\n if (\"cur_total_debt\" in event && \"accrued_interest\" in event) {\n compoundAmount = Number(event.compound_amount - event.accrued_interest);\n totalAmount = Number(event.total_amount - event.cur_total_debt);\n }\n // NaviAutoCompoundingEvent\n growthRate = isNaN(compoundAmount / totalAmount)\n ? 0\n : compoundAmount / totalAmount;\n const poolName = investorPoolMap.get(\n event.investor_id,\n ) as SingleAssetPoolNames;\n const coinName = poolCoinMap[poolName];\n\n growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);\n }\n\n // Accumulate the time-weighted growth\n totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;\n previousGrowthRate = growthRate;\n\n // Accumulate the total time span\n totalTimeSpan += timeDiff;\n previousTimeDiff = timeDiff;\n\n // Update the previous timestamp to the current event's timestamp\n previousTimestamp = event.timestamp;\n }\n\n const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;\n\n return apr;\n}\n\nasync function calculateAprForInvestors(\n events: AutoCompoundingEventNode[],\n): Promise<Record<string, number>> {\n const investorEvents: Record<string, AutoCompoundingEventNode[]> = {};\n\n // Step 1: Segregate events by investor_id\n for (const event of events) {\n const investorId = event.investor_id;\n if (!investorEvents[investorId]) {\n investorEvents[investorId] = [];\n }\n investorEvents[investorId].push(event);\n }\n\n // Step 2: Calculate APR for each event and aggregate for each investor\n const aprPromises: Promise<{ investorId: string; apr: number }>[] = [];\n\n for (const investorId in investorEvents) {\n const investorEventList = investorEvents[investorId];\n // Push a promise that resolves to an object containing the investorId and the corresponding APR\n aprPromises.push(\n calculateAprForInvestor(investorEventList).then((apr) => ({\n investorId,\n apr,\n })),\n );\n }\n\n // Resolve all promises and map them to the corresponding APR\n const aprs = await Promise.all(aprPromises);\n\n // Convert the array of results into an object mapping investorId to APR\n const aprMap = aprs.reduce(\n (map, result) => {\n map[result.investorId] = result.apr;\n return map;\n },\n {} as Record<string, number>,\n );\n\n return aprMap;\n}\n\nexport async function calculateAprForPools(\n events: AutoCompoundingEventNode[],\n): Promise<Record<PoolName, number>> {\n const aprMap: Record<string, number> = {};\n const investorPoolNameMap = await getInvestorPoolMap();\n const investorAprMap = await calculateAprForInvestors(events);\n for (const investorId in investorAprMap) {\n const poolName = investorPoolNameMap.get(investorId);\n if (poolName) {\n aprMap[poolName] = investorAprMap[investorId];\n }\n }\n return aprMap;\n}\n"]}
1
+ {"version":3,"file":"fetchAutoCompoundingEvents.js","sourceRoot":"","sources":["../../../../src/sui-sdk/events/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AAC,GAAG,CAAC;QACpC,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAA4B,CAC7B,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAkC;IAElC,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,mBAAmB,GAAG,MAAM,kBAAkB,EAAE,CAAC;IACvD,MAAM,cAAc,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;IAC9D,KAAK,MAAM,UAAU,IAAI,cAAc,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACrD,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,QAAQ,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { coins } from \"../../common/coins.js\";\nimport {\n getInvestorPoolMap,\n poolCoinMap,\n poolInfo,\n} from \"../../common/maps.js\";\nimport { PoolName, SingleAssetPoolNames } from \"../../common/types.js\";\nimport { fetchEvents } from \"./fetchEvents.js\";\nimport {\n AutoCompoundingEventNode,\n FetchAutoCompoundingEventsParams,\n //RebalanceEventNode,\n} from \"./types.js\";\n\nexport async function fetchAutoCompoundingEvents(\n params: FetchAutoCompoundingEventsParams,\n): Promise<AutoCompoundingEventNode[]> {\n const eventTypesSet = new Set<string>();\n // const rebalanceEventTypesSet = new Set<string>();\n if (params.poolNames) {\n params.poolNames.forEach((poolName) => {\n const eventType = poolInfo[poolName].autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n } else {\n // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n Object.values(poolInfo).forEach((info) => {\n const eventType = info.autoCompoundingEventType;\n if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n eventTypesSet.add(eventType);\n }\n });\n }\n // for rebalance\n // if (params.poolNames) {\n // params.poolNames.forEach((poolName) => {\n // const eventType = poolInfo[poolName].rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // } else {\n // // Iterate over all the values in poolInfo and add each autoCompoundingEventType to the Set\n // Object.values(poolInfo).forEach((info) => {\n // const eventType = info.rebalanceEventType;\n // if (eventType !== undefined && eventType !== null && eventType !== \"\") {\n // rebalanceEventTypesSet.add(eventType);\n // }\n // });\n // }\n\n const eventTypes = Array.from(eventTypesSet);\n // const rebalanceEventTypes = Array.from(rebalanceEventTypesSet);\n\n const eventsPromises = eventTypes.map(async (eventType) => {\n const events = (\n await fetchEvents({\n startTime: params.startTime,\n endTime: params.endTime,\n eventTypes: [eventType],\n })\n ).filter((e) => {\n if (params.poolNames) {\n return params.poolNames\n .map((poolName) => poolInfo[poolName].investorId)\n .includes((e as AutoCompoundingEventNode).investor_id);\n } else {\n return true;\n }\n });\n return events;\n });\n // const rebalanceEventsPromises = rebalanceEventTypes.map(async (eventType) => {\n // const events = (\n // await fetchEvents({\n // startTime: params.startTime,\n // endTime: params.endTime,\n // eventTypes: [eventType],\n // })\n // ).filter((e) => {\n // if (params.poolNames) {\n // return params.poolNames\n // .map((poolName) => poolInfo[poolName].investorId)\n // .includes((e as AutoCompoundingEventNode).investor_id);\n // } else {\n // return true;\n // }\n // });\n // return events;\n // });\n\n const events = (await Promise.all(eventsPromises)).flat();\n\n const autoCompoundingEvents = events.map((e) => {\n return e as AutoCompoundingEventNode;\n });\n\n // const rebalanceEvents = (await Promise.all(rebalanceEventsPromises)).flat();\n\n // const rebalancingEvents = rebalanceEvents.map((e) => {\n // return e as RebalanceEventNode;\n // });\n\n // const investorPoolMap = await getInvestorPoolMap();\n\n // for (const x of autoCompoundingEvents) {\n // if (\n // investorPoolMap.get(x.investor_id) &&\n // poolInfo[investorPoolMap.get(x.investor_id)!].parentProtocolName ==\n // \"CETUS\"\n // ) {\n // if (\n // \"total_amount_a\" in x &&\n // x.total_amount_a == 0n &&\n // x.total_amount_b == 0n\n // ) {\n // for (const y of rebalancingEvents) {\n // if (y.investor_id == x.investor_id && y.timestamp == x.timestamp) {\n // // works if its guaranteed that for one timestamp there is only one rebalance for one pool\n // x.total_amount_a = BigInt(y.amount_a_before);\n // x.total_amount_b = BigInt(y.amount_b_before);\n // }\n // }\n // }\n // }\n // }\n\n return autoCompoundingEvents;\n}\n\nexport async function calculateAprForInvestor(\n events: AutoCompoundingEventNode[],\n): Promise<number> {\n let totalGrowth = 0;\n let totalTimeSpan = 0;\n\n try {\n // Sort events by timestamp to process them in order\n events.sort((a, b) => a.timestamp - b.timestamp);\n\n let previousTimestamp = events[0].timestamp; // Start with the timestamp of the first event\n let previousGrowthRate = 0;\n let previousTimeDiff = 0;\n let prevCompoundA = 0n;\n let prevCompoundB = 0n;\n\n const investorPoolMap = await getInvestorPoolMap();\n\n // const matchInvestor =\n // \"0x05fa099d1df7b5bfb2e420d5ee2d63508db17c40ce7c4e0ca0305cd5df974e43\";\n // if (events && events.length > 0 && events[0].investor_id === matchInvestor) {\n // console.log(\n // \"Compund A,Total A,Compound B,Total B,Freebalance A,Freebalance B,GrowthA,GrowthB,AvgGrowth,Timestamp,TimeDiff (Min)\",\n // );\n // }\n for (const event of events) {\n // Calculate the time difference from the previous event\n let timeDiff = event.timestamp - previousTimestamp; // / (1000 * 60 * 60 * 24);\n\n // Calculate growth rate\n let growthRate = 0;\n if (\"total_amount_a\" in event && \"total_amount_b\" in event) {\n let growthA = 0;\n let growthB = 0;\n\n if (Number(event.total_amount_a) === 0) {\n prevCompoundA += event.compound_amount_a;\n } else {\n prevCompoundA = 0n;\n }\n if (prevCompoundA > 0n) {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a + prevCompoundA) /\n Number(event.total_amount_a - prevCompoundA);\n } else {\n growthA =\n Number(event.total_amount_a) === 0\n ? 0\n : Number(event.compound_amount_a) / Number(event.total_amount_a);\n }\n\n if (Number(event.total_amount_b) === 0) {\n prevCompoundB += event.compound_amount_b;\n } else {\n prevCompoundB = 0n;\n }\n\n if (prevCompoundB > 0n) {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b + prevCompoundB) /\n Number(event.total_amount_b - prevCompoundB);\n } else {\n growthB =\n Number(event.total_amount_b) == 0\n ? 0\n : Number(event.compound_amount_b) / Number(event.total_amount_b);\n }\n\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // timeDiff / (1000 * 60),\n // event.timestamp,\n // event.timestamp - previousTimestamp,\n // );\n // }\n // if (\n // event.investor_id ===\n // \"0xd060e81548aee885bd3d37ae0caec181185be792bf45412e0d0acccd1e0174e6\"\n // ) {\n // console.log(\n // event.compound_amount_a,\n // event.total_amount_a,\n // growthA,\n // event.compound_amount_b,\n // event.total_amount_b,\n // growthB,\n // );\n // }\n growthRate = (growthA + growthB) / 2; // Averaging growth rates for token A and B\n // if (event.investor_id === matchInvestor) {\n // console.log(\n // `${event.compound_amount_a},${event.total_amount_a},${event.compound_amount_b},${event.total_amount_b},${event.free_balance_a},${event.free_balance_b},${growthA},${growthB},${growthRate},${event.timestamp},${(event.timestamp - previousTimestamp) / (1000 * 60)}`,\n // );\n // }\n if (Math.abs(growthA - growthB) > 0.5) {\n // skip row, fill with previous event\n growthRate = previousGrowthRate;\n timeDiff = previousTimeDiff;\n }\n if (growthRate > 0.005) {\n growthRate = 0;\n }\n } else if (\"total_amount\" in event) {\n let compoundAmount: number = Number(event.compound_amount);\n let totalAmount: number = Number(event.total_amount);\n if (\"cur_total_debt\" in event && \"accrued_interest\" in event) {\n compoundAmount = Number(\n event.compound_amount - event.accrued_interest,\n );\n totalAmount = Number(event.total_amount - event.cur_total_debt);\n }\n // NaviAutoCompoundingEvent\n growthRate = isNaN(compoundAmount / totalAmount)\n ? 0\n : compoundAmount / totalAmount;\n const poolName = investorPoolMap.get(\n event.investor_id,\n ) as SingleAssetPoolNames;\n const coinName = poolCoinMap[poolName];\n\n growthRate = growthRate * Math.pow(10, 9 - coins[coinName].expo);\n }\n\n // Accumulate the time-weighted growth\n totalGrowth = (totalGrowth + 1) * (1 + growthRate) - 1;\n previousGrowthRate = growthRate;\n\n // Accumulate the total time span\n totalTimeSpan += timeDiff;\n previousTimeDiff = timeDiff;\n\n // Update the previous timestamp to the current event's timestamp\n previousTimestamp = event.timestamp;\n }\n } catch (error) {\n console.error(\"Error calculating apr from events.\");\n if (events.length > 0) {\n console.error(\"Investor-ID: \", events[0].investor_id);\n }\n }\n const apr = (totalGrowth / totalTimeSpan) * (1000 * 60 * 60 * 24 * 365) * 100;\n\n return apr;\n}\n\nasync function calculateAprForInvestors(\n events: AutoCompoundingEventNode[],\n): Promise<Record<string, number>> {\n const investorEvents: Record<string, AutoCompoundingEventNode[]> = {};\n\n // Step 1: Segregate events by investor_id\n for (const event of events) {\n const investorId = event.investor_id;\n if (!investorEvents[investorId]) {\n investorEvents[investorId] = [];\n }\n investorEvents[investorId].push(event);\n }\n\n // Step 2: Calculate APR for each event and aggregate for each investor\n const aprPromises: Promise<{ investorId: string; apr: number }>[] = [];\n\n for (const investorId in investorEvents) {\n const investorEventList = investorEvents[investorId];\n // Push a promise that resolves to an object containing the investorId and the corresponding APR\n aprPromises.push(\n calculateAprForInvestor(investorEventList).then((apr) => ({\n investorId,\n apr,\n })),\n );\n }\n\n // Resolve all promises and map them to the corresponding APR\n const aprs = await Promise.all(aprPromises);\n\n // Convert the array of results into an object mapping investorId to APR\n const aprMap = aprs.reduce(\n (map, result) => {\n map[result.investorId] = result.apr;\n return map;\n },\n {} as Record<string, number>,\n );\n\n return aprMap;\n}\n\nexport async function calculateAprForPools(\n events: AutoCompoundingEventNode[],\n): Promise<Record<PoolName, number>> {\n const aprMap: Record<string, number> = {};\n const investorPoolNameMap = await getInvestorPoolMap();\n const investorAprMap = await calculateAprForInvestors(events);\n for (const investorId in investorAprMap) {\n const poolName = investorPoolNameMap.get(investorId);\n if (poolName) {\n aprMap[poolName] = investorAprMap[investorId];\n }\n }\n return aprMap;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alphafi/alphafi-sdk",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "description": "Official AlphaFi SDK to access AlphaFi data",
5
5
  "type": "module",
6
6
  "main": "dist/cjs/index.js",