forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceOracle.js
More file actions
417 lines (368 loc) · 13.1 KB
/
Copy pathpriceOracle.js
File metadata and controls
417 lines (368 loc) · 13.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const cache = require('./cache');
const stellarDex = require('./sources/stellarDex');
const coingecko = require('./sources/coingecko');
const coinmarketcap = require('./sources/coinmarketcap');
const config = require('../config');
const logger = require('../logger');
const { CircuitBreaker } = require('../utils/circuitBreaker');
const CACHE_PREFIX = 'price:';
const HISTORY_PREFIX = 'price:history:';
const breakerOptions = config.price.circuitBreaker;
// In-flight de-duplication map: key -> Promise. Prevents concurrent getPrice/
// fetchFreshPrice calls for the same asset from each independently hitting all
// upstream sources (CoinGecko, CoinMarketCap, Stellar DEX) on a cache miss.
const inFlight = new Map();
const ALL_SOURCES = [
{
name: 'stellar_dex',
fetch: stellarDex.fetchPrice,
isSupported: stellarDex.isSupported,
breaker: new CircuitBreaker('stellar_dex', breakerOptions),
},
{
name: 'coingecko',
fetch: coingecko.fetchPrice,
isSupported: coingecko.isSupported,
breaker: new CircuitBreaker('coingecko', breakerOptions),
getCircuitState: coingecko.getCircuitState,
},
{
name: 'coinmarketcap',
fetch: coinmarketcap.fetchPrice,
isSupported: coinmarketcap.isSupported,
breaker: new CircuitBreaker('coinmarketcap', breakerOptions),
getCircuitState: coinmarketcap.getCircuitState,
},
];
function sortByPriority(sources, priority) {
if (!priority || priority.length === 0) return sources;
const order = new Map(priority.map((name, i) => [name, i]));
return [...sources].sort((a, b) => {
const ia = order.has(a.name) ? order.get(a.name) : Infinity;
const ib = order.has(b.name) ? order.get(b.name) : Infinity;
return ia - ib;
});
}
const SOURCES = sortByPriority(ALL_SOURCES, config.price.sourcePriority);
/**
* Circuit-breaker state for every source that has one (currently coingecko
* and coinmarketcap — stellar_dex has no API-key/auth failure mode). Lets
* callers (e.g. /health) see at a glance which price sources are currently
* skipped due to a nonRetryable failure. See #95.
*/
function getSourceCircuitStates() {
return SOURCES.filter((source) => typeof source.getCircuitState === 'function').map((source) =>
source.getCircuitState()
);
}
function median(values) {
if (values.length === 0) return null;
const sorted = [...values].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
return (sorted[mid - 1] + sorted[mid]) / 2;
}
return sorted[mid];
}
function buildCacheKey(assetCode, issuer) {
if (!issuer) return `${CACHE_PREFIX}${assetCode}`;
return `${CACHE_PREFIX}${assetCode}:${issuer}`;
}
function buildHistoryKey(assetCode, issuer) {
if (!issuer) return `${HISTORY_PREFIX}${assetCode}`;
return `${HISTORY_PREFIX}${assetCode}:${issuer}`;
}
async function detectAnomaly(currentPrice, assetCode, issuer) {
const historyKey = buildHistoryKey(assetCode, issuer);
let history = null;
try {
history = await cache.get(historyKey);
} catch (err) {
logger.warn('Cache read failed in anomaly detection, skipping', { error: err.message });
return { anomalous: false, changePercent: 0 };
}
if (!history || !history.price || history.price <= 0) {
try {
await cache.set(historyKey, { price: currentPrice, timestamp: Date.now() }, 3600);
} catch (err) {
logger.warn('Cache write failed in anomaly detection', { error: err.message });
}
return { anomalous: false, changePercent: 0 };
}
const changePercent = Math.abs((currentPrice - history.price) / history.price) * 100;
const anomalous = changePercent > config.price.anomalyThresholdPercent;
if (anomalous) {
logger.warn('Price anomaly detected', {
assetCode,
issuer,
previousPrice: history.price,
currentPrice,
changePercent: changePercent.toFixed(2),
});
}
try {
await cache.set(historyKey, { price: currentPrice, timestamp: Date.now() }, 3600);
} catch (err) {
logger.warn('Cache write failed in anomaly detection', { error: err.message });
}
return { anomalous, changePercent };
}
async function fetchFromAllSources(assetCode, issuer) {
const results = [];
for (const source of SOURCES) {
// A source that cannot serve this asset at all (e.g. CoinGecko has no
// mapping for a non-XLM asset) is a permanent, per-asset condition, not
// a source failure — skip the breaker-wrapped call entirely so it never
// counts toward that source's failure threshold. Without this, a
// source asked about even one asset it doesn't support would eventually
// trip its circuit open for every asset it *does* support (#130).
if (typeof source.isSupported === 'function' && !source.isSupported(assetCode, issuer)) {
continue;
}
try {
const price = await source.breaker.call(() => source.fetch(assetCode, issuer));
if (price !== null && price > 0) {
results.push({ source: source.name, price });
}
} catch (err) {
logger.warn('Source fetch failed', { source: source.name, assetCode, error: err.message });
}
}
return results;
}
function getCircuitStates() {
return SOURCES.reduce((states, source) => {
states[source.name] = source.breaker.getState();
return states;
}, {});
}
function resetCircuitBreakers() {
for (const source of SOURCES) {
source.breaker.reset();
}
}
const QUERIED_ASSETS_KEY = 'queried_assets';
async function recordQueriedAsset(assetCode, issuer = null) {
try {
if (!cache.isConnected()) return;
const redis = cache.getClient();
const key = issuer ? `${assetCode}:${issuer}` : assetCode;
await redis.sadd(QUERIED_ASSETS_KEY, key);
} catch (err) {
logger.warn('Failed to record queried asset', { assetCode, issuer, error: err.message });
}
}
async function getQueriedAssets() {
try {
if (!cache.isConnected()) return [];
const redis = cache.getClient();
const members = await redis.smembers(QUERIED_ASSETS_KEY);
return (members || []).map((entry) => {
const [code, issuer] = entry.split(':');
return { code, issuer: issuer || null };
});
} catch (err) {
logger.warn('Failed to fetch queried assets', { error: err.message });
return [];
}
}
async function getPrice(assetCode, issuer = null) {
recordQueriedAsset(assetCode, issuer);
const cacheKey = buildCacheKey(assetCode, issuer);
let redisUnavailable = false;
try {
const cached = await cache.get(cacheKey);
if (cached) {
const ageMs = Date.now() - cached.fetchedAt;
const ageMinutes = ageMs / 60000;
const isStale = ageMinutes > config.price.staleThresholdMinutes;
return {
asset_code: assetCode,
issuer: issuer || null,
price_usd: cached.price,
source: cached.source,
fetched_at: new Date(cached.fetchedAt).toISOString(),
is_stale: isStale,
stale_warning: isStale
? `Price is ${ageMinutes.toFixed(1)} minutes old (threshold: ${config.price.staleThresholdMinutes} min)`
: null,
sources_attempted: cached.sourcesAttempted || [],
redis_unavailable: false,
};
}
} catch (err) {
logger.warn('Cache read failed, falling back to source fetch', { error: err.message });
redisUnavailable = true;
}
return fetchFreshPrice(assetCode, issuer, redisUnavailable);
}
async function doFetchFreshPrice(assetCode, issuer = null, redisUnavailable = false) {
const sourceResults = await fetchFromAllSources(assetCode, issuer);
const sourcesAttempted = sourceResults.map((r) => r.source);
const prices = sourceResults.map((r) => r.price);
const quorumMet = sourcesAttempted.length >= config.price.minSources;
const aggregatedPrice = median(prices);
if (aggregatedPrice === null) {
logger.warn('No price sources available', { assetCode, issuer });
return {
asset_code: assetCode,
issuer: issuer || null,
price_usd: null,
source: 'unavailable',
fetched_at: new Date().toISOString(),
is_stale: true,
stale_warning: 'No price data available from any source',
sources_attempted: sourcesAttempted,
redis_unavailable: redisUnavailable,
quorum_met: false,
anomalous: false,
};
}
const primarySource = sourceResults.length > 0 ? sourceResults[0].source : 'aggregated';
// Anomaly detection — quorum_met is computed from source count, independent of Redis
let anomalous = false;
if (!redisUnavailable) {
const anomalyResult = await detectAnomaly(aggregatedPrice, assetCode, issuer);
anomalous = anomalyResult.anomalous;
}
// In reject mode, fall back to last known good cached price for anomalous readings
if (anomalous && config.price.anomalyAction === 'reject' && !redisUnavailable) {
try {
const cacheKey = buildCacheKey(assetCode, issuer);
const cached = await cache.get(cacheKey);
if (cached && cached.price) {
logger.warn('Anomalous price rejected, returning cached price', {
assetCode,
issuer,
rejectedPrice: aggregatedPrice,
cachedPrice: cached.price,
});
return {
asset_code: assetCode,
issuer: issuer || null,
price_usd: cached.price,
source: cached.source || 'cached',
fetched_at: new Date(cached.fetchedAt).toISOString(),
is_stale: true,
stale_warning: 'Anomalous price rejected — returning last known good price',
sources_attempted: sourcesAttempted,
redis_unavailable: redisUnavailable,
quorum_met: quorumMet,
anomalous: true,
};
}
} catch (err) {
logger.warn('Cache read failed during anomaly rejection, serving fetched price', { error: err.message });
}
}
if (!redisUnavailable) {
try {
const cacheKey = buildCacheKey(assetCode, issuer);
await cache.set(
cacheKey,
{
price: aggregatedPrice,
source: primarySource,
fetchedAt: Date.now(),
sourcesAttempted,
},
config.price.cacheTtl
);
} catch (err) {
logger.warn('Cache write failed, continuing without caching', { error: err.message });
redisUnavailable = true;
}
}
return {
asset_code: assetCode,
issuer: issuer || null,
price_usd: aggregatedPrice,
source: primarySource,
fetched_at: new Date().toISOString(),
is_stale: false,
stale_warning: null,
sources_attempted: sourcesAttempted,
redis_unavailable: redisUnavailable,
quorum_met: quorumMet,
anomalous,
};
}
/**
* Single-flight wrapper around doFetchFreshPrice. Concurrent calls for the
* same assetCode:issuer pair while a fetch is already in-flight will await
* the same promise instead of each independently hitting all upstream sources.
*/
async function fetchFreshPrice(assetCode, issuer = null, redisUnavailable = false) {
const normalisedIssuer = issuer || null;
const key = `${assetCode}:${normalisedIssuer}`;
if (inFlight.has(key)) {
const existing = inFlight.get(key);
const coalescedCount = inFlight.size;
logger.debug('Coalesced caller onto in-flight price fetch', {
assetCode,
issuer: normalisedIssuer,
coalesced: coalescedCount,
});
return existing;
}
const promise = doFetchFreshPrice(assetCode, issuer, redisUnavailable)
.finally(() => inFlight.delete(key));
inFlight.set(key, promise);
return promise;
}
async function refreshAllCachedPrices() {
if (!cache.isConnected()) {
logger.warn('Redis unavailable, skipping scheduled price refresh cycle');
return;
}
const redis = cache.getClient();
const keys = [];
let cursor = '0';
try {
do {
const result = await redis.scan(cursor, 'MATCH', `${CACHE_PREFIX}*`, 'COUNT', 100);
cursor = result[0];
keys.push(...result[1]);
} while (cursor !== '0');
} catch (err) {
logger.warn('Redis scan failed during price refresh, aborting cycle', { error: err.message });
return;
}
const freshPrices = {};
const refreshPromises = keys
.filter((key) => !key.includes(':history:'))
.map(async (key) => {
const suffix = key.replace(CACHE_PREFIX, '');
const parts = suffix.split(':');
const assetCode = parts[0];
const issuer = parts.length > 1 ? parts[1] : null;
const assetKey = issuer ? `${assetCode}:${issuer}` : assetCode;
try {
const result = await fetchFreshPrice(assetCode, issuer);
if (result && result.price_usd !== null) {
freshPrices[assetKey] = { price: result.price_usd, source: result.source };
}
logger.debug('Refreshed price', { assetCode, issuer });
} catch (err) {
logger.warn('Price refresh failed', { assetCode, issuer, error: err.message });
}
});
await Promise.allSettled(refreshPromises);
logger.info('Price refresh cycle completed', { keysRefreshed: keys.length });
return freshPrices;
}
module.exports = {
getPrice,
fetchFreshPrice,
getCircuitStates,
resetCircuitBreakers,
refreshAllCachedPrices,
getQueriedAssets,
recordQueriedAsset,
// Internal helpers exported for unit testing.
median,
detectAnomaly,
fetchFromAllSources,
getSourceCircuitStates,
inFlight,
};