-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetherfi.ts
68 lines (54 loc) · 1.83 KB
/
etherfi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { AdapterExport } from "../utils/adapter.ts";
import { maybeWrapCORSProxy } from "../utils/cors.ts";
const API_URL = await maybeWrapCORSProxy(
"https://app.ether.fi/api/portfolio/v3/{address}"
);
export default {
fetch: async (address: string) => {
return await (await fetch(API_URL.replace("{address}", address))).json();
},
points: (data: Record<string, unknown>) => {
const res: Record<string, number> = {};
// Parse regular points.
const parse = (obj: object, prefix = "") => {
for (const [k, v] of Object.entries(obj)) {
if (!isNaN(Number(k))) continue; // Array key
const fullKey = prefix ? `${prefix}.${k}` : k;
if (typeof v === "number") res[fullKey] = v;
else if (typeof v === "object" && v !== null) parse(v, fullKey);
}
};
parse(data);
// Parse badges.
if (Array.isArray(data.badges)) {
for (const badge of data.badges) {
if (typeof badge.Points === "number") {
res[`badges.${badge.Name}`] = badge.Points;
}
}
}
return res;
},
total: (data: Record<string, unknown>) => {
let historical = 0;
let previous = 0;
let current = 0;
const traverse = (obj: object) => {
for (const value of Object.values(obj)) {
if (typeof value === "object" && value !== null) {
traverse(value);
}
}
// I love typescript
const x = obj as { [key: string]: unknown };
if (typeof x.CurrentSeasonPoints === "number")
current += x.CurrentSeasonPoints;
if (typeof x.PreviousSeasonPoints === "number")
previous += x.PreviousSeasonPoints;
if (typeof x.PreviousHistoricalPoints === "number")
historical += x.PreviousHistoricalPoints;
};
traverse(data);
return { current, previous, historical };
},
} as AdapterExport;