forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarDex.js
More file actions
98 lines (82 loc) · 2.87 KB
/
Copy pathstellarDex.js
File metadata and controls
98 lines (82 loc) · 2.87 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
const { Asset, Horizon } = require('@stellar/stellar-sdk');
const config = require('../../config');
const logger = require('../../logger');
let server = null;
function getServer() {
if (!server) {
server = new Horizon.Server(config.stellar.horizonUrl);
}
return server;
}
function xlmAsset() {
return Asset.native();
}
function usdcAsset() {
return new Asset('USDC', config.stellar.usdcIssuer);
}
function issuedAsset(assetCode, issuer) {
return new Asset(assetCode, issuer);
}
function midpointFromOrderBook(orderBook) {
const bidPrice = orderBook.bids?.[0]?.price;
const askPrice = orderBook.asks?.[0]?.price;
const bestBid = bidPrice !== undefined ? parseFloat(bidPrice) : null;
const bestAsk = askPrice !== undefined ? parseFloat(askPrice) : null;
const hasBid = Number.isFinite(bestBid) && bestBid > 0;
const hasAsk = Number.isFinite(bestAsk) && bestAsk > 0;
if (hasBid && hasAsk) return (bestBid + bestAsk) / 2;
if (hasBid) return bestBid;
if (hasAsk) return bestAsk;
return null;
}
async function fetchOrderBookMidpoint(horizon, base, counter) {
const orderBook = await horizon.orderbook(base, counter).limit(1).call();
return midpointFromOrderBook(orderBook);
}
/**
* Whether this source can serve the given asset/issuer at all — distinct
* from a transient fetch failure. Callers (priceOracle's fetchFromAllSources)
* check this before ever invoking the circuit-breaker-wrapped fetchPrice, so
* an issued asset queried without an issuer never counts toward this
* source's failure threshold (#130). Matches fetchPrice's own
* issuer-required check exactly.
*/
function isSupported(assetCode, issuer = null) {
const normalizedCode = (assetCode || '').toUpperCase();
if (normalizedCode === 'XLM') return true;
return Boolean(issuer);
}
async function fetchPrice(assetCode, issuer) {
try {
const horizon = getServer();
const normalizedCode = assetCode.toUpperCase();
if (!issuer && normalizedCode !== 'XLM') {
logger.debug('Stellar DEX issuer required for issued asset', { assetCode });
return null;
}
if (normalizedCode === 'XLM') {
return await fetchOrderBookMidpoint(horizon, xlmAsset(), usdcAsset());
}
const assetInXlm = await fetchOrderBookMidpoint(
horizon,
issuedAsset(normalizedCode, issuer),
xlmAsset()
);
if (assetInXlm === null) return null;
const xlmUsd = await getXlmUsdPrice(horizon);
if (xlmUsd === null) return null;
return assetInXlm * xlmUsd;
} catch (err) {
logger.warn('Stellar DEX price fetch failed', { assetCode, issuer, error: err.message });
throw err;
}
}
async function getXlmUsdPrice(horizon) {
try {
return await fetchOrderBookMidpoint(horizon, xlmAsset(), usdcAsset());
} catch (err) {
logger.warn('XLM/USDC price fetch failed', { error: err.message });
throw err;
}
}
module.exports = { fetchPrice, isSupported };