dexbot 1.6.17 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/README.md +24 -18
- package/analysis/README.md +105 -12
- package/analysis/ama_fitting/README.md +2 -2
- package/analysis/bot_fitting/README.md +3 -3
- package/claw/package.json +1 -1
- package/claw/runtimes/openclaw-plugin/openclaw.plugin.json +1 -1
- package/claw/runtimes/openclaw-plugin/package.json +1 -1
- package/dist/analysis/analyze_args.d.ts +2 -1
- package/dist/analysis/analyze_dynamic_weight.js +2 -1
- package/dist/analysis/analyze_kalman.js +3 -2
- package/dist/analysis/analyze_liquidity.d.ts +17 -0
- package/dist/analysis/analyze_liquidity.js +325 -0
- package/dist/analysis/analyze_regime.js +3 -2
- package/dist/analysis/analyze_regime_windows.js +3 -2
- package/dist/analysis/analyze_volatility.js +3 -2
- package/dist/analysis/fills_source.d.ts +1 -8
- package/dist/analysis/fills_source.js +1 -1
- package/dist/analysis/grid_correction_check.d.ts +9 -6
- package/dist/analysis/grid_correction_check.js +56 -64
- package/dist/analysis/liquidity/liquidity_chain.d.ts +66 -0
- package/dist/analysis/liquidity/liquidity_chain.js +178 -0
- package/dist/analysis/liquidity/liquidity_chart_generator.d.ts +19 -0
- package/dist/analysis/liquidity/liquidity_chart_generator.js +523 -0
- package/dist/analysis/liquidity/liquidity_depth_model.d.ts +141 -0
- package/dist/analysis/liquidity/liquidity_depth_model.js +230 -0
- package/dist/analysis/liquidity/liquidity_view.d.ts +38 -0
- package/dist/analysis/liquidity/liquidity_view.js +55 -0
- package/dist/analysis/math_utils.d.ts +1 -1
- package/dist/analysis/trade_profitability.d.ts +1 -1
- package/dist/analysis/trade_profitability.js +2 -1
- package/dist/analysis/tradingview/analyze_tradingview.js +2 -1
- package/dist/analysis/tradingview/tradingview_uplot_chart_generator.js +3 -3
- package/dist/analysis/trend_detection/huber_scale_variants.d.ts +4 -3
- package/dist/dexbot.js +74 -169
- package/dist/market_adapter/cli_args.d.ts +6 -1
- package/dist/market_adapter/cli_args.js +16 -2
- package/dist/market_adapter/core/kibana_candles.d.ts +5 -1
- package/dist/market_adapter/core/kibana_candles.js +3 -1
- package/dist/market_adapter/core/strategies/regime_gate.d.ts +7 -1
- package/dist/market_adapter/core/strategies/regime_gate.js +3 -1
- package/dist/market_adapter/inputs/cex_candle_parsing.d.ts +7 -2
- package/dist/market_adapter/inputs/cex_candle_parsing.js +3 -1
- package/dist/market_adapter/inputs/fetch_book_data.d.ts +7 -1
- package/dist/market_adapter/inputs/fetch_book_data.js +3 -1
- package/dist/market_adapter/inputs/fetch_cex_synthetic_data.js +2 -11
- package/dist/market_adapter/inputs/fetch_lp_data.d.ts +26 -1
- package/dist/market_adapter/inputs/fetch_lp_data.js +62 -52
- package/dist/market_adapter/inputs/kibana_feed_source.d.ts +14 -2
- package/dist/market_adapter/inputs/kibana_feed_source.js +14 -1
- package/dist/market_adapter/inputs/window_cache.d.ts +14 -3
- package/dist/market_adapter/inputs/window_cache.js +14 -1
- package/dist/market_adapter/lp_chart_runner.d.ts +12 -9
- package/dist/market_adapter/lp_chart_runner.js +3 -1
- package/dist/market_adapter/lp_chart_strategy_loader.d.ts +5 -1
- package/dist/market_adapter/lp_chart_strategy_loader.js +3 -1
- package/dist/market_adapter/market_adapter.d.ts +10 -2
- package/dist/market_adapter/market_adapter.js +10 -11
- package/dist/market_adapter/utils/chain.d.ts +2 -3
- package/dist/market_adapter/utils/file_json_cache.d.ts +5 -1
- package/dist/market_adapter/utils/file_json_cache.js +3 -1
- package/dist/market_adapter/utils/file_lock.js +1 -1
- package/dist/market_adapter/utils/native_history.d.ts +7 -3
- package/dist/market_adapter/utils/native_history.js +3 -1
- package/dist/market_adapter/utils/pair_orientation.d.ts +2 -2
- package/dist/modules/asset_lookup.d.ts +25 -0
- package/dist/modules/asset_lookup.js +75 -0
- package/dist/modules/bots_file_lock.d.ts +2 -2
- package/dist/modules/bots_file_lock.js +2 -2
- package/dist/modules/chain_scan.d.ts +64 -0
- package/dist/modules/chain_scan.js +80 -0
- package/dist/modules/cli_colors.d.ts +5 -0
- package/dist/modules/cli_colors.js +7 -0
- package/dist/modules/cli_progress.d.ts +51 -0
- package/dist/modules/cli_progress.js +113 -0
- package/dist/modules/constants.js +6 -3
- package/dist/modules/dexbot_class.d.ts +8 -0
- package/dist/modules/dexbot_class.js +10 -0
- package/dist/modules/dexbot_cow_runtime.d.ts +62 -1
- package/dist/modules/dexbot_cow_runtime.js +475 -47
- package/dist/modules/dexbot_credential_client.d.ts +5 -0
- package/dist/modules/dexbot_fill_runtime.js +20 -0
- package/dist/modules/dexbot_maintenance_runtime.js +71 -9
- package/dist/modules/dexbot_startup_runtime.js +7 -1
- package/dist/modules/graceful_shutdown.d.ts +2 -2
- package/dist/modules/graceful_shutdown.js +2 -2
- package/dist/modules/launcher/runtime_entry.d.ts +21 -1
- package/dist/modules/launcher/runtime_entry.js +36 -1
- package/dist/modules/launcher/runtime_mode.d.ts +4 -3
- package/dist/modules/liquidity_pool_ref.d.ts +32 -0
- package/dist/modules/liquidity_pool_ref.js +116 -0
- package/dist/modules/node_manager.d.ts +1 -1
- package/dist/modules/node_manager.js +1 -1
- package/dist/modules/order/accounting.js +2 -1
- package/dist/modules/order/export.d.ts +2 -2
- package/dist/modules/order/export.js +2 -2
- package/dist/modules/order/grid.d.ts +19 -14
- package/dist/modules/order/grid.js +269 -61
- package/dist/modules/order/grid_reconcile.js +27 -2
- package/dist/modules/order/grid_reconcile_internal.js +94 -15
- package/dist/modules/order/index.d.ts +24 -4
- package/dist/modules/order/manager.d.ts +12 -0
- package/dist/modules/order/manager.js +44 -11
- package/dist/modules/order/strategy.js +22 -2
- package/dist/modules/order/sync_engine.d.ts +32 -1
- package/dist/modules/order/sync_engine.js +209 -26
- package/dist/modules/order/utils/math.d.ts +26 -10
- package/dist/modules/order/utils/math.js +54 -14
- package/dist/modules/order/utils/order.d.ts +158 -11
- package/dist/modules/order/utils/order.js +434 -21
- package/dist/modules/order/utils/system.d.ts +136 -30
- package/dist/modules/order/utils/system.js +465 -63
- package/dist/modules/order/utils/validate.d.ts +1 -0
- package/dist/modules/order/utils/validate.js +37 -3
- package/dist/modules/storage/index.d.ts +3 -2
- package/dist/modules/storage/index.js +3 -2
- package/dist/modules/types.d.ts +14 -0
- package/dist/modules/utils/asset_symbols.d.ts +13 -2
- package/dist/modules/utils/asset_symbols.js +21 -2
- package/dist/scripts/analysis_runner.d.ts +10 -0
- package/dist/scripts/analysis_runner.js +33 -0
- package/dist/scripts/analyze-orders.js +2 -1
- package/dist/scripts/asset.d.ts +26 -0
- package/dist/scripts/asset.js +165 -0
- package/dist/scripts/bts_distribution.d.ts +1 -0
- package/dist/scripts/bts_distribution.js +194 -142
- package/dist/scripts/chart_command.d.ts +1 -1
- package/dist/scripts/chart_command.js +60 -86
- package/dist/scripts/chart_targets.d.ts +18 -0
- package/dist/scripts/chart_targets.js +52 -0
- package/dist/scripts/dw.js +1 -1
- package/dist/scripts/liquid.d.ts +24 -0
- package/dist/scripts/liquid.js +134 -0
- package/dist/scripts/rich_list.js +12 -16
- package/dist/scripts/token_distribution.d.ts +2 -0
- package/dist/scripts/token_distribution.js +132 -108
- package/dist/scripts/tv.js +1 -1
- package/dist/scripts/verify-browser-bundle.js +18 -0
- package/docs/COPY_ON_WRITE_MASTER_PLAN.md +1 -1
- package/docs/COW_INVARIANTS.md +21 -11
- package/docs/DEXBOT_COMPARISON.md +6 -6
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +11 -11
- package/docs/GRID_PRICE_INVARIANT.md +28 -5
- package/docs/GRID_RECONCILE.md +25 -5
- package/docs/LIFECYCLE.md +11 -11
- package/docs/LOGGING.md +37 -2
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +124 -36
- package/docs/README.md +1 -1
- package/docs/WORKFLOW.md +3 -1
- package/docs/architecture.md +115 -13
- package/docs/developer_guide.md +12 -0
- package/market_adapter/README.md +9 -4
- package/modules/README.md +23 -15
- package/package.json +6 -3
- package/scripts/README.md +23 -7
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* LIQUIDITY DEPTH CHART (uPlot)
|
|
4
|
+
*
|
|
5
|
+
* Renders one asset pair's liquidity: cumulative order-book depth and the
|
|
6
|
+
* constant-product AMM pool curve, overlaid side by side on the same price
|
|
7
|
+
* axis. The two sources are shown as independent curves (never summed), so the
|
|
8
|
+
* trader can see which one actually supplies the depth at a given price. The
|
|
9
|
+
* visual model follows PoolTool's depth chart, rebuilt with the vendored uPlot
|
|
10
|
+
* runtime used by every other DEXBot2 chart exporter.
|
|
11
|
+
*
|
|
12
|
+
* The page is fully self-contained (uPlot JS+CSS inlined) and needs no network
|
|
13
|
+
* access once written.
|
|
14
|
+
*/
|
|
15
|
+
import { embedFunctionSources, escapeHtml, serializeJsonForScript, uplotInlineTags } from '../chart_utils.js';
|
|
16
|
+
import { sharedChartCSS } from '../chart_css.js';
|
|
17
|
+
import { formatCurrency, formatFundsValue, isValidNumber, safeFormat } from '../../modules/order/format.js';
|
|
18
|
+
import { buildDepthView } from './liquidity_view.js';
|
|
19
|
+
const COLORS = {
|
|
20
|
+
bidBook: '#3fb950',
|
|
21
|
+
askBook: '#f85149',
|
|
22
|
+
bidPool: '#58a6ff',
|
|
23
|
+
askPool: '#d2a8ff',
|
|
24
|
+
};
|
|
25
|
+
function generateLiquidityHTML(input, title) {
|
|
26
|
+
const { base, quote, pool, book, series, window } = input;
|
|
27
|
+
const poolPrice = series.poolPrice;
|
|
28
|
+
const pageTitle = title || `Liquidity Depth — ${base.symbol} / ${quote.symbol}`;
|
|
29
|
+
const priceLabel = `${quote.symbol} per ${base.symbol}`;
|
|
30
|
+
const depthLabel = quote.symbol;
|
|
31
|
+
const payload = {
|
|
32
|
+
base: {
|
|
33
|
+
symbol: base.symbol,
|
|
34
|
+
id: base.id ?? null,
|
|
35
|
+
precision: base.precision ?? null,
|
|
36
|
+
},
|
|
37
|
+
quote: {
|
|
38
|
+
symbol: quote.symbol,
|
|
39
|
+
id: quote.id ?? null,
|
|
40
|
+
precision: quote.precision ?? null,
|
|
41
|
+
},
|
|
42
|
+
pool: pool
|
|
43
|
+
? {
|
|
44
|
+
id: pool.id ?? null,
|
|
45
|
+
shareAsset: pool.shareAsset ?? null,
|
|
46
|
+
baseReserve: pool.baseReserve,
|
|
47
|
+
quoteReserve: pool.quoteReserve,
|
|
48
|
+
feePercent: pool.feePercent ?? null,
|
|
49
|
+
price: poolPrice,
|
|
50
|
+
}
|
|
51
|
+
: null,
|
|
52
|
+
book: {
|
|
53
|
+
bestBid: book.bestBid,
|
|
54
|
+
bestAsk: book.bestAsk,
|
|
55
|
+
mid: book.mid,
|
|
56
|
+
spreadPercent: book.spreadPercent,
|
|
57
|
+
bidOrders: book.bidOrders,
|
|
58
|
+
askOrders: book.askOrders,
|
|
59
|
+
bidQuoteTotal: book.bidQuoteTotal,
|
|
60
|
+
askQuoteTotal: book.askQuoteTotal,
|
|
61
|
+
},
|
|
62
|
+
series,
|
|
63
|
+
window,
|
|
64
|
+
priceLabel,
|
|
65
|
+
depthLabel,
|
|
66
|
+
generatedAt: input.generatedAt ?? new Date().toISOString(),
|
|
67
|
+
colors: COLORS,
|
|
68
|
+
};
|
|
69
|
+
const poolBadge = pool
|
|
70
|
+
? `<span class="badge badge-pool">POOL ${escapeHtml(pool.id ?? 'unknown')}</span>
|
|
71
|
+
<span class="badge" id="badge-pool-price">${escapeHtml(formatCurrency(poolPrice))} ${escapeHtml(priceLabel)}</span>`
|
|
72
|
+
: `<span class="badge badge-muted">NO AMM POOL</span>`;
|
|
73
|
+
const midBadge = book.mid != null
|
|
74
|
+
? `<span class="badge" id="badge-mid">BOOK MID ${escapeHtml(formatCurrency(book.mid))}</span>`
|
|
75
|
+
: `<span class="badge badge-muted">ONE-SIDED BOOK</span>`;
|
|
76
|
+
const spreadBadge = book.spreadPercent != null
|
|
77
|
+
? `<span class="badge">SPREAD ${escapeHtml(formatCurrency(book.spreadPercent))}%</span>`
|
|
78
|
+
: '';
|
|
79
|
+
const windowBadge = `<span class="badge badge-muted">±${escapeHtml(formatCurrency(((window.max - window.min) / (2 * (poolPrice ?? book.mid ?? window.min))) * 100))}% WINDOW</span>`;
|
|
80
|
+
return `<!DOCTYPE html>
|
|
81
|
+
<html lang="en">
|
|
82
|
+
<head>
|
|
83
|
+
<meta charset="UTF-8">
|
|
84
|
+
<title>${escapeHtml(pageTitle)}</title>
|
|
85
|
+
${uplotInlineTags()}
|
|
86
|
+
<style>
|
|
87
|
+
${sharedChartCSS()}
|
|
88
|
+
#header { height: 64px; }
|
|
89
|
+
#panels { height: calc(100vh - 64px); }
|
|
90
|
+
#liquidity-panel { flex: 1 1 auto; min-height: 0; position: relative; }
|
|
91
|
+
#liquidity-chart { position: absolute; inset: 0; }
|
|
92
|
+
.badge { display: inline-block; padding: 2px 8px; border-radius: 3px; font-size: 10px; font-weight: 600;
|
|
93
|
+
letter-spacing: 0.4px; background: rgba(110,118,129,0.16); color: #adbac7; white-space: nowrap; }
|
|
94
|
+
.badge-pool { background: rgba(88,166,255,0.18); color: #79c0ff; }
|
|
95
|
+
.badge-muted { color: #6e7681; }
|
|
96
|
+
.readout { position: absolute; top: 10px; left: 12px; z-index: 10; pointer-events: none;
|
|
97
|
+
background: rgba(11,14,20,0.82); border: 1px solid #21262d; border-radius: 6px;
|
|
98
|
+
padding: 7px 10px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
99
|
+
font-size: 13px; color: #adbac7; line-height: 1.45; white-space: nowrap; }
|
|
100
|
+
.readout .price { color: #e6edf3; font-size: 15px; font-weight: 600; }
|
|
101
|
+
.readout table { border-collapse: collapse; margin-top: 3px; }
|
|
102
|
+
.readout th { font-weight: 600; color: #6e7681; padding: 0 10px 0 0; text-align: right; }
|
|
103
|
+
.readout th.side { text-align: left; }
|
|
104
|
+
.readout td { padding: 0 10px 0 0; text-align: right; }
|
|
105
|
+
.readout td.side-bid { color: ${COLORS.bidBook}; text-align: left; }
|
|
106
|
+
.readout td.side-ask { color: ${COLORS.askBook}; text-align: left; }
|
|
107
|
+
.hint { font-size: 12px; color: #8b949e; white-space: nowrap; }
|
|
108
|
+
.legend-swatch { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
|
|
109
|
+
.legend-item { display: inline-flex; align-items: center; gap: 5px; margin-left: 8px; vertical-align: middle; }
|
|
110
|
+
</style>
|
|
111
|
+
</head>
|
|
112
|
+
<body>
|
|
113
|
+
<div id="header">
|
|
114
|
+
<div style="display:flex;flex-direction:column;gap:3px;">
|
|
115
|
+
<div style="font-weight:bold;color:#fff;font-size:13px;">${escapeHtml(pageTitle)}</div>
|
|
116
|
+
<div style="display:flex;gap:6px;align-items:center;flex-wrap:wrap;">
|
|
117
|
+
${poolBadge}
|
|
118
|
+
${midBadge}
|
|
119
|
+
${spreadBadge}
|
|
120
|
+
${windowBadge}
|
|
121
|
+
<span class="badge badge-muted">${escapeHtml(String(book.bidOrders))} BID / ${escapeHtml(String(book.askOrders))} ASK ORDERS</span>
|
|
122
|
+
</div>
|
|
123
|
+
</div>
|
|
124
|
+
<div style="display:flex;flex-direction:column;gap:3px;align-items:flex-end;">
|
|
125
|
+
<div class="hint">Scroll = zoom · Drag = pan · Double-click = reset</div>
|
|
126
|
+
<div class="hint">
|
|
127
|
+
<span class="legend-item"><span class="legend-swatch" style="background:${COLORS.bidBook};"></span>book bids</span>
|
|
128
|
+
<span class="legend-item"><span class="legend-swatch" style="background:${COLORS.askBook};"></span>book asks</span>
|
|
129
|
+
<span class="legend-item"><span class="legend-swatch" style="background:${COLORS.bidPool};"></span>pool bids</span>
|
|
130
|
+
<span class="legend-item"><span class="legend-swatch" style="background:${COLORS.askPool};"></span>pool asks</span>
|
|
131
|
+
</div>
|
|
132
|
+
</div>
|
|
133
|
+
</div>
|
|
134
|
+
|
|
135
|
+
<div id="panels">
|
|
136
|
+
<div id="liquidity-panel">
|
|
137
|
+
<div class="readout" id="readout">
|
|
138
|
+
<div>Price <span class="price" id="r-price">-</span> <span id="r-unit">${escapeHtml(priceLabel)}</span></div>
|
|
139
|
+
<table>
|
|
140
|
+
<thead><tr><th class="side"></th><th>book</th><th>pool</th></tr></thead>
|
|
141
|
+
<tbody>
|
|
142
|
+
<tr>
|
|
143
|
+
<td class="side-bid">BIDS</td>
|
|
144
|
+
<td id="r-book-bid" style="color:${COLORS.bidBook};">-</td>
|
|
145
|
+
<td id="r-pool-bid" style="color:${COLORS.bidPool};">-</td>
|
|
146
|
+
</tr>
|
|
147
|
+
<tr>
|
|
148
|
+
<td class="side-ask">ASKS</td>
|
|
149
|
+
<td id="r-book-ask" style="color:${COLORS.askBook};">-</td>
|
|
150
|
+
<td id="r-pool-ask" style="color:${COLORS.askPool};">-</td>
|
|
151
|
+
</tr>
|
|
152
|
+
</tbody>
|
|
153
|
+
</table>
|
|
154
|
+
<div style="color:#6e7681;font-size:11px;margin-top:2px;">depth in <span id="r-depth-unit">${escapeHtml(depthLabel)}</span> · click axes: y=depth asset, x=invert price</div>
|
|
155
|
+
</div>
|
|
156
|
+
<div id="liquidity-chart"></div>
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<script id="payload" type="application/json">${serializeJsonForScript(payload)}</script>
|
|
161
|
+
|
|
162
|
+
<script>
|
|
163
|
+
(function () {
|
|
164
|
+
const data = JSON.parse(document.getElementById('payload').textContent);
|
|
165
|
+
${embedFunctionSources([buildDepthView, isValidNumber, safeFormat, formatCurrency, formatFundsValue])}
|
|
166
|
+
const fmtPrice = formatCurrency;
|
|
167
|
+
const fmtDepth = formatFundsValue;
|
|
168
|
+
const colors = data.colors;
|
|
169
|
+
const series = data.series;
|
|
170
|
+
const quoteUnit = data.quote.symbol;
|
|
171
|
+
const baseUnit = data.base.symbol;
|
|
172
|
+
|
|
173
|
+
// orientation: 'normal' = quote per base, 'inverted' = base per quote.
|
|
174
|
+
// depthMode is relative to the current orientation: 'quote' means the
|
|
175
|
+
// current quote asset, 'base' the current base asset.
|
|
176
|
+
let orientation = 'normal';
|
|
177
|
+
let depthMode = 'quote';
|
|
178
|
+
let view = null;
|
|
179
|
+
const guides = [];
|
|
180
|
+
let xMin = data.window.min;
|
|
181
|
+
let xMax = data.window.max;
|
|
182
|
+
|
|
183
|
+
function currentPriceUnit() {
|
|
184
|
+
return orientation === 'normal' ? data.priceLabel : baseUnit + ' per ' + quoteUnit;
|
|
185
|
+
}
|
|
186
|
+
function currentDepthUnit() {
|
|
187
|
+
if (orientation === 'normal') return depthMode === 'base' ? baseUnit : quoteUnit;
|
|
188
|
+
return depthMode === 'base' ? quoteUnit : baseUnit;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Build the currently displayed arrays. The transform is shared with
|
|
192
|
+
// the Node tests (liquidity_view.ts) and embedded verbatim above.
|
|
193
|
+
function buildView() {
|
|
194
|
+
return buildDepthView(series, {
|
|
195
|
+
orientation: orientation,
|
|
196
|
+
depthMode: depthMode,
|
|
197
|
+
poolPrice: data.pool ? data.pool.price : null,
|
|
198
|
+
mid: data.book.mid,
|
|
199
|
+
window: data.window,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
function currentChartData() {
|
|
203
|
+
return [view.prices, view.poolBid, view.poolAsk, view.bookBid, view.bookAsk];
|
|
204
|
+
}
|
|
205
|
+
function rebuildGuides() {
|
|
206
|
+
guides.length = 0;
|
|
207
|
+
if (view.poolPrice != null) {
|
|
208
|
+
guides.push({ price: view.poolPrice, color: 'rgba(230,237,243,0.55)', dash: [6, 4], label: 'POOL' });
|
|
209
|
+
}
|
|
210
|
+
if (view.mid != null) {
|
|
211
|
+
guides.push({ price: view.mid, color: 'rgba(139,148,158,0.65)', dash: [2, 4], label: 'BOOK MID' });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
view = buildView();
|
|
215
|
+
rebuildGuides();
|
|
216
|
+
xMin = view.min;
|
|
217
|
+
xMax = view.max;
|
|
218
|
+
|
|
219
|
+
// ── formatting ──────────────────────────────────────────────────────
|
|
220
|
+
// 4-significant-digit formatting comes from modules/order/format.ts
|
|
221
|
+
// (formatCurrency / formatFundsValue), embedded above so generated
|
|
222
|
+
// charts run the same formatters as DEXBot's order logs.
|
|
223
|
+
|
|
224
|
+
// ── view range (zoom / pan) ─────────────────────────────────────────
|
|
225
|
+
let pendingRange = null, pendingRangeRaf = 0;
|
|
226
|
+
function currentX(chart) {
|
|
227
|
+
const s = chart.scales.x || {};
|
|
228
|
+
return {
|
|
229
|
+
min: Number.isFinite(s.min) ? s.min : xMin,
|
|
230
|
+
max: Number.isFinite(s.max) ? s.max : xMax,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function clampXRange(min, max) {
|
|
234
|
+
const full = xMax - xMin;
|
|
235
|
+
const minSpan = full * 0.02;
|
|
236
|
+
let span = max - min;
|
|
237
|
+
if (!Number.isFinite(span) || span <= 0) return { min: xMin, max: xMax };
|
|
238
|
+
span = Math.max(minSpan, Math.min(full, span));
|
|
239
|
+
let nextMin = min;
|
|
240
|
+
let nextMax = min + span;
|
|
241
|
+
if (nextMin < xMin) { nextMin = xMin; nextMax = xMin + span; }
|
|
242
|
+
if (nextMax > xMax) { nextMax = xMax; nextMin = xMax - span; }
|
|
243
|
+
return { min: nextMin, max: nextMax };
|
|
244
|
+
}
|
|
245
|
+
function applyXRange(chart, min, max) {
|
|
246
|
+
pendingRange = clampXRange(min, max);
|
|
247
|
+
if (pendingRangeRaf) return;
|
|
248
|
+
const raf = typeof requestAnimationFrame !== 'undefined'
|
|
249
|
+
? requestAnimationFrame.bind(window)
|
|
250
|
+
: (fn) => setTimeout(fn, 0);
|
|
251
|
+
pendingRangeRaf = raf(() => {
|
|
252
|
+
const next = pendingRange;
|
|
253
|
+
pendingRange = null;
|
|
254
|
+
pendingRangeRaf = 0;
|
|
255
|
+
if (next) chart.setScale('x', next);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ── guide-line draw hook (pool price + book mid) ────────────────────
|
|
260
|
+
function drawGuides(u) {
|
|
261
|
+
const { ctx, bbox } = u;
|
|
262
|
+
const xScale = u.scales.x;
|
|
263
|
+
if (!xScale || !Number.isFinite(xScale.min) || !Number.isFinite(xScale.max)) return;
|
|
264
|
+
ctx.save();
|
|
265
|
+
ctx.beginPath();
|
|
266
|
+
ctx.rect(bbox.left, bbox.top, bbox.width, bbox.height);
|
|
267
|
+
ctx.clip();
|
|
268
|
+
for (let i = 0; i < guides.length; i++) {
|
|
269
|
+
const guide = guides[i];
|
|
270
|
+
if (guide.price < xScale.min || guide.price > xScale.max) continue;
|
|
271
|
+
const x = u.valToPos(guide.price, 'x', true);
|
|
272
|
+
ctx.strokeStyle = guide.color;
|
|
273
|
+
ctx.lineWidth = 1;
|
|
274
|
+
ctx.setLineDash(guide.dash);
|
|
275
|
+
ctx.beginPath();
|
|
276
|
+
ctx.moveTo(x, bbox.top);
|
|
277
|
+
ctx.lineTo(x, bbox.top + bbox.height);
|
|
278
|
+
ctx.stroke();
|
|
279
|
+
ctx.setLineDash([]);
|
|
280
|
+
const alignRight = x > bbox.left + bbox.width / 2;
|
|
281
|
+
// Stagger labels vertically so near-identical guide prices
|
|
282
|
+
// (pool vs book mid) never overprint each other.
|
|
283
|
+
const labelY = bbox.top + 14 + (i % 2) * 13;
|
|
284
|
+
ctx.fillStyle = guide.color;
|
|
285
|
+
ctx.font = '11px ui-monospace, SFMono-Regular, Menlo, monospace';
|
|
286
|
+
ctx.textAlign = alignRight ? 'right' : 'left';
|
|
287
|
+
ctx.fillText(guide.label + ' ' + fmtPrice(guide.price), x + (alignRight ? -6 : 6), labelY);
|
|
288
|
+
}
|
|
289
|
+
ctx.restore();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ── chart ───────────────────────────────────────────────────────────
|
|
293
|
+
const chartEl = document.getElementById('liquidity-panel');
|
|
294
|
+
const chart = new uPlot({
|
|
295
|
+
width: chartEl.offsetWidth,
|
|
296
|
+
height: chartEl.offsetHeight,
|
|
297
|
+
padding: [10, 44, 10, 8],
|
|
298
|
+
legend: { show: false },
|
|
299
|
+
select: { show: false },
|
|
300
|
+
cursor: {
|
|
301
|
+
show: true,
|
|
302
|
+
x: true,
|
|
303
|
+
y: false,
|
|
304
|
+
points: { show: false },
|
|
305
|
+
drag: { x: false, y: false, setScale: false },
|
|
306
|
+
focus: { prox: -1 },
|
|
307
|
+
},
|
|
308
|
+
scales: {
|
|
309
|
+
x: { time: false },
|
|
310
|
+
y: {
|
|
311
|
+
auto: true,
|
|
312
|
+
range: (u, min, max) => [0, Number.isFinite(max) && max > 0 ? max * 1.08 : 1],
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
series: [
|
|
316
|
+
{ label: 'Price' },
|
|
317
|
+
{ label: 'Pool bids', stroke: colors.bidPool, width: 1.5, fill: 'rgba(88,166,255,0.09)', points: { show: false } },
|
|
318
|
+
{ label: 'Pool asks', stroke: colors.askPool, width: 1.5, fill: 'rgba(210,168,255,0.09)', points: { show: false } },
|
|
319
|
+
{ label: 'Book bids', stroke: colors.bidBook, width: 1.6, dash: [4, 3], fill: 'rgba(63,185,80,0.10)', points: { show: false } },
|
|
320
|
+
{ label: 'Book asks', stroke: colors.askBook, width: 1.6, dash: [4, 3], fill: 'rgba(248,81,73,0.10)', points: { show: false } },
|
|
321
|
+
],
|
|
322
|
+
axes: [
|
|
323
|
+
{
|
|
324
|
+
stroke: '#ffffff',
|
|
325
|
+
grid: { stroke: '#1c2128' },
|
|
326
|
+
ticks: { stroke: '#30363d', width: 1 },
|
|
327
|
+
size: 34,
|
|
328
|
+
font: '13px Segoe UI, sans-serif',
|
|
329
|
+
label: 'Price (' + currentPriceUnit() + ')',
|
|
330
|
+
labelSize: 20,
|
|
331
|
+
labelFont: '12px Segoe UI, sans-serif',
|
|
332
|
+
values: (u, splits) => splits.map((v) => fmtPrice(v)),
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
side: 1,
|
|
336
|
+
stroke: '#ffffff',
|
|
337
|
+
grid: { stroke: '#1c2128', dash: [4, 4] },
|
|
338
|
+
ticks: { stroke: '#30363d', width: 1 },
|
|
339
|
+
size: 70,
|
|
340
|
+
font: '13px Segoe UI, sans-serif',
|
|
341
|
+
label: 'Cumulative depth (' + currentDepthUnit() + ')',
|
|
342
|
+
labelSize: 20,
|
|
343
|
+
labelFont: '12px Segoe UI, sans-serif',
|
|
344
|
+
values: (u, splits) => splits.map((v) => fmtDepth(v)),
|
|
345
|
+
},
|
|
346
|
+
],
|
|
347
|
+
hooks: { draw: [drawGuides] },
|
|
348
|
+
}, currentChartData(), document.getElementById('liquidity-chart'));
|
|
349
|
+
|
|
350
|
+
// ── cursor readout ──────────────────────────────────────────────────
|
|
351
|
+
function nearestIndex(price) {
|
|
352
|
+
const prices = view.prices;
|
|
353
|
+
let lo = 0, hi = prices.length - 1;
|
|
354
|
+
while (lo < hi) {
|
|
355
|
+
const mid = (lo + hi) >> 1;
|
|
356
|
+
if (prices[mid] < price) lo = mid + 1;
|
|
357
|
+
else hi = mid;
|
|
358
|
+
}
|
|
359
|
+
if (lo > 0 && Math.abs(prices[lo - 1] - price) <= Math.abs(prices[lo] - price)) return lo - 1;
|
|
360
|
+
return lo;
|
|
361
|
+
}
|
|
362
|
+
function setText(id, value) {
|
|
363
|
+
const el = document.getElementById(id);
|
|
364
|
+
if (el) el.textContent = value;
|
|
365
|
+
}
|
|
366
|
+
function updateReadout(idx) {
|
|
367
|
+
if (idx == null || idx < 0 || idx >= view.prices.length) {
|
|
368
|
+
const anchor = view.poolPrice != null ? view.poolPrice : (view.mid != null ? view.mid : view.min);
|
|
369
|
+
idx = nearestIndex(anchor);
|
|
370
|
+
}
|
|
371
|
+
setText('r-price', fmtPrice(view.prices[idx]));
|
|
372
|
+
setText('r-book-bid', fmtDepth(view.bookBid[idx]));
|
|
373
|
+
setText('r-pool-bid', fmtDepth(view.poolBid[idx]));
|
|
374
|
+
setText('r-book-ask', fmtDepth(view.bookAsk[idx]));
|
|
375
|
+
setText('r-pool-ask', fmtDepth(view.poolAsk[idx]));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function updateHeaderPrices() {
|
|
379
|
+
if (view.poolPrice != null) setText('badge-pool-price', fmtPrice(view.poolPrice) + ' ' + currentPriceUnit());
|
|
380
|
+
if (view.mid != null) setText('badge-mid', 'BOOK MID ' + fmtPrice(view.mid));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ── axis toggles (y = depth asset, x = invert price) ────────────────
|
|
384
|
+
function inYAxisZone(e) {
|
|
385
|
+
const over = chart.over && chart.over.getBoundingClientRect();
|
|
386
|
+
return !!over && e.clientX >= over.right - 2;
|
|
387
|
+
}
|
|
388
|
+
function inXAxisZone(e) {
|
|
389
|
+
const over = chart.over && chart.over.getBoundingClientRect();
|
|
390
|
+
return !!over && e.clientY >= over.bottom - 2;
|
|
391
|
+
}
|
|
392
|
+
function maxOf(arrays) {
|
|
393
|
+
let max = 0;
|
|
394
|
+
for (const arr of arrays) {
|
|
395
|
+
for (const v of arr) if (Number.isFinite(v) && v > max) max = v;
|
|
396
|
+
}
|
|
397
|
+
return max;
|
|
398
|
+
}
|
|
399
|
+
function applyView() {
|
|
400
|
+
view = buildView();
|
|
401
|
+
xMin = view.min;
|
|
402
|
+
xMax = view.max;
|
|
403
|
+
chart.setData(currentChartData());
|
|
404
|
+
chart.axes[0].label = 'Price (' + currentPriceUnit() + ')';
|
|
405
|
+
chart.axes[1].label = 'Cumulative depth (' + currentDepthUnit() + ')';
|
|
406
|
+
rebuildGuides();
|
|
407
|
+
// setSize re-lays out the axis labels; it also redraws the chart.
|
|
408
|
+
chart.setSize({ width: chartEl.offsetWidth, height: chartEl.offsetHeight });
|
|
409
|
+
const maxY = maxOf([view.bookBid, view.poolBid, view.bookAsk, view.poolAsk]);
|
|
410
|
+
chart.setScale('y', { min: 0, max: Number.isFinite(maxY) && maxY > 0 ? maxY * 1.08 : 1 });
|
|
411
|
+
chart.setScale('x', { min: xMin, max: xMax });
|
|
412
|
+
setText('r-unit', currentPriceUnit());
|
|
413
|
+
setText('r-depth-unit', currentDepthUnit());
|
|
414
|
+
updateHeaderPrices();
|
|
415
|
+
updateReadout(null);
|
|
416
|
+
}
|
|
417
|
+
// Axis clicks toggle, but a drag that starts on an axis gutter must not
|
|
418
|
+
// be mistaken for a click when the mouse is released.
|
|
419
|
+
let pointerDownX = 0;
|
|
420
|
+
let pointerDownY = 0;
|
|
421
|
+
chart.root.addEventListener('click', (e) => {
|
|
422
|
+
if (Math.hypot(e.clientX - pointerDownX, e.clientY - pointerDownY) > 4) return;
|
|
423
|
+
if (inYAxisZone(e)) {
|
|
424
|
+
depthMode = depthMode === 'quote' ? 'base' : 'quote';
|
|
425
|
+
applyView();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (inXAxisZone(e)) {
|
|
429
|
+
orientation = orientation === 'normal' ? 'inverted' : 'normal';
|
|
430
|
+
applyView();
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
updateHeaderPrices();
|
|
434
|
+
|
|
435
|
+
let leavePending = null;
|
|
436
|
+
chart.over.addEventListener('mousemove', () => {
|
|
437
|
+
if (leavePending !== null) { clearTimeout(leavePending); leavePending = null; }
|
|
438
|
+
updateReadout(chart.cursor.idx);
|
|
439
|
+
});
|
|
440
|
+
chart.over.addEventListener('mouseleave', () => {
|
|
441
|
+
leavePending = setTimeout(() => { leavePending = null; updateReadout(null); }, 60);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
// ── zoom / pan / reset ──────────────────────────────────────────────
|
|
445
|
+
chart.root.addEventListener('wheel', (e) => {
|
|
446
|
+
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
447
|
+
e.preventDefault();
|
|
448
|
+
e.stopPropagation();
|
|
449
|
+
const rect = chart.root.getBoundingClientRect();
|
|
450
|
+
const left = e.clientX - rect.left - (chart.bbox.left / (chart.pxRatio || 1));
|
|
451
|
+
const center = chart.posToVal(left, 'x');
|
|
452
|
+
const current = currentX(chart);
|
|
453
|
+
const span = current.max - current.min;
|
|
454
|
+
if (!Number.isFinite(span) || span <= 0) return;
|
|
455
|
+
const factor = e.deltaY < 0 ? 0.85 : 1.15;
|
|
456
|
+
const nextSpan = Math.max((xMax - xMin) * 0.02, Math.min(xMax - xMin, span * factor));
|
|
457
|
+
const ratio = (center - current.min) / span;
|
|
458
|
+
applyXRange(chart, center - nextSpan * ratio, center - nextSpan * ratio + nextSpan);
|
|
459
|
+
}, { passive: false });
|
|
460
|
+
|
|
461
|
+
chart.root.addEventListener('dblclick', () => {
|
|
462
|
+
chart.setScale('x', { min: xMin, max: xMax });
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
let dragging = false;
|
|
466
|
+
let dragStartX = 0;
|
|
467
|
+
let dragStartMin = xMin;
|
|
468
|
+
let dragStartMax = xMax;
|
|
469
|
+
let xUnitsPerPx = 0;
|
|
470
|
+
function endDrag() {
|
|
471
|
+
if (!dragging) return;
|
|
472
|
+
dragging = false;
|
|
473
|
+
document.body.style.cursor = '';
|
|
474
|
+
window.removeEventListener('mousemove', onMouseMove);
|
|
475
|
+
window.removeEventListener('mouseup', endDrag);
|
|
476
|
+
}
|
|
477
|
+
function onMouseMove(e) {
|
|
478
|
+
if (!dragging) return;
|
|
479
|
+
e.preventDefault();
|
|
480
|
+
const deltaPx = e.clientX - dragStartX;
|
|
481
|
+
applyXRange(chart, dragStartMin - deltaPx * xUnitsPerPx, dragStartMax - deltaPx * xUnitsPerPx);
|
|
482
|
+
}
|
|
483
|
+
chart.root.addEventListener('mousedown', (e) => {
|
|
484
|
+
if (!e || e.button !== 0 || e.ctrlKey || e.metaKey || e.altKey) return;
|
|
485
|
+
pointerDownX = e.clientX;
|
|
486
|
+
pointerDownY = e.clientY;
|
|
487
|
+
const rect = chart.root.getBoundingClientRect();
|
|
488
|
+
if (e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom) return;
|
|
489
|
+
// Axis gutters are click zones (y = depth asset, x = invert price), not pan.
|
|
490
|
+
const overRect = chart.over && chart.over.getBoundingClientRect();
|
|
491
|
+
if (overRect && (e.clientX >= overRect.right - 2 || e.clientY >= overRect.bottom - 2)) return;
|
|
492
|
+
e.preventDefault();
|
|
493
|
+
e.stopPropagation();
|
|
494
|
+
dragging = true;
|
|
495
|
+
dragStartX = e.clientX;
|
|
496
|
+
const current = currentX(chart);
|
|
497
|
+
dragStartMin = current.min;
|
|
498
|
+
dragStartMax = current.max;
|
|
499
|
+
xUnitsPerPx = (current.max - current.min) / (chart.bbox.width / (chart.pxRatio || 1));
|
|
500
|
+
document.body.style.cursor = 'grabbing';
|
|
501
|
+
window.addEventListener('mousemove', onMouseMove);
|
|
502
|
+
window.addEventListener('mouseup', endDrag, { once: true });
|
|
503
|
+
});
|
|
504
|
+
window.addEventListener('blur', endDrag);
|
|
505
|
+
window.addEventListener('keydown', (e) => {
|
|
506
|
+
if ((e.ctrlKey || e.metaKey) && e.key === '0') chart.setScale('x', { min: xMin, max: xMax });
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
// ── resize ──────────────────────────────────────────────────────────
|
|
510
|
+
function sizeChart() {
|
|
511
|
+
chart.setSize({ width: chartEl.offsetWidth, height: chartEl.offsetHeight });
|
|
512
|
+
}
|
|
513
|
+
window.addEventListener('resize', sizeChart);
|
|
514
|
+
sizeChart();
|
|
515
|
+
updateReadout(null);
|
|
516
|
+
requestAnimationFrame(() => requestAnimationFrame(sizeChart));
|
|
517
|
+
})();
|
|
518
|
+
</script>
|
|
519
|
+
</body>
|
|
520
|
+
</html>`;
|
|
521
|
+
}
|
|
522
|
+
export { generateLiquidityHTML };
|
|
523
|
+
//# sourceMappingURL=liquidity_chart_generator.js.map
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LIQUIDITY DEPTH MODEL (pure, no I/O)
|
|
3
|
+
*
|
|
4
|
+
* Blends the limit-order book with the constant-product AMM pool of one asset
|
|
5
|
+
* pair into a single cumulative-depth curve, in the style of PoolTool's depth
|
|
6
|
+
* chart.
|
|
7
|
+
*
|
|
8
|
+
* Conventions
|
|
9
|
+
* -----------
|
|
10
|
+
* - `base` = asset A (the asset whose price is quoted)
|
|
11
|
+
* - `quote` = asset B (the asset the depth is measured in)
|
|
12
|
+
* - display price = quote per base (B/A), matching DEXBot2's price orientation
|
|
13
|
+
* - depth = cumulative quote (B) liquidity at or better than that price:
|
|
14
|
+
* * bids: quote the buyer has to spend to fill every bid at price >= p
|
|
15
|
+
* * asks: quote the seller receives for every ask at price <= p
|
|
16
|
+
* Parallel base-unit (A) depth arrays are sampled for the chart's y-axis
|
|
17
|
+
* toggle: bids/asks accumulate order base amounts, and the pool uses the
|
|
18
|
+
* mirror constant-product solution (see poolBidBaseDepth/poolAskBaseDepth).
|
|
19
|
+
*
|
|
20
|
+
* The on-chain order book exposes orders as `base`/`quote` decimal strings.
|
|
21
|
+
* `get_order_book(A, B)` returns:
|
|
22
|
+
* - `bids`: orders that SELL A (real asks) — the source code puts every
|
|
23
|
+
* order whose `sell_price.base` is asset A into this list.
|
|
24
|
+
* - `asks`: orders that SELL B, i.e. buy A (real bids).
|
|
25
|
+
* `normalizeOrderBook` performs that side flip, so the rest of this module
|
|
26
|
+
* (and the chart) speaks plain real bid/ask.
|
|
27
|
+
*
|
|
28
|
+
* Pool depth is the exact constant-product solution for moving the pool price
|
|
29
|
+
* from p0 to p:
|
|
30
|
+
* - below p0 (selling base into the pool):
|
|
31
|
+
* quote received = quoteReserve * (1 - sqrt(p / p0))
|
|
32
|
+
* - above p0 (buying base from the pool):
|
|
33
|
+
* quote paid = quoteReserve * (sqrt(p / p0) - 1)
|
|
34
|
+
* which is the same curve PoolTool plots as `amount_y * (1 - sqrt(p_pool/p))`
|
|
35
|
+
* and `amount_y * (sqrt(p_pool/p) - 1)` (there p_pool/p is inverted because
|
|
36
|
+
* PoolTool's price is base-per-quote). The closed forms preserve x*y = k and
|
|
37
|
+
* move the implied pool price to exactly p; the taker fee is not applied,
|
|
38
|
+
* matching PoolTool's depth chart.
|
|
39
|
+
*/
|
|
40
|
+
interface RawBookOrder {
|
|
41
|
+
price?: string | number;
|
|
42
|
+
base?: string | number;
|
|
43
|
+
quote?: string | number;
|
|
44
|
+
id?: string;
|
|
45
|
+
owner_name?: string;
|
|
46
|
+
}
|
|
47
|
+
interface RawOrderBook {
|
|
48
|
+
bids?: RawBookOrder[];
|
|
49
|
+
asks?: RawBookOrder[];
|
|
50
|
+
}
|
|
51
|
+
interface BookOrder {
|
|
52
|
+
/** Display price in quote per base. */
|
|
53
|
+
price: number;
|
|
54
|
+
/** Amount of quote asset traded by this order (the depth unit). */
|
|
55
|
+
quote: number;
|
|
56
|
+
/** Amount of base asset traded by this order. */
|
|
57
|
+
base: number;
|
|
58
|
+
}
|
|
59
|
+
interface BookSides {
|
|
60
|
+
/** Real bids (buyers of base), highest price first. */
|
|
61
|
+
bids: BookOrder[];
|
|
62
|
+
/** Real asks (sellers of base), lowest price first. */
|
|
63
|
+
asks: BookOrder[];
|
|
64
|
+
}
|
|
65
|
+
interface PoolState {
|
|
66
|
+
id?: string;
|
|
67
|
+
shareAsset?: string;
|
|
68
|
+
baseReserve: number;
|
|
69
|
+
quoteReserve: number;
|
|
70
|
+
/** AMM taker fee in percent (e.g. 0.2 = 20 bps), when known. */
|
|
71
|
+
feePercent?: number | null;
|
|
72
|
+
}
|
|
73
|
+
interface DepthSeries {
|
|
74
|
+
prices: number[];
|
|
75
|
+
/** Cumulative quote (B) depth. */
|
|
76
|
+
bookBid: number[];
|
|
77
|
+
poolBid: number[];
|
|
78
|
+
bookAsk: number[];
|
|
79
|
+
poolAsk: number[];
|
|
80
|
+
/** Cumulative base (A) depth, same price axis (toggled by the chart). */
|
|
81
|
+
bookBidBase: number[];
|
|
82
|
+
poolBidBase: number[];
|
|
83
|
+
bookAskBase: number[];
|
|
84
|
+
poolAskBase: number[];
|
|
85
|
+
}
|
|
86
|
+
interface PriceWindow {
|
|
87
|
+
min: number;
|
|
88
|
+
max: number;
|
|
89
|
+
}
|
|
90
|
+
interface BookStats {
|
|
91
|
+
bestBid: number | null;
|
|
92
|
+
bestAsk: number | null;
|
|
93
|
+
mid: number | null;
|
|
94
|
+
spreadPercent: number | null;
|
|
95
|
+
bidOrders: number;
|
|
96
|
+
askOrders: number;
|
|
97
|
+
bidQuoteTotal: number;
|
|
98
|
+
askQuoteTotal: number;
|
|
99
|
+
}
|
|
100
|
+
interface DepthBuildInput {
|
|
101
|
+
bids: BookOrder[];
|
|
102
|
+
asks: BookOrder[];
|
|
103
|
+
pool?: PoolState | null;
|
|
104
|
+
window: PriceWindow;
|
|
105
|
+
steps?: number;
|
|
106
|
+
}
|
|
107
|
+
interface DepthBuildResult extends DepthSeries {
|
|
108
|
+
poolPrice: number | null;
|
|
109
|
+
/** Union grid including exact book levels for sharp step corners. */
|
|
110
|
+
priceCount: number;
|
|
111
|
+
}
|
|
112
|
+
declare const DEFAULT_STEPS = 400;
|
|
113
|
+
/**
|
|
114
|
+
* Convert the raw `get_order_book` payload into real bid/ask sides and display
|
|
115
|
+
* prices (quote per base). Invalid orders (missing/zero amounts) are skipped.
|
|
116
|
+
* See the module header for why the on-chain `bids` list becomes real asks.
|
|
117
|
+
*/
|
|
118
|
+
declare function normalizeOrderBook(rawBook: RawOrderBook | null | undefined): BookSides;
|
|
119
|
+
declare function computePoolPrice(pool: PoolState | null | undefined): number | null;
|
|
120
|
+
declare function computeBookStats(sides: BookSides): BookStats;
|
|
121
|
+
/**
|
|
122
|
+
* Symmetric linear window around the pool price (or the book mid when there is
|
|
123
|
+
* no pool), e.g. range 0.5 => [0.5 * center, 1.5 * center].
|
|
124
|
+
*/
|
|
125
|
+
declare function resolvePriceWindow(center: number | null, range: number): PriceWindow;
|
|
126
|
+
declare function poolBidDepth(price: number, poolPrice: number, quoteReserve: number): number;
|
|
127
|
+
declare function poolAskDepth(price: number, poolPrice: number, quoteReserve: number): number;
|
|
128
|
+
declare function poolBidBaseDepth(price: number, poolPrice: number, baseReserve: number): number;
|
|
129
|
+
declare function poolAskBaseDepth(price: number, poolPrice: number, baseReserve: number): number;
|
|
130
|
+
/**
|
|
131
|
+
* Sample the two liquidity sources independently over a price window. The
|
|
132
|
+
* returned arrays are all the same length and share the `prices` axis.
|
|
133
|
+
*
|
|
134
|
+
* Book depth and pool depth are deliberately NOT summed: an on-chain order and
|
|
135
|
+
* an AMM reserve are different claims on liquidity, so the chart shows them
|
|
136
|
+
* side by side at each price instead of collapsing them into one stacked total.
|
|
137
|
+
*/
|
|
138
|
+
declare function buildDepthSeries(input: DepthBuildInput): DepthBuildResult;
|
|
139
|
+
export { DEFAULT_STEPS, normalizeOrderBook, computePoolPrice, computeBookStats, resolvePriceWindow, buildDepthSeries, poolBidDepth, poolAskDepth, poolBidBaseDepth, poolAskBaseDepth, };
|
|
140
|
+
export type { RawBookOrder, RawOrderBook, BookOrder, BookSides, PoolState, DepthSeries, DepthBuildInput, DepthBuildResult, PriceWindow, BookStats, };
|
|
141
|
+
//# sourceMappingURL=liquidity_depth_model.d.ts.map
|