dexbot 1.6.18 → 1.7.1
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 +68 -0
- package/README.md +24 -18
- package/analysis/README.md +117 -5
- 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/account_resolver.js +5 -6
- 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 +336 -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/bot_key_utils.d.ts +44 -1
- package/dist/analysis/bot_key_utils.js +76 -7
- package/dist/analysis/fills_cache.js +2 -1
- package/dist/analysis/grid_correction_check.d.ts +1 -19
- package/dist/analysis/grid_correction_check.js +12 -30
- 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 +693 -0
- package/dist/analysis/liquidity/liquidity_depth_model.d.ts +166 -0
- package/dist/analysis/liquidity/liquidity_depth_model.js +258 -0
- package/dist/analysis/liquidity/liquidity_view.d.ts +53 -0
- package/dist/analysis/liquidity/liquidity_view.js +66 -0
- package/dist/analysis/resolve_bot_accounts.js +7 -6
- package/dist/analysis/trade_profitability.js +4 -2
- package/dist/analysis/tradingview/analyze_tradingview.js +2 -1
- package/dist/credential-daemon.js +13 -2
- package/dist/dexbot.js +77 -172
- package/dist/modules/account_bots.d.ts +11 -1
- package/dist/modules/account_orders.d.ts +12 -0
- package/dist/modules/account_orders.js +30 -0
- package/dist/modules/asset_lookup.d.ts +25 -0
- package/dist/modules/asset_lookup.js +75 -0
- package/dist/modules/bitshares-native/transport.js +24 -6
- package/dist/modules/bitshares_client.d.ts +24 -6
- package/dist/modules/bitshares_client.js +44 -7
- package/dist/modules/bot_settings.d.ts +2 -0
- 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.d.ts +26 -3
- package/dist/modules/constants.js +63 -8
- package/dist/modules/dexbot_class.d.ts +1 -6
- package/dist/modules/dexbot_class.js +14 -0
- package/dist/modules/dexbot_cow_runtime.d.ts +97 -7
- package/dist/modules/dexbot_cow_runtime.js +634 -136
- package/dist/modules/dexbot_fill_runtime.js +132 -23
- package/dist/modules/dexbot_maintenance_runtime.js +242 -74
- package/dist/modules/dexbot_startup_runtime.js +32 -20
- package/dist/modules/dexbot_state_recovery.d.ts +33 -1
- package/dist/modules/dexbot_state_recovery.js +167 -5
- package/dist/modules/launcher/runtime_entry.d.ts +21 -1
- package/dist/modules/launcher/runtime_entry.js +36 -1
- package/dist/modules/liquidity_pool_ref.d.ts +32 -0
- package/dist/modules/liquidity_pool_ref.js +116 -0
- package/dist/modules/node_connect_policy.d.ts +8 -1
- package/dist/modules/node_connect_policy.js +18 -2
- package/dist/modules/node_flap_score.d.ts +87 -0
- package/dist/modules/node_flap_score.js +124 -0
- package/dist/modules/node_health_cache.d.ts +40 -3
- package/dist/modules/node_health_cache.js +66 -20
- package/dist/modules/node_manager.d.ts +101 -19
- package/dist/modules/node_manager.js +255 -65
- package/dist/modules/order/accounting.js +56 -7
- package/dist/modules/order/format.d.ts +24 -10
- package/dist/modules/order/format.js +30 -10
- package/dist/modules/order/grid.js +91 -13
- package/dist/modules/order/grid_reconcile.d.ts +5 -1
- package/dist/modules/order/grid_reconcile.js +333 -189
- package/dist/modules/order/grid_reconcile_internal.d.ts +38 -2
- package/dist/modules/order/grid_reconcile_internal.js +298 -100
- package/dist/modules/order/index.d.ts +19 -1
- package/dist/modules/order/lifecycle.d.ts +353 -0
- package/dist/modules/order/lifecycle.js +1109 -0
- package/dist/modules/order/manager.d.ts +273 -5
- package/dist/modules/order/manager.js +654 -48
- package/dist/modules/order/placement_lock.d.ts +102 -0
- package/dist/modules/order/placement_lock.js +222 -0
- package/dist/modules/order/strategy.js +13 -1
- package/dist/modules/order/sync_engine.d.ts +33 -1
- package/dist/modules/order/sync_engine.js +241 -73
- package/dist/modules/order/utils/math.d.ts +39 -1
- package/dist/modules/order/utils/math.js +83 -1
- package/dist/modules/order/utils/order.d.ts +181 -5
- package/dist/modules/order/utils/order.js +455 -9
- package/dist/modules/order/utils/system.d.ts +81 -1
- package/dist/modules/order/utils/system.js +331 -44
- package/dist/modules/order/utils/validate.d.ts +7 -1
- package/dist/modules/order/utils/validate.js +80 -3
- package/dist/modules/outcome.d.ts +54 -0
- package/dist/modules/outcome.js +51 -0
- package/dist/modules/settings_merge.d.ts +4 -0
- package/dist/modules/settings_merge.js +9 -0
- package/dist/modules/timing_policy.d.ts +82 -0
- package/dist/modules/timing_policy.js +244 -0
- package/dist/modules/types.d.ts +137 -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-credit.js +54 -17
- package/dist/scripts/analyze-orders.d.ts +4 -2
- package/dist/scripts/analyze-orders.js +115 -35
- 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 +48 -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/test-credit-renewal.js +12 -5
- 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/COW_INVARIANTS.md +69 -10
- package/docs/DEXBOT_COMPARISON.md +6 -6
- package/docs/EVOLUTION.md +10 -8
- package/docs/FUND_MOVEMENT_AND_ACCOUNTING.md +1 -1
- package/docs/GRID_PRICE_INVARIANT.md +5 -2
- package/docs/GRID_RECONCILE.md +36 -11
- package/docs/LOGGING.md +50 -4
- package/docs/ORDER_ENGINE_POST_1.0_RETROSPECTIVE.md +177 -27
- package/docs/README.md +23 -2
- package/docs/WORKFLOW.md +12 -4
- package/docs/architecture.md +42 -5
- package/docs/developer_guide.md +25 -8
- package/modules/README.md +8 -1
- package/package.json +3 -1
- package/scripts/README.md +39 -7
- package/scripts/pdev.sh +14 -1
- package/scripts/pmain.sh +23 -8
- package/scripts/ptest.sh +7 -0
- package/tests/README.md +30 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* LAST-FILL-GUARD CHECK
|
|
5
5
|
*
|
|
6
6
|
* Validates LAST-FILL-GUARD discipline — DIRECTION ONLY, relative to the last
|
|
7
|
-
* fill price (v1.6.
|
|
7
|
+
* fill price (v1.6.17+, retrospective §9.7):
|
|
8
8
|
* BUY blocked if price >= pivot
|
|
9
9
|
* SELL blocked if price <= pivot
|
|
10
10
|
* e.g. pivot 1000 => BUY must be < 1000, SELL must be > 1000.
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
* identity for a fill, so it cannot classify edge fills and will still
|
|
28
28
|
* judge the pair after one. Expect rare false positives on wick fills.
|
|
29
29
|
*
|
|
30
|
-
*
|
|
30
|
+
* Uses the runtime's own predicate (`modules/order/utils/system.ts:`
|
|
31
|
+
* `isLastFillGuardBlocked`); the checker never re-implements it.
|
|
31
32
|
*
|
|
32
33
|
* Fetches fill_order operations from Kibana (same pipeline as
|
|
33
34
|
* trade_profitability.ts) and checks for last-fill guard violations.
|
|
@@ -42,6 +43,7 @@
|
|
|
42
43
|
* node dist/analysis/grid_correction_check.js --list-bots
|
|
43
44
|
*/
|
|
44
45
|
import { getErrorMessage, getErrorField } from '../modules/utils/errors.js';
|
|
46
|
+
import { isLastFillGuardBlocked } from '../modules/order/utils/system.js';
|
|
45
47
|
import fs from 'node:fs';
|
|
46
48
|
import { pathToFileURL } from 'node:url';
|
|
47
49
|
import * as KC from '../market_adapter/core/kibana_client.js';
|
|
@@ -51,33 +53,13 @@ import { resolveBotAccount } from './account_resolver.js';
|
|
|
51
53
|
import { classifyFill, assetSymbol, fetchAllFills, resolveAssetPrecisions, } from './fills_source.js';
|
|
52
54
|
const { kibanaSearch, DEFAULT_CONFIG: BASE_CONFIG } = KC;
|
|
53
55
|
const OP_LIMIT_ORDER_UPDATE = 77;
|
|
54
|
-
// ─── LAST-FILL-GUARD
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
* (a re-place at the just-filled level).
|
|
62
|
-
* @param {number} price - Target order price
|
|
63
|
-
* @param {string} type - buy/sell
|
|
64
|
-
* @param {number|null} lastPrice - Most recent fill price
|
|
65
|
-
* @param {string|null} lastType - Most recent fill side (buy/sell)
|
|
66
|
-
* @returns {{blocked: boolean, pivot: number|null, halfInc: number, threshold: number|null}}
|
|
67
|
-
*/
|
|
68
|
-
function isLastFillGuardBlocked(price, type, lastPrice, lastType) {
|
|
69
|
-
const numPrice = Number(price);
|
|
70
|
-
if (!Number.isFinite(numPrice))
|
|
71
|
-
return { blocked: false, pivot: null };
|
|
72
|
-
if (lastPrice == null || !Number.isFinite(Number(lastPrice)) || lastType == null)
|
|
73
|
-
return { blocked: false, pivot: null };
|
|
74
|
-
const pivot = Number(lastPrice);
|
|
75
|
-
if (type === 'buy' && numPrice >= pivot)
|
|
76
|
-
return { blocked: true, pivot, halfInc: 0, threshold: pivot };
|
|
77
|
-
if (type === 'sell' && numPrice <= pivot)
|
|
78
|
-
return { blocked: true, pivot, halfInc: 0, threshold: pivot };
|
|
79
|
-
return { blocked: false, pivot: null, halfInc: 0, threshold: null };
|
|
80
|
-
}
|
|
56
|
+
// ─── LAST-FILL-GUARD (the runtime's own predicate) ──────────────────────────────
|
|
57
|
+
//
|
|
58
|
+
// `isLastFillGuardBlocked` is IMPORTED from modules/order/utils/system.ts, not
|
|
59
|
+
// re-implemented. The old local copy was labelled "mirrors 1:1" and had already
|
|
60
|
+
// kept the removed band term; a copy of a rule is a second rule waiting to drift
|
|
61
|
+
// (retrospective R3.5 / §9.10/P5). The module returns `{blocked, pivot, threshold}`
|
|
62
|
+
// — there is no `halfInc`, because the increment no longer participates.
|
|
81
63
|
function resolveIncrementPercent(botMeta, override) {
|
|
82
64
|
if (override != null && Number.isFinite(override) && override > 0)
|
|
83
65
|
return override;
|
|
@@ -97,7 +79,7 @@ Usage: node dist/analysis/grid_correction_check.js --bot-key <key> [options]
|
|
|
97
79
|
Validates LAST-FILL-GUARD discipline (DIRECTION ONLY relative to the last fill):
|
|
98
80
|
BUY must be < last fill price, SELL must be > last fill price.
|
|
99
81
|
Equal price counts as blocked (a re-place at the just-filled level).
|
|
100
|
-
|
|
82
|
+
Uses the runtime's own predicate (isLastFillGuardBlocked); not a copy.
|
|
101
83
|
Cold start (no previous fill) is disabled.
|
|
102
84
|
|
|
103
85
|
Required:
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BookSides, PoolState } from './liquidity_depth_model.js';
|
|
2
|
+
interface ChainDbClient {
|
|
3
|
+
db(method: string, args?: unknown[]): Promise<unknown>;
|
|
4
|
+
}
|
|
5
|
+
interface ChainAsset {
|
|
6
|
+
id: string;
|
|
7
|
+
symbol: string;
|
|
8
|
+
precision: number;
|
|
9
|
+
}
|
|
10
|
+
interface RawLimitOrder {
|
|
11
|
+
id?: string;
|
|
12
|
+
for_sale?: string | number;
|
|
13
|
+
sell_price?: {
|
|
14
|
+
base?: {
|
|
15
|
+
amount?: string | number;
|
|
16
|
+
asset_id?: string;
|
|
17
|
+
};
|
|
18
|
+
quote?: {
|
|
19
|
+
amount?: string | number;
|
|
20
|
+
asset_id?: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
interface RawPool {
|
|
25
|
+
id?: string;
|
|
26
|
+
asset_a?: string;
|
|
27
|
+
asset_b?: string;
|
|
28
|
+
asset_ids?: string[];
|
|
29
|
+
balance_a?: string | number;
|
|
30
|
+
balance_b?: string | number;
|
|
31
|
+
share_asset?: string;
|
|
32
|
+
taker_fee_percent?: number;
|
|
33
|
+
}
|
|
34
|
+
declare function resolveAsset(client: ChainDbClient, ref: string): Promise<ChainAsset>;
|
|
35
|
+
/**
|
|
36
|
+
* Decode raw limit orders into real bid/ask sides with display prices
|
|
37
|
+
* (quote per base) and quote/base amounts.
|
|
38
|
+
*
|
|
39
|
+
* `get_order_book` and `get_limit_orders` share the same side convention: an
|
|
40
|
+
* order whose `sell_price.base` is the requested base SELLS the base (real
|
|
41
|
+
* ask); the other side sells the quote (real bid).
|
|
42
|
+
*/
|
|
43
|
+
declare function normalizeLimitOrders(orders: unknown, base: ChainAsset, quote: ChainAsset): BookSides;
|
|
44
|
+
/**
|
|
45
|
+
* Fetch the order book, preferring `get_limit_orders` because nodes usually
|
|
46
|
+
* cap `get_order_book` at 50 levels while `get_limit_orders` allows up to
|
|
47
|
+
* ~300 per side. Falls back to `get_order_book` when the wider API is not
|
|
48
|
+
* available. `depth` is per side.
|
|
49
|
+
*/
|
|
50
|
+
declare function fetchOrderBook(client: ChainDbClient, base: ChainAsset, quote: ChainAsset, depth: number): Promise<BookSides>;
|
|
51
|
+
declare function fetchPools(client: ChainDbClient, baseId: string, quoteId: string): Promise<RawPool[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Resolve a pool by id (`133` / `1.19.133`) or by its pool token (LP share-asset
|
|
54
|
+
* symbol). Shared with `dexbot tv`/`dw` and the liquidity analyzer's `--pool`.
|
|
55
|
+
*/
|
|
56
|
+
declare function resolvePool(client: ChainDbClient, ref: string): Promise<RawPool>;
|
|
57
|
+
/** Decode one raw pool into display reserves (base = asset A, quote = asset B). */
|
|
58
|
+
declare function decodePool(pool: RawPool | null | undefined, base: ChainAsset, quote: ChainAsset): PoolState | null;
|
|
59
|
+
/**
|
|
60
|
+
* Decode and select a pool. With `poolId`, only that pool is considered;
|
|
61
|
+
* otherwise the deepest pool (largest quote reserve) wins.
|
|
62
|
+
*/
|
|
63
|
+
declare function selectPool(pools: RawPool[], base: ChainAsset, quote: ChainAsset, poolId?: string | null): PoolState | null;
|
|
64
|
+
export { resolveAsset, fetchOrderBook, normalizeLimitOrders, fetchPools, resolvePool, decodePool, selectPool };
|
|
65
|
+
export type { ChainDbClient, ChainAsset, RawLimitOrder, RawPool };
|
|
66
|
+
//# sourceMappingURL=liquidity_chain.d.ts.map
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* CHAIN READ HELPERS for the liquidity depth chart.
|
|
4
|
+
*
|
|
5
|
+
* Thin, dependency-injected wrappers over the read-only chain client so the
|
|
6
|
+
* order-book/pool decoding can be unit-tested with a stub `db()` function.
|
|
7
|
+
* Analysis connections never use the production singleton client (see
|
|
8
|
+
* analysis/chain_pool.ts).
|
|
9
|
+
*
|
|
10
|
+
* Pool references (id or pool token) are resolved by the shared helper in
|
|
11
|
+
* modules/liquidity_pool_ref.ts, so `dexbot liq`, `dexbot tv`/`dw` and this
|
|
12
|
+
* analyzer accept the same spellings.
|
|
13
|
+
*/
|
|
14
|
+
import { blockchainToFloat } from '../../modules/order/utils/math.js';
|
|
15
|
+
import { poolAssetIds, resolvePoolRef } from '../../modules/liquidity_pool_ref.js';
|
|
16
|
+
import { resolveAssetRef } from '../../modules/asset_lookup.js';
|
|
17
|
+
import { normalizeOrderBook } from './liquidity_depth_model.js';
|
|
18
|
+
async function resolveAsset(client, ref) {
|
|
19
|
+
const probe = String(ref || '').trim();
|
|
20
|
+
if (!probe)
|
|
21
|
+
throw new Error('Asset reference is empty');
|
|
22
|
+
// Shared case-insensitive lookup (modules/asset_lookup.ts): "token.sym",
|
|
23
|
+
// "TOKEN.SYM" and "1.3.x" all reach the same asset object.
|
|
24
|
+
const asset = await resolveAssetRef((method, args) => client.db(method, args), probe);
|
|
25
|
+
if (!asset?.id || !asset.symbol || typeof asset.precision !== 'number') {
|
|
26
|
+
throw new Error(`Cannot resolve asset '${probe}' on chain`);
|
|
27
|
+
}
|
|
28
|
+
return { id: asset.id, symbol: asset.symbol, precision: asset.precision };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Decode raw limit orders into real bid/ask sides with display prices
|
|
32
|
+
* (quote per base) and quote/base amounts.
|
|
33
|
+
*
|
|
34
|
+
* `get_order_book` and `get_limit_orders` share the same side convention: an
|
|
35
|
+
* order whose `sell_price.base` is the requested base SELLS the base (real
|
|
36
|
+
* ask); the other side sells the quote (real bid).
|
|
37
|
+
*/
|
|
38
|
+
function normalizeLimitOrders(orders, base, quote) {
|
|
39
|
+
const bids = [];
|
|
40
|
+
const asks = [];
|
|
41
|
+
if (!Array.isArray(orders))
|
|
42
|
+
return { bids, asks };
|
|
43
|
+
for (const entry of orders) {
|
|
44
|
+
const sellAsset = entry?.sell_price?.base?.asset_id;
|
|
45
|
+
const baseRaw = Number(entry?.sell_price?.base?.amount);
|
|
46
|
+
const quoteRaw = Number(entry?.sell_price?.quote?.amount);
|
|
47
|
+
const forSale = Number(entry?.for_sale ?? baseRaw);
|
|
48
|
+
if (!(baseRaw > 0) || !(quoteRaw > 0) || !(forSale > 0))
|
|
49
|
+
continue;
|
|
50
|
+
const sellsBase = sellAsset === base.id;
|
|
51
|
+
const sellsQuote = sellAsset === quote.id;
|
|
52
|
+
if (!sellsBase && !sellsQuote)
|
|
53
|
+
continue;
|
|
54
|
+
// `received` is the amount of the other asset the seller gets for the
|
|
55
|
+
// whole order; scaling by for_sale/baseRaw handles reduced fractions.
|
|
56
|
+
const receivedRaw = forSale * (quoteRaw / baseRaw);
|
|
57
|
+
if (sellsBase) {
|
|
58
|
+
const soldBase = blockchainToFloat(forSale, base.precision);
|
|
59
|
+
const receivedQuote = blockchainToFloat(receivedRaw, quote.precision);
|
|
60
|
+
if (soldBase > 0 && receivedQuote > 0) {
|
|
61
|
+
asks.push({ price: receivedQuote / soldBase, quote: receivedQuote, base: soldBase });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const soldQuote = blockchainToFloat(forSale, quote.precision);
|
|
66
|
+
const receivedBase = blockchainToFloat(receivedRaw, base.precision);
|
|
67
|
+
if (soldQuote > 0 && receivedBase > 0) {
|
|
68
|
+
bids.push({ price: soldQuote / receivedBase, quote: soldQuote, base: receivedBase });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
bids.sort((a, b) => b.price - a.price);
|
|
73
|
+
asks.sort((a, b) => a.price - b.price);
|
|
74
|
+
return { bids, asks };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Fetch the order book, preferring `get_limit_orders` because nodes usually
|
|
78
|
+
* cap `get_order_book` at 50 levels while `get_limit_orders` allows up to
|
|
79
|
+
* ~300 per side. Falls back to `get_order_book` when the wider API is not
|
|
80
|
+
* available. `depth` is per side.
|
|
81
|
+
*/
|
|
82
|
+
async function fetchOrderBook(client, base, quote, depth) {
|
|
83
|
+
const requested = Math.max(1, Math.floor(depth));
|
|
84
|
+
const limits = [];
|
|
85
|
+
for (const candidate of [requested, 300, 100, 50]) {
|
|
86
|
+
if (candidate <= requested && !limits.includes(candidate))
|
|
87
|
+
limits.push(candidate);
|
|
88
|
+
}
|
|
89
|
+
for (const limit of limits) {
|
|
90
|
+
try {
|
|
91
|
+
const orders = await client.db('get_limit_orders', [base.id, quote.id, limit]);
|
|
92
|
+
if (Array.isArray(orders))
|
|
93
|
+
return normalizeLimitOrders(orders, base, quote);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
const message = String(err?.message ?? err);
|
|
97
|
+
if (!/limit|configured_limit|unknown|not found|method/i.test(message))
|
|
98
|
+
throw err;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Conservative fallback: the classic order-book API (commonly capped at 50).
|
|
102
|
+
for (const limit of limits) {
|
|
103
|
+
if (limit > 50)
|
|
104
|
+
continue;
|
|
105
|
+
try {
|
|
106
|
+
const raw = await client.db('get_order_book', [base.id, quote.id, limit]);
|
|
107
|
+
return normalizeOrderBook(raw);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
const message = String(err?.message ?? err);
|
|
111
|
+
if (!/limit|configured_limit/i.test(message))
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const raw = await client.db('get_order_book', [base.id, quote.id, 50]);
|
|
116
|
+
return normalizeOrderBook(raw);
|
|
117
|
+
}
|
|
118
|
+
async function fetchPools(client, baseId, quoteId) {
|
|
119
|
+
const raw = await client.db('get_liquidity_pools_by_both_assets', [baseId, quoteId]);
|
|
120
|
+
return Array.isArray(raw) ? raw : [];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Resolve a pool by id (`133` / `1.19.133`) or by its pool token (LP share-asset
|
|
124
|
+
* symbol). Shared with `dexbot tv`/`dw` and the liquidity analyzer's `--pool`.
|
|
125
|
+
*/
|
|
126
|
+
async function resolvePool(client, ref) {
|
|
127
|
+
return await resolvePoolRef((method, args) => client.db(method, args), ref);
|
|
128
|
+
}
|
|
129
|
+
/** Decode one raw pool into display reserves (base = asset A, quote = asset B). */
|
|
130
|
+
function decodePool(pool, base, quote) {
|
|
131
|
+
if (!pool)
|
|
132
|
+
return null;
|
|
133
|
+
const [assetA, assetB] = poolAssetIds(pool);
|
|
134
|
+
const aIsBase = assetA === base.id;
|
|
135
|
+
const aIsQuote = assetA === quote.id;
|
|
136
|
+
if (!aIsBase && !aIsQuote)
|
|
137
|
+
return null;
|
|
138
|
+
if (aIsBase && assetB !== quote.id)
|
|
139
|
+
return null;
|
|
140
|
+
if (aIsQuote && assetB !== base.id)
|
|
141
|
+
return null;
|
|
142
|
+
const baseRaw = aIsBase ? pool.balance_a : pool.balance_b;
|
|
143
|
+
const quoteRaw = aIsBase ? pool.balance_b : pool.balance_a;
|
|
144
|
+
if (baseRaw == null || quoteRaw == null)
|
|
145
|
+
return null;
|
|
146
|
+
const baseReserve = blockchainToFloat(baseRaw, base.precision);
|
|
147
|
+
const quoteReserve = blockchainToFloat(quoteRaw, quote.precision);
|
|
148
|
+
if (!(baseReserve > 0) || !(quoteReserve > 0))
|
|
149
|
+
return null;
|
|
150
|
+
// Chain stores the taker fee in 1/10000 units (50 => 0.5%).
|
|
151
|
+
const feePercent = typeof pool.taker_fee_percent === 'number'
|
|
152
|
+
? pool.taker_fee_percent / 100
|
|
153
|
+
: null;
|
|
154
|
+
return {
|
|
155
|
+
id: pool.id,
|
|
156
|
+
shareAsset: pool.share_asset,
|
|
157
|
+
baseReserve,
|
|
158
|
+
quoteReserve,
|
|
159
|
+
feePercent,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Decode and select a pool. With `poolId`, only that pool is considered;
|
|
164
|
+
* otherwise the deepest pool (largest quote reserve) wins.
|
|
165
|
+
*/
|
|
166
|
+
function selectPool(pools, base, quote, poolId) {
|
|
167
|
+
const wanted = poolId ? String(poolId).trim() : null;
|
|
168
|
+
const decoded = pools
|
|
169
|
+
.filter((p) => !wanted || p.id === wanted)
|
|
170
|
+
.map((p) => decodePool(p, base, quote))
|
|
171
|
+
.filter((p) => p != null);
|
|
172
|
+
if (decoded.length === 0)
|
|
173
|
+
return null;
|
|
174
|
+
decoded.sort((a, b) => b.quoteReserve - a.quoteReserve);
|
|
175
|
+
return decoded[0];
|
|
176
|
+
}
|
|
177
|
+
export { resolveAsset, fetchOrderBook, normalizeLimitOrders, fetchPools, resolvePool, decodePool, selectPool };
|
|
178
|
+
//# sourceMappingURL=liquidity_chain.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BookStats, DepthBuildResult, PoolState, PriceWindow } from './liquidity_depth_model.js';
|
|
2
|
+
interface ChartAsset {
|
|
3
|
+
symbol: string;
|
|
4
|
+
id?: string;
|
|
5
|
+
precision?: number;
|
|
6
|
+
}
|
|
7
|
+
interface LiquidityChartInput {
|
|
8
|
+
base: ChartAsset;
|
|
9
|
+
quote: ChartAsset;
|
|
10
|
+
pool: PoolState | null;
|
|
11
|
+
book: BookStats;
|
|
12
|
+
series: DepthBuildResult;
|
|
13
|
+
window: PriceWindow;
|
|
14
|
+
generatedAt?: string;
|
|
15
|
+
}
|
|
16
|
+
declare function generateLiquidityHTML(input: LiquidityChartInput, title?: string): string;
|
|
17
|
+
export { generateLiquidityHTML };
|
|
18
|
+
export type { LiquidityChartInput, ChartAsset };
|
|
19
|
+
//# sourceMappingURL=liquidity_chart_generator.d.ts.map
|