-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceService.js
More file actions
219 lines (192 loc) · 6.12 KB
/
Copy pathpriceService.js
File metadata and controls
219 lines (192 loc) · 6.12 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { proxyFetch } from './fetchUtil.js';
import {
PRICE_CACHE_TTL_MS,
PRICE_NEGATIVE_CACHE_TTL_MS,
PRICE_FETCH_TIMEOUT_MS,
} from './config.js';
const CHAIN_ID_TO_COINGECKO_ID = {
1: 'ethereum',
10: 'ethereum',
25: 'crypto-com-chain',
56: 'binancecoin',
66: 'oec-token',
100: 'xdai',
137: 'matic-network',
250: 'fantom',
288: 'ethereum',
324: 'ethereum',
1088: 'metis-token',
1284: 'moonbeam',
1285: 'moonriver',
2222: 'kava',
5000: 'mantle',
7700: 'canto',
8217: 'kaia',
8453: 'ethereum',
9001: 'evmos',
42161: 'ethereum',
42170: 'ethereum',
42220: 'celo',
43114: 'avalanche-2',
59144: 'ethereum',
81457: 'ethereum',
534352: 'ethereum',
1313161554: 'ethereum',
1666600000: 'harmony',
};
const COINGECKO_PRICE_URL = 'https://api.coingecko.com/api/v3/simple/price';
// Cache keyed by coinId so sibling chains share a single entry naturally.
// Value: { usd: number, updatedAt: string } for hits,
// { usd: null, updatedAt: string } for negative entries (short TTL).
const priceCache = new Map();
// Coalesce concurrent fetches: one in-flight promise per coinId.
const inflight = new Map();
export function getCoinGeckoId(chainId) {
return CHAIN_ID_TO_COINGECKO_ID[chainId] ?? null;
}
function isFresh(entry) {
if (!entry) return false;
const age = Date.now() - new Date(entry.updatedAt).getTime();
const ttl = entry.usd === null ? PRICE_NEGATIVE_CACHE_TTL_MS : PRICE_CACHE_TTL_MS;
return age <= ttl;
}
function getCachedByCoinId(coinId) {
const entry = priceCache.get(coinId);
return isFresh(entry) ? entry : null;
}
function toPublic(entry) {
if (!entry || entry.usd === null) return null;
return { usd: entry.usd, updatedAt: entry.updatedAt };
}
async function fetchWithTimeout(url) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), PRICE_FETCH_TIMEOUT_MS);
try {
return await proxyFetch(url, { signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
async function fetchCoinIds(coinIds) {
if (coinIds.length === 0) return { map: new Map(), ok: true };
// Coalesce: for each coinId, reuse an in-flight promise if one exists.
// Otherwise schedule the missing IDs in a single batched request.
const result = new Map();
let ok = true;
const toFetch = [];
const waiters = [];
for (const id of coinIds) {
const pending = inflight.get(id);
if (pending) {
waiters.push(pending.then(({ map, ok: pendingOk }) => ({ id, usd: map.get(id), ok: pendingOk })));
} else {
toFetch.push(id);
}
}
if (toFetch.length > 0) {
const batchPromise = (async () => {
const map = new Map();
let batchOk = true;
const url = `${COINGECKO_PRICE_URL}?ids=${toFetch.join(',')}&vs_currencies=usd`;
try {
const response = await fetchWithTimeout(url);
if (response.ok) {
const data = await response.json();
for (const [id, prices] of Object.entries(data)) {
if (typeof prices?.usd === 'number') map.set(id, prices.usd);
}
} else {
batchOk = false;
console.warn(`CoinGecko price fetch failed: HTTP ${response.status}`);
}
} catch (err) {
batchOk = false;
console.warn(`CoinGecko price fetch error: ${err.message}`);
}
return { map, ok: batchOk };
})();
for (const id of toFetch) {
inflight.set(id, batchPromise);
}
try {
const { map, ok: batchOk } = await batchPromise;
if (!batchOk) ok = false;
for (const id of toFetch) {
if (map.has(id)) result.set(id, map.get(id));
}
} finally {
for (const id of toFetch) inflight.delete(id);
}
}
for (const { id, usd, ok: waiterOk } of await Promise.all(waiters.map(p => p))) {
if (!waiterOk) ok = false;
if (usd !== undefined) result.set(id, usd);
}
return { map: result, ok };
}
function recordResults(coinIds, { map: fetched, ok }) {
const updatedAt = new Date().toISOString();
for (const id of coinIds) {
if (fetched.has(id)) {
priceCache.set(id, { usd: fetched.get(id), updatedAt });
} else if (ok) {
// Upstream succeeded but didn't return this id — it's genuinely missing.
// Negative-cache with the short TTL so we don't hammer CoinGecko.
priceCache.set(id, { usd: null, updatedAt });
}
// else: upstream failed; leave any prior entry intact so retries can succeed.
}
}
export async function getPriceForChain(chainId) {
const coinId = getCoinGeckoId(chainId);
if (!coinId) return null;
const cached = getCachedByCoinId(coinId);
if (cached) return toPublic(cached);
const fetched = await fetchCoinIds([coinId]);
recordResults([coinId], fetched);
return toPublic(priceCache.get(coinId));
}
export async function getPricesForChains(chainIds) {
const result = new Map();
const wantedCoinIds = new Set();
const chainToCoin = new Map();
for (const chainId of chainIds) {
const coinId = getCoinGeckoId(chainId);
if (!coinId) {
result.set(chainId, null);
continue;
}
chainToCoin.set(chainId, coinId);
const cached = getCachedByCoinId(coinId);
if (cached) {
result.set(chainId, toPublic(cached));
} else {
wantedCoinIds.add(coinId);
}
}
if (wantedCoinIds.size > 0) {
const coinIds = [...wantedCoinIds];
const fetched = await fetchCoinIds(coinIds);
recordResults(coinIds, fetched);
}
for (const [chainId, coinId] of chainToCoin) {
if (result.has(chainId)) continue;
result.set(chainId, toPublic(priceCache.get(coinId)));
}
return result;
}
/**
* Warm the cache for all chainIds with a known CoinGecko mapping.
* Intended to be called once after data load so the first /chains request
* doesn't pay a CoinGecko round-trip on the hot path. Failures are silent —
* a cold cache falls back to per-request fetching with the same timeout.
*/
export async function prefetchAllPrices() {
const coinIds = [...new Set(Object.values(CHAIN_ID_TO_COINGECKO_ID))];
const fetched = await fetchCoinIds(coinIds);
recordResults(coinIds, fetched);
}
export function clearPriceCache() {
priceCache.clear();
inflight.clear();
}