-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (147 loc) · 4.83 KB
/
index.js
File metadata and controls
179 lines (147 loc) · 4.83 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const utils = require("../utils");
const AFFLUENT_MULTIPLY_VAULT_API_URL = "https://api.affluent.org/v2/api/strategyvaults";
const AFFLUENT_LENDING_VAULT_API_URL = "https://api.affluent.org/v2/api/sharevaults";
const AFFLUENT_ASSETS_API_URL = "https://api.affluent.org/v2/api/assets";
const nowSec = () => Math.floor(Date.now() / 1000);
const getAPY = async () => {
try {
const assetMap = await getAssetMap();
const [strategy, share] = await Promise.all([
getStrategyVaultsMapped(assetMap),
getShareVaultsMapped(assetMap),
]);
const merged = [...strategy, ...share];
return merged;
} catch (err) {
console.error("getAPY failed:", err);
return [];
}
};
async function getStrategyVaultsMapped(assetMap) {
const res = await fetch(AFFLUENT_MULTIPLY_VAULT_API_URL);
if (!res.ok) {
throw new Error(`Strategy API error: HTTP ${res.status} ${res.statusText}`);
}
const data = await res.json();
if (!Array.isArray(data)) {
throw new Error("Strategy: Unexpected response shape (not an array)");
}
return data.map((v) => {
const assetKeys = Object.keys(v.assets || {});
const assetSymbols = assetKeys
.map((addr) => assetMap[addr] || addr)
.sort((a, b) => a.localeCompare(b));
const assetSymbolString = assetSymbols.join("-");
return mapToOutput({
address: v.address,
symbol: assetSymbolString,
tvl: v.tvl,
netApy: v.netApy,
poolMeta: v.name,
});
});
}
async function getShareVaultList() {
const res = await fetch(AFFLUENT_LENDING_VAULT_API_URL);
if (!res.ok) {
throw new Error(`Share list API error: HTTP ${res.status} ${res.statusText}`);
}
const data = await res.json();
if (!Array.isArray(data)) {
throw new Error("Share list: Unexpected response shape (not an array)");
}
return data;
}
async function getShareVaultPoint(address, ts = nowSec()) {
const url = `${AFFLUENT_LENDING_VAULT_API_URL}/${address}?from=${ts}&to=${ts}&unit=3600`;
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Share point API error(${address}): HTTP ${res.status} ${res.statusText}`);
}
const arr = await res.json();
return Array.isArray(arr) && arr[0] ? arr[0] : null;
}
async function getShareVaultsMapped(assetMap) {
const list = await getShareVaultList();
const ts = nowSec();
const results = await Promise.allSettled(
list.map(async (v) => {
const p = await getShareVaultPoint(v.address, ts);
const netApy = typeof p?.apy === "number" ? p.apy : undefined;
const tvl = p?.tvl;
const underlyingSymbol = v?.underlying ? (assetMap[v.underlying] || v.underlying) : undefined;
return mapToOutput({
address: v.address,
symbol: underlyingSymbol,
tvl,
netApy,
poolMeta: v.name,
});
})
);
return results.map((r, i) => {
if (r.status === "fulfilled") return r.value;
const sv = list[i];
return mapToOutput({
address: sv.address,
symbol: sv.symbol,
tvl: 0,
netApy: undefined,
poolMeta: sv.poolMeta,
});
});
}
async function getAssetMap() {
const res = await fetch(AFFLUENT_ASSETS_API_URL);
if (!res.ok) {
throw new Error(`Asset API error: HTTP ${res.status} ${res.statusText}`);
}
const data = await res.json();
if (!Array.isArray(data)) {
throw new Error("Asset: Unexpected response shape (not an array)");
}
const map = {};
for (const item of data) {
let symbol = item.symbol;
if (symbol === "FactorialTON") {
symbol = "TON";
}
map[item.address] = symbol;
}
return map;
}
module.exports = {
apy: getAPY,
timetravel: false,
url: "https://affluent.org",
};
function toNumberOr0(v) {
if (typeof v === "number") return v;
if (typeof v === "string") {
const n = Number(v);
return Number.isFinite(n) ? n : 0;
}
return 0;
}
function mapToOutput({ address, symbol, tvl, netApy, poolMeta }) {
const pool = `${address}-TON`;
const project = "affluent";
const apyBase = netApy;
const vaultUrl = `https://app.affluent.org/earn/${address}`;
const output = {
pool,
chain: "TON",
project,
symbol: symbol,
poolMeta: poolMeta,
tvlUsd: toNumberOr0(tvl),
...(apyBase !== undefined ? { apyBase } : {}),
};
if (!hiddenVaultUrlWhitelist.includes(address)) {
output.url = vaultUrl;
}
return output;
}
const hiddenVaultUrlWhitelist = [
"EQD3F7Ex_uxBjxEub8FgeDoYYUSIbAUVyehCb_JSiCVL369T",
];