@tradejs/infra 2.0.18 → 2.0.20
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/dist/ai.js +39 -74
- package/dist/ai.mjs +37 -74
- package/dist/chunk-2CZREG43.mjs +112 -0
- package/dist/chunk-DFMKDB2R.mjs +1285 -0
- package/dist/chunk-I2J6YDBD.mjs +910 -0
- package/dist/chunk-NWXFWTWU.mjs +1114 -0
- package/dist/chunk-SZQB7ER5.mjs +492 -0
- package/dist/chunk-YVIHTUV5.mjs +286 -0
- package/dist/coreResearch.d.mts +20 -0
- package/dist/coreResearch.d.ts +20 -0
- package/dist/coreResearch.js +89 -0
- package/dist/coreResearch.mjs +48 -0
- package/dist/internal-2coHaaos.d.mts +26 -0
- package/dist/internal-2coHaaos.d.ts +26 -0
- package/dist/ml.mjs +3 -3
- package/dist/runtimeDeployments.d.mts +10 -0
- package/dist/runtimeDeployments.d.ts +10 -0
- package/dist/runtimeDeployments.js +447 -0
- package/dist/runtimeDeployments.mjs +81 -0
- package/dist/runtimeStrategyConfigs.d.mts +28 -0
- package/dist/runtimeStrategyConfigs.d.ts +28 -0
- package/dist/runtimeStrategyConfigs.js +425 -0
- package/dist/runtimeStrategyConfigs.mjs +89 -0
- package/dist/strategyReleaseEvidence.d.mts +12 -0
- package/dist/strategyReleaseEvidence.d.ts +12 -0
- package/dist/strategyReleaseEvidence.js +120 -0
- package/dist/strategyReleaseEvidence.mjs +90 -0
- package/dist/timescale/candles.d.mts +38 -0
- package/dist/timescale/candles.d.ts +38 -0
- package/dist/timescale/candles.js +408 -0
- package/dist/timescale/candles.mjs +21 -0
- package/dist/timescale/client.d.mts +4 -0
- package/dist/timescale/client.d.ts +4 -0
- package/dist/timescale/client.js +109 -0
- package/dist/timescale/client.mjs +12 -0
- package/dist/timescale/derivatives.d.mts +90 -0
- package/dist/timescale/derivatives.d.ts +90 -0
- package/dist/timescale/derivatives.js +1270 -0
- package/dist/timescale/derivatives.mjs +26 -0
- package/dist/timescale/hyperliquidWhales.d.mts +149 -0
- package/dist/timescale/hyperliquidWhales.d.ts +149 -0
- package/dist/timescale/hyperliquidWhales.js +1893 -0
- package/dist/timescale/hyperliquidWhales.mjs +30 -0
- package/dist/timescale/marketContext.d.mts +188 -0
- package/dist/timescale/marketContext.d.ts +188 -0
- package/dist/timescale/marketContext.js +2091 -0
- package/dist/timescale/marketContext.mjs +60 -0
- package/dist/timescale/spread.d.mts +11 -0
- package/dist/timescale/spread.d.ts +11 -0
- package/dist/timescale/spread.js +215 -0
- package/dist/timescale/spread.mjs +11 -0
- package/dist/timescale.d.mts +9 -476
- package/dist/timescale.d.ts +9 -476
- package/dist/timescale.js +2121 -2112
- package/dist/timescale.mjs +73 -4070
- package/dist/tradingAccounts.d.mts +2 -8
- package/dist/tradingAccounts.d.ts +2 -8
- package/dist/tradingAccounts.js +0 -71
- package/dist/tradingAccounts.mjs +0 -65
- package/dist/values-BrvcmnfM.d.mts +6 -0
- package/dist/values-BrvcmnfM.d.ts +6 -0
- package/package.json +53 -2
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
// src/timescale/pool.ts
|
|
2
|
+
import { Pool } from "pg";
|
|
3
|
+
var getPool = () => {
|
|
4
|
+
if (!global.__pgPool__) {
|
|
5
|
+
const max = Number(process.env.PG_POOL_MAX ?? 10);
|
|
6
|
+
const connectionTimeoutMillis = Number(
|
|
7
|
+
process.env.PG_CONNECTION_TIMEOUT_MS ?? 3e4
|
|
8
|
+
);
|
|
9
|
+
global.__pgPool__ = new Pool({
|
|
10
|
+
host: process.env.PG_HOST || "127.0.0.1",
|
|
11
|
+
port: Number(process.env.PG_PORT ?? 5432),
|
|
12
|
+
user: process.env.PG_USER || "app",
|
|
13
|
+
password: String(process.env.PG_PASSWORD ?? "app"),
|
|
14
|
+
database: process.env.PG_DATABASE || process.env.PG_DB || "app",
|
|
15
|
+
max: Number.isFinite(max) && max > 0 ? Math.floor(max) : 10,
|
|
16
|
+
idleTimeoutMillis: 3e4,
|
|
17
|
+
connectionTimeoutMillis: Number.isFinite(connectionTimeoutMillis) && connectionTimeoutMillis > 0 ? Math.floor(connectionTimeoutMillis) : 3e4
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return global.__pgPool__;
|
|
21
|
+
};
|
|
22
|
+
var closePool = async () => {
|
|
23
|
+
const pool = global.__pgPool__;
|
|
24
|
+
if (!pool) return;
|
|
25
|
+
global.__pgPool__ = void 0;
|
|
26
|
+
await pool.end();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/timescale/query.ts
|
|
30
|
+
var resolveQueryTimeoutMs = (override) => {
|
|
31
|
+
if (Number.isFinite(override) && Number(override) > 0) {
|
|
32
|
+
return Math.floor(Number(override));
|
|
33
|
+
}
|
|
34
|
+
const configured = Number(process.env.MARKET_CONTEXT_SQL_TIMEOUT_MS);
|
|
35
|
+
return Number.isFinite(configured) && configured > 0 ? Math.floor(configured) : 3e4;
|
|
36
|
+
};
|
|
37
|
+
var createQueryError = (name, message) => {
|
|
38
|
+
const error = new Error(message);
|
|
39
|
+
error.name = name;
|
|
40
|
+
return error;
|
|
41
|
+
};
|
|
42
|
+
var queryMarketContext = async (text, values, options = {}) => {
|
|
43
|
+
const client = await getPool().connect();
|
|
44
|
+
const timeoutMs = resolveQueryTimeoutMs(options.timeoutMs);
|
|
45
|
+
let released = false;
|
|
46
|
+
let rejectCancellation;
|
|
47
|
+
const release = (error) => {
|
|
48
|
+
if (released) return;
|
|
49
|
+
released = true;
|
|
50
|
+
client.release(error);
|
|
51
|
+
};
|
|
52
|
+
const cancellation = new Promise((_resolve, reject) => {
|
|
53
|
+
rejectCancellation = reject;
|
|
54
|
+
});
|
|
55
|
+
const cancel = (error) => {
|
|
56
|
+
release(error);
|
|
57
|
+
rejectCancellation?.(error);
|
|
58
|
+
};
|
|
59
|
+
const onAbort = () => cancel(
|
|
60
|
+
createQueryError("AbortError", "Timescale market-context query aborted")
|
|
61
|
+
);
|
|
62
|
+
const timer = setTimeout(
|
|
63
|
+
() => cancel(
|
|
64
|
+
createQueryError(
|
|
65
|
+
"TimescaleQueryTimeoutError",
|
|
66
|
+
`Timescale market-context query exceeded ${timeoutMs}ms`
|
|
67
|
+
)
|
|
68
|
+
),
|
|
69
|
+
timeoutMs
|
|
70
|
+
);
|
|
71
|
+
timer.unref?.();
|
|
72
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
73
|
+
try {
|
|
74
|
+
if (options.signal?.aborted) {
|
|
75
|
+
const error = createQueryError(
|
|
76
|
+
"AbortError",
|
|
77
|
+
"Timescale market-context query aborted"
|
|
78
|
+
);
|
|
79
|
+
release(error);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
return await Promise.race([client.query(text, values), cancellation]);
|
|
83
|
+
} finally {
|
|
84
|
+
clearTimeout(timer);
|
|
85
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
86
|
+
release();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/timescale/values.ts
|
|
91
|
+
var PG_SAFE_MAX_BIND_PARAMS = 3e4;
|
|
92
|
+
var normalizeCandleProvider = (provider) => String(provider || "").trim().toLowerCase();
|
|
93
|
+
var normalizeCandleSymbol = (symbol) => String(symbol || "").trim().toUpperCase();
|
|
94
|
+
var getSafeBulkInsertRows = (columnsCount) => Math.max(1, Math.floor(PG_SAFE_MAX_BIND_PARAMS / columnsCount));
|
|
95
|
+
var toMarketFeatureAge = (rowTs, atMs) => {
|
|
96
|
+
const ageMs = atMs - rowTs.getTime();
|
|
97
|
+
return Number.isFinite(ageMs) ? ageMs : null;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/timescale/internal.ts
|
|
101
|
+
var candlesSchemaReady = false;
|
|
102
|
+
var derivativesSchemaReady = false;
|
|
103
|
+
var spreadSchemaReady = false;
|
|
104
|
+
var binanceMarketSchemaReady = false;
|
|
105
|
+
var hyperliquidWhaleSchemaReady = false;
|
|
106
|
+
var candlesSchemaReadyPromise = null;
|
|
107
|
+
var derivativesSchemaReadyPromise = null;
|
|
108
|
+
var spreadSchemaReadyPromise = null;
|
|
109
|
+
var binanceMarketSchemaReadyPromise = null;
|
|
110
|
+
var hyperliquidWhaleSchemaReadyPromise = null;
|
|
111
|
+
var marketContextSchemaMode = "ensure";
|
|
112
|
+
var verifiedMarketContextSchemas = /* @__PURE__ */ new Set();
|
|
113
|
+
var configureTimescaleMarketContextSchemaMode = (mode) => {
|
|
114
|
+
marketContextSchemaMode = mode;
|
|
115
|
+
verifiedMarketContextSchemas.clear();
|
|
116
|
+
};
|
|
117
|
+
var closeTimescalePool = async () => {
|
|
118
|
+
candlesSchemaReady = false;
|
|
119
|
+
derivativesSchemaReady = false;
|
|
120
|
+
spreadSchemaReady = false;
|
|
121
|
+
binanceMarketSchemaReady = false;
|
|
122
|
+
hyperliquidWhaleSchemaReady = false;
|
|
123
|
+
candlesSchemaReadyPromise = null;
|
|
124
|
+
derivativesSchemaReadyPromise = null;
|
|
125
|
+
spreadSchemaReadyPromise = null;
|
|
126
|
+
binanceMarketSchemaReadyPromise = null;
|
|
127
|
+
hyperliquidWhaleSchemaReadyPromise = null;
|
|
128
|
+
verifiedMarketContextSchemas.clear();
|
|
129
|
+
await closePool();
|
|
130
|
+
};
|
|
131
|
+
var CANDLES_SCHEMA_LOCK_KEY = 61e4;
|
|
132
|
+
var DERIVATIVES_SCHEMA_LOCK_KEY = 610001;
|
|
133
|
+
var SPREAD_SCHEMA_LOCK_KEY = 610002;
|
|
134
|
+
var BINANCE_MARKET_SCHEMA_LOCK_KEY = 610003;
|
|
135
|
+
var HYPERLIQUID_WHALE_SCHEMA_LOCK_KEY = 610004;
|
|
136
|
+
var withSchemaLock = async (lockKey, work) => {
|
|
137
|
+
const pool = getPool();
|
|
138
|
+
await pool.query("SELECT pg_advisory_lock($1)", [lockKey]);
|
|
139
|
+
try {
|
|
140
|
+
await work();
|
|
141
|
+
} finally {
|
|
142
|
+
await pool.query("SELECT pg_advisory_unlock($1)", [lockKey]);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
var ensureCandlesSchema = async () => {
|
|
146
|
+
if (candlesSchemaReady) return;
|
|
147
|
+
if (candlesSchemaReadyPromise) {
|
|
148
|
+
await candlesSchemaReadyPromise;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
candlesSchemaReadyPromise = withSchemaLock(
|
|
152
|
+
CANDLES_SCHEMA_LOCK_KEY,
|
|
153
|
+
async () => {
|
|
154
|
+
const pool = getPool();
|
|
155
|
+
await pool.query("CREATE EXTENSION IF NOT EXISTS timescaledb");
|
|
156
|
+
await pool.query(`
|
|
157
|
+
CREATE TABLE IF NOT EXISTS candles (
|
|
158
|
+
provider text NOT NULL DEFAULT 'bybit',
|
|
159
|
+
symbol text NOT NULL,
|
|
160
|
+
interval integer NOT NULL,
|
|
161
|
+
ts timestamptz NOT NULL,
|
|
162
|
+
open double precision NOT NULL,
|
|
163
|
+
high double precision NOT NULL,
|
|
164
|
+
low double precision NOT NULL,
|
|
165
|
+
close double precision NOT NULL,
|
|
166
|
+
volume double precision,
|
|
167
|
+
turnover double precision,
|
|
168
|
+
taker_buy_base_volume double precision,
|
|
169
|
+
taker_buy_quote_volume double precision,
|
|
170
|
+
taker_sell_base_volume double precision,
|
|
171
|
+
taker_sell_quote_volume double precision,
|
|
172
|
+
PRIMARY KEY (provider, symbol, interval, ts)
|
|
173
|
+
)
|
|
174
|
+
`);
|
|
175
|
+
await pool.query(`
|
|
176
|
+
SELECT create_hypertable(
|
|
177
|
+
'candles',
|
|
178
|
+
'ts',
|
|
179
|
+
if_not_exists => TRUE,
|
|
180
|
+
chunk_time_interval => interval '7 days'
|
|
181
|
+
)
|
|
182
|
+
`);
|
|
183
|
+
await pool.query(`
|
|
184
|
+
CREATE INDEX IF NOT EXISTS candles_provider_symbol_interval_ts_idx
|
|
185
|
+
ON candles (provider, symbol, interval, ts DESC)
|
|
186
|
+
`);
|
|
187
|
+
await pool.query(`
|
|
188
|
+
ALTER TABLE candles
|
|
189
|
+
ADD COLUMN IF NOT EXISTS taker_buy_base_volume double precision,
|
|
190
|
+
ADD COLUMN IF NOT EXISTS taker_buy_quote_volume double precision,
|
|
191
|
+
ADD COLUMN IF NOT EXISTS taker_sell_base_volume double precision,
|
|
192
|
+
ADD COLUMN IF NOT EXISTS taker_sell_quote_volume double precision
|
|
193
|
+
`);
|
|
194
|
+
candlesSchemaReady = true;
|
|
195
|
+
}
|
|
196
|
+
).finally(() => {
|
|
197
|
+
candlesSchemaReadyPromise = null;
|
|
198
|
+
});
|
|
199
|
+
await candlesSchemaReadyPromise;
|
|
200
|
+
};
|
|
201
|
+
var ensureDerivativesSchema = async () => {
|
|
202
|
+
if (derivativesSchemaReady) return;
|
|
203
|
+
if (derivativesSchemaReadyPromise) {
|
|
204
|
+
await derivativesSchemaReadyPromise;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const pool = getPool();
|
|
208
|
+
derivativesSchemaReadyPromise = withSchemaLock(
|
|
209
|
+
DERIVATIVES_SCHEMA_LOCK_KEY,
|
|
210
|
+
async () => {
|
|
211
|
+
if (derivativesSchemaReady) return;
|
|
212
|
+
await pool.query("CREATE EXTENSION IF NOT EXISTS timescaledb");
|
|
213
|
+
await pool.query(`
|
|
214
|
+
CREATE TABLE IF NOT EXISTS derivatives_market (
|
|
215
|
+
symbol text NOT NULL,
|
|
216
|
+
interval text NOT NULL,
|
|
217
|
+
ts timestamptz NOT NULL,
|
|
218
|
+
open_interest double precision,
|
|
219
|
+
funding_rate double precision,
|
|
220
|
+
liq_long double precision,
|
|
221
|
+
liq_short double precision,
|
|
222
|
+
liq_total double precision,
|
|
223
|
+
source text,
|
|
224
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
225
|
+
PRIMARY KEY (symbol, interval, ts)
|
|
226
|
+
)
|
|
227
|
+
`);
|
|
228
|
+
await pool.query(`
|
|
229
|
+
SELECT create_hypertable(
|
|
230
|
+
'derivatives_market',
|
|
231
|
+
'ts',
|
|
232
|
+
if_not_exists => TRUE,
|
|
233
|
+
chunk_time_interval => interval '14 days'
|
|
234
|
+
)
|
|
235
|
+
`);
|
|
236
|
+
await pool.query(`
|
|
237
|
+
CREATE INDEX IF NOT EXISTS derivatives_market_symbol_tf_ts_idx
|
|
238
|
+
ON derivatives_market (symbol, interval, ts DESC)
|
|
239
|
+
`);
|
|
240
|
+
await pool.query(`
|
|
241
|
+
CREATE TABLE IF NOT EXISTS derivatives_backfill_coverage (
|
|
242
|
+
source text NOT NULL,
|
|
243
|
+
symbol text NOT NULL,
|
|
244
|
+
interval text NOT NULL,
|
|
245
|
+
from_ts timestamptz NOT NULL,
|
|
246
|
+
to_ts timestamptz NOT NULL,
|
|
247
|
+
rows_count integer NOT NULL DEFAULT 0,
|
|
248
|
+
checked_at timestamptz NOT NULL DEFAULT now(),
|
|
249
|
+
PRIMARY KEY (source, symbol, interval, from_ts, to_ts)
|
|
250
|
+
)
|
|
251
|
+
`);
|
|
252
|
+
await pool.query(`
|
|
253
|
+
CREATE INDEX IF NOT EXISTS derivatives_backfill_coverage_lookup_idx
|
|
254
|
+
ON derivatives_backfill_coverage (source, symbol, interval, from_ts, to_ts)
|
|
255
|
+
`);
|
|
256
|
+
await pool.query(`
|
|
257
|
+
CREATE TABLE IF NOT EXISTS derivatives_metric_coverage (
|
|
258
|
+
source text NOT NULL,
|
|
259
|
+
metric text NOT NULL,
|
|
260
|
+
symbol text NOT NULL,
|
|
261
|
+
interval text NOT NULL,
|
|
262
|
+
from_ts timestamptz NOT NULL,
|
|
263
|
+
to_ts timestamptz NOT NULL,
|
|
264
|
+
event_rows_count integer NOT NULL DEFAULT 0,
|
|
265
|
+
zero_rows_count integer NOT NULL DEFAULT 0,
|
|
266
|
+
checked_at timestamptz NOT NULL DEFAULT now(),
|
|
267
|
+
PRIMARY KEY (source, metric, symbol, interval, from_ts, to_ts)
|
|
268
|
+
)
|
|
269
|
+
`);
|
|
270
|
+
await pool.query(`
|
|
271
|
+
CREATE INDEX IF NOT EXISTS derivatives_metric_coverage_lookup_idx
|
|
272
|
+
ON derivatives_metric_coverage (
|
|
273
|
+
source,
|
|
274
|
+
metric,
|
|
275
|
+
symbol,
|
|
276
|
+
interval,
|
|
277
|
+
from_ts,
|
|
278
|
+
to_ts
|
|
279
|
+
)
|
|
280
|
+
`);
|
|
281
|
+
derivativesSchemaReady = true;
|
|
282
|
+
}
|
|
283
|
+
).finally(() => {
|
|
284
|
+
derivativesSchemaReadyPromise = null;
|
|
285
|
+
});
|
|
286
|
+
await derivativesSchemaReadyPromise;
|
|
287
|
+
};
|
|
288
|
+
var ensureSpreadSchema = async () => {
|
|
289
|
+
if (spreadSchemaReady) return;
|
|
290
|
+
if (spreadSchemaReadyPromise) {
|
|
291
|
+
await spreadSchemaReadyPromise;
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const pool = getPool();
|
|
295
|
+
spreadSchemaReadyPromise = withSchemaLock(
|
|
296
|
+
SPREAD_SCHEMA_LOCK_KEY,
|
|
297
|
+
async () => {
|
|
298
|
+
if (spreadSchemaReady) return;
|
|
299
|
+
await pool.query("CREATE EXTENSION IF NOT EXISTS timescaledb");
|
|
300
|
+
await pool.query(`
|
|
301
|
+
CREATE TABLE IF NOT EXISTS market_spread (
|
|
302
|
+
symbol text NOT NULL,
|
|
303
|
+
interval text NOT NULL,
|
|
304
|
+
ts timestamptz NOT NULL,
|
|
305
|
+
binance_price double precision,
|
|
306
|
+
coinbase_price double precision,
|
|
307
|
+
spread double precision,
|
|
308
|
+
source text,
|
|
309
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
310
|
+
PRIMARY KEY (symbol, interval, ts)
|
|
311
|
+
)
|
|
312
|
+
`);
|
|
313
|
+
await pool.query(`
|
|
314
|
+
SELECT create_hypertable(
|
|
315
|
+
'market_spread',
|
|
316
|
+
'ts',
|
|
317
|
+
if_not_exists => TRUE,
|
|
318
|
+
chunk_time_interval => interval '14 days'
|
|
319
|
+
)
|
|
320
|
+
`);
|
|
321
|
+
await pool.query(`
|
|
322
|
+
CREATE INDEX IF NOT EXISTS market_spread_symbol_tf_ts_idx
|
|
323
|
+
ON market_spread (symbol, interval, ts DESC)
|
|
324
|
+
`);
|
|
325
|
+
spreadSchemaReady = true;
|
|
326
|
+
}
|
|
327
|
+
).finally(() => {
|
|
328
|
+
spreadSchemaReadyPromise = null;
|
|
329
|
+
});
|
|
330
|
+
await spreadSchemaReadyPromise;
|
|
331
|
+
};
|
|
332
|
+
var ensureBinanceMarketSchema = async () => {
|
|
333
|
+
if (binanceMarketSchemaReady) return;
|
|
334
|
+
if (binanceMarketSchemaReadyPromise) {
|
|
335
|
+
await binanceMarketSchemaReadyPromise;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const pool = getPool();
|
|
339
|
+
binanceMarketSchemaReadyPromise = withSchemaLock(
|
|
340
|
+
BINANCE_MARKET_SCHEMA_LOCK_KEY,
|
|
341
|
+
async () => {
|
|
342
|
+
if (binanceMarketSchemaReady) return;
|
|
343
|
+
await pool.query("CREATE EXTENSION IF NOT EXISTS timescaledb");
|
|
344
|
+
await pool.query(`
|
|
345
|
+
CREATE TABLE IF NOT EXISTS market_trade_flow (
|
|
346
|
+
symbol text NOT NULL,
|
|
347
|
+
interval text NOT NULL,
|
|
348
|
+
ts timestamptz NOT NULL,
|
|
349
|
+
trades integer NOT NULL,
|
|
350
|
+
buy_base_volume double precision,
|
|
351
|
+
sell_base_volume double precision,
|
|
352
|
+
buy_quote_volume double precision,
|
|
353
|
+
sell_quote_volume double precision,
|
|
354
|
+
net_base_delta double precision,
|
|
355
|
+
net_quote_delta double precision,
|
|
356
|
+
buy_pressure_pct double precision,
|
|
357
|
+
source text,
|
|
358
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
359
|
+
PRIMARY KEY (symbol, interval, ts)
|
|
360
|
+
)
|
|
361
|
+
`);
|
|
362
|
+
await pool.query(`
|
|
363
|
+
SELECT create_hypertable(
|
|
364
|
+
'market_trade_flow',
|
|
365
|
+
'ts',
|
|
366
|
+
if_not_exists => TRUE,
|
|
367
|
+
chunk_time_interval => interval '7 days'
|
|
368
|
+
)
|
|
369
|
+
`);
|
|
370
|
+
await pool.query(`
|
|
371
|
+
CREATE INDEX IF NOT EXISTS market_trade_flow_symbol_tf_ts_idx
|
|
372
|
+
ON market_trade_flow (symbol, interval, ts DESC)
|
|
373
|
+
`);
|
|
374
|
+
await pool.query(`
|
|
375
|
+
CREATE TABLE IF NOT EXISTS market_breadth (
|
|
376
|
+
universe text NOT NULL,
|
|
377
|
+
interval text NOT NULL,
|
|
378
|
+
ts timestamptz NOT NULL,
|
|
379
|
+
symbols_count integer NOT NULL,
|
|
380
|
+
advancers integer NOT NULL,
|
|
381
|
+
decliners integer NOT NULL,
|
|
382
|
+
unchanged integer NOT NULL,
|
|
383
|
+
advance_decline_ratio double precision,
|
|
384
|
+
pct_above_ma20 double precision,
|
|
385
|
+
pct_above_ma50 double precision,
|
|
386
|
+
equal_weighted_return double precision,
|
|
387
|
+
volume_weighted_return double precision,
|
|
388
|
+
dispersion double precision,
|
|
389
|
+
btc_return_1h double precision,
|
|
390
|
+
btc_return_4h double precision,
|
|
391
|
+
btc_return_24h double precision,
|
|
392
|
+
alt_basket_return_1h double precision,
|
|
393
|
+
alt_basket_return_4h double precision,
|
|
394
|
+
alt_basket_return_24h double precision,
|
|
395
|
+
btc_vs_alt_return_1h double precision,
|
|
396
|
+
btc_vs_alt_return_4h double precision,
|
|
397
|
+
btc_vs_alt_return_24h double precision,
|
|
398
|
+
btc_turnover_share_1h double precision,
|
|
399
|
+
btc_turnover_share_24h double precision,
|
|
400
|
+
btc_turnover_share_change_24h double precision,
|
|
401
|
+
alt_vol_to_btc_vol_24h double precision,
|
|
402
|
+
alt_dispersion_24h double precision,
|
|
403
|
+
btc_alt_regime text,
|
|
404
|
+
source text,
|
|
405
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
406
|
+
PRIMARY KEY (universe, interval, ts)
|
|
407
|
+
)
|
|
408
|
+
`);
|
|
409
|
+
await pool.query(`
|
|
410
|
+
ALTER TABLE market_breadth
|
|
411
|
+
ADD COLUMN IF NOT EXISTS btc_return_1h double precision,
|
|
412
|
+
ADD COLUMN IF NOT EXISTS btc_return_4h double precision,
|
|
413
|
+
ADD COLUMN IF NOT EXISTS btc_return_24h double precision,
|
|
414
|
+
ADD COLUMN IF NOT EXISTS alt_basket_return_1h double precision,
|
|
415
|
+
ADD COLUMN IF NOT EXISTS alt_basket_return_4h double precision,
|
|
416
|
+
ADD COLUMN IF NOT EXISTS alt_basket_return_24h double precision,
|
|
417
|
+
ADD COLUMN IF NOT EXISTS btc_vs_alt_return_1h double precision,
|
|
418
|
+
ADD COLUMN IF NOT EXISTS btc_vs_alt_return_4h double precision,
|
|
419
|
+
ADD COLUMN IF NOT EXISTS btc_vs_alt_return_24h double precision,
|
|
420
|
+
ADD COLUMN IF NOT EXISTS btc_turnover_share_1h double precision,
|
|
421
|
+
ADD COLUMN IF NOT EXISTS btc_turnover_share_24h double precision,
|
|
422
|
+
ADD COLUMN IF NOT EXISTS btc_turnover_share_change_24h double precision,
|
|
423
|
+
ADD COLUMN IF NOT EXISTS alt_vol_to_btc_vol_24h double precision,
|
|
424
|
+
ADD COLUMN IF NOT EXISTS alt_dispersion_24h double precision,
|
|
425
|
+
ADD COLUMN IF NOT EXISTS btc_alt_regime text
|
|
426
|
+
`);
|
|
427
|
+
await pool.query(`
|
|
428
|
+
SELECT create_hypertable(
|
|
429
|
+
'market_breadth',
|
|
430
|
+
'ts',
|
|
431
|
+
if_not_exists => TRUE,
|
|
432
|
+
chunk_time_interval => interval '14 days'
|
|
433
|
+
)
|
|
434
|
+
`);
|
|
435
|
+
await pool.query(`
|
|
436
|
+
CREATE INDEX IF NOT EXISTS market_breadth_universe_tf_ts_idx
|
|
437
|
+
ON market_breadth (universe, interval, ts DESC)
|
|
438
|
+
`);
|
|
439
|
+
await pool.query(`
|
|
440
|
+
CREATE TABLE IF NOT EXISTS market_global_context (
|
|
441
|
+
source text NOT NULL,
|
|
442
|
+
ts timestamptz NOT NULL,
|
|
443
|
+
updated_at_ts timestamptz,
|
|
444
|
+
active_cryptocurrencies integer,
|
|
445
|
+
active_exchanges integer,
|
|
446
|
+
active_market_pairs integer,
|
|
447
|
+
markets integer,
|
|
448
|
+
total_market_cap_usd double precision,
|
|
449
|
+
total_volume_usd double precision,
|
|
450
|
+
total_volume_reported_usd double precision,
|
|
451
|
+
btc_dominance_pct double precision,
|
|
452
|
+
eth_dominance_pct double precision,
|
|
453
|
+
alt_market_cap_usd double precision,
|
|
454
|
+
alt_volume_usd double precision,
|
|
455
|
+
alt_volume_reported_usd double precision,
|
|
456
|
+
btc_to_alt_market_cap_ratio double precision,
|
|
457
|
+
market_cap_change_pct_24h_usd double precision,
|
|
458
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
459
|
+
PRIMARY KEY (source, ts)
|
|
460
|
+
)
|
|
461
|
+
`);
|
|
462
|
+
await pool.query(`
|
|
463
|
+
SELECT create_hypertable(
|
|
464
|
+
'market_global_context',
|
|
465
|
+
'ts',
|
|
466
|
+
if_not_exists => TRUE,
|
|
467
|
+
chunk_time_interval => interval '30 days'
|
|
468
|
+
)
|
|
469
|
+
`);
|
|
470
|
+
await pool.query(`
|
|
471
|
+
CREATE INDEX IF NOT EXISTS market_global_context_source_ts_idx
|
|
472
|
+
ON market_global_context (source, ts DESC)
|
|
473
|
+
`);
|
|
474
|
+
await pool.query(`
|
|
475
|
+
ALTER TABLE market_global_context
|
|
476
|
+
ADD COLUMN IF NOT EXISTS active_exchanges integer,
|
|
477
|
+
ADD COLUMN IF NOT EXISTS active_market_pairs integer,
|
|
478
|
+
ADD COLUMN IF NOT EXISTS total_volume_reported_usd double precision,
|
|
479
|
+
ADD COLUMN IF NOT EXISTS alt_volume_usd double precision,
|
|
480
|
+
ADD COLUMN IF NOT EXISTS alt_volume_reported_usd double precision
|
|
481
|
+
`);
|
|
482
|
+
await pool.query(`
|
|
483
|
+
CREATE TABLE IF NOT EXISTS market_reference_asset_context (
|
|
484
|
+
source text NOT NULL,
|
|
485
|
+
symbol text NOT NULL,
|
|
486
|
+
cmc_id integer NOT NULL,
|
|
487
|
+
interval text NOT NULL,
|
|
488
|
+
ts timestamptz NOT NULL,
|
|
489
|
+
open_usd double precision,
|
|
490
|
+
high_usd double precision,
|
|
491
|
+
low_usd double precision,
|
|
492
|
+
close_usd double precision,
|
|
493
|
+
volume_usd double precision,
|
|
494
|
+
market_cap_usd double precision,
|
|
495
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
496
|
+
PRIMARY KEY (source, symbol, interval, ts)
|
|
497
|
+
)
|
|
498
|
+
`);
|
|
499
|
+
await pool.query(`
|
|
500
|
+
SELECT create_hypertable(
|
|
501
|
+
'market_reference_asset_context',
|
|
502
|
+
'ts',
|
|
503
|
+
if_not_exists => TRUE,
|
|
504
|
+
chunk_time_interval => interval '30 days'
|
|
505
|
+
)
|
|
506
|
+
`);
|
|
507
|
+
await pool.query(`
|
|
508
|
+
CREATE INDEX IF NOT EXISTS market_reference_asset_context_lookup_idx
|
|
509
|
+
ON market_reference_asset_context (source, symbol, interval, ts DESC)
|
|
510
|
+
`);
|
|
511
|
+
await pool.query(`
|
|
512
|
+
CREATE TABLE IF NOT EXISTS market_cmc_exchange_liquidity_context (
|
|
513
|
+
source text NOT NULL,
|
|
514
|
+
interval text NOT NULL,
|
|
515
|
+
ts timestamptz NOT NULL,
|
|
516
|
+
exchanges_count integer NOT NULL,
|
|
517
|
+
total_volume_usd double precision,
|
|
518
|
+
binance_volume_usd double precision,
|
|
519
|
+
binance_volume_share double precision,
|
|
520
|
+
top_exchange_volume_share double precision,
|
|
521
|
+
liquidity_regime text,
|
|
522
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
523
|
+
PRIMARY KEY (source, interval, ts)
|
|
524
|
+
)
|
|
525
|
+
`);
|
|
526
|
+
await pool.query(`
|
|
527
|
+
SELECT create_hypertable(
|
|
528
|
+
'market_cmc_exchange_liquidity_context',
|
|
529
|
+
'ts',
|
|
530
|
+
if_not_exists => TRUE,
|
|
531
|
+
chunk_time_interval => interval '30 days'
|
|
532
|
+
)
|
|
533
|
+
`);
|
|
534
|
+
await pool.query(`
|
|
535
|
+
CREATE INDEX IF NOT EXISTS market_cmc_exchange_liquidity_context_lookup_idx
|
|
536
|
+
ON market_cmc_exchange_liquidity_context (source, interval, ts DESC)
|
|
537
|
+
`);
|
|
538
|
+
await pool.query(`
|
|
539
|
+
CREATE TABLE IF NOT EXISTS market_cmc_fear_greed_context (
|
|
540
|
+
source text NOT NULL,
|
|
541
|
+
interval text NOT NULL,
|
|
542
|
+
ts timestamptz NOT NULL,
|
|
543
|
+
value integer NOT NULL,
|
|
544
|
+
classification text NOT NULL,
|
|
545
|
+
sentiment_regime text NOT NULL,
|
|
546
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
547
|
+
PRIMARY KEY (source, interval, ts)
|
|
548
|
+
)
|
|
549
|
+
`);
|
|
550
|
+
await pool.query(`
|
|
551
|
+
SELECT create_hypertable(
|
|
552
|
+
'market_cmc_fear_greed_context',
|
|
553
|
+
'ts',
|
|
554
|
+
if_not_exists => TRUE,
|
|
555
|
+
chunk_time_interval => interval '30 days'
|
|
556
|
+
)
|
|
557
|
+
`);
|
|
558
|
+
await pool.query(`
|
|
559
|
+
CREATE INDEX IF NOT EXISTS market_cmc_fear_greed_context_lookup_idx
|
|
560
|
+
ON market_cmc_fear_greed_context (source, interval, ts DESC)
|
|
561
|
+
`);
|
|
562
|
+
await pool.query(`
|
|
563
|
+
CREATE TABLE IF NOT EXISTS market_cmc_index_context (
|
|
564
|
+
source text NOT NULL,
|
|
565
|
+
index_slug text NOT NULL,
|
|
566
|
+
interval text NOT NULL,
|
|
567
|
+
ts timestamptz NOT NULL,
|
|
568
|
+
value double precision NOT NULL,
|
|
569
|
+
constituents_count integer,
|
|
570
|
+
top_constituent_symbol text,
|
|
571
|
+
top_constituent_weight_pct double precision,
|
|
572
|
+
constituents jsonb,
|
|
573
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
574
|
+
PRIMARY KEY (source, index_slug, interval, ts)
|
|
575
|
+
)
|
|
576
|
+
`);
|
|
577
|
+
await pool.query(`
|
|
578
|
+
SELECT create_hypertable(
|
|
579
|
+
'market_cmc_index_context',
|
|
580
|
+
'ts',
|
|
581
|
+
if_not_exists => TRUE,
|
|
582
|
+
chunk_time_interval => interval '30 days'
|
|
583
|
+
)
|
|
584
|
+
`);
|
|
585
|
+
await pool.query(`
|
|
586
|
+
CREATE INDEX IF NOT EXISTS market_cmc_index_context_lookup_idx
|
|
587
|
+
ON market_cmc_index_context (source, index_slug, interval, ts DESC)
|
|
588
|
+
`);
|
|
589
|
+
await pool.query(`
|
|
590
|
+
CREATE TABLE IF NOT EXISTS market_context_backfill_coverage (
|
|
591
|
+
source text NOT NULL,
|
|
592
|
+
scope text NOT NULL,
|
|
593
|
+
interval text NOT NULL,
|
|
594
|
+
from_ts timestamptz NOT NULL,
|
|
595
|
+
to_ts timestamptz NOT NULL,
|
|
596
|
+
rows_count integer NOT NULL DEFAULT 0,
|
|
597
|
+
checked_at timestamptz NOT NULL DEFAULT now(),
|
|
598
|
+
PRIMARY KEY (source, scope, interval, from_ts, to_ts)
|
|
599
|
+
)
|
|
600
|
+
`);
|
|
601
|
+
await pool.query(`
|
|
602
|
+
CREATE INDEX IF NOT EXISTS market_context_backfill_coverage_lookup_idx
|
|
603
|
+
ON market_context_backfill_coverage (source, scope, interval, from_ts, to_ts)
|
|
604
|
+
`);
|
|
605
|
+
binanceMarketSchemaReady = true;
|
|
606
|
+
}
|
|
607
|
+
).finally(() => {
|
|
608
|
+
binanceMarketSchemaReadyPromise = null;
|
|
609
|
+
});
|
|
610
|
+
await binanceMarketSchemaReadyPromise;
|
|
611
|
+
};
|
|
612
|
+
var ensureHyperliquidWhaleSchema = async () => {
|
|
613
|
+
if (hyperliquidWhaleSchemaReady) return;
|
|
614
|
+
if (hyperliquidWhaleSchemaReadyPromise) {
|
|
615
|
+
await hyperliquidWhaleSchemaReadyPromise;
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
hyperliquidWhaleSchemaReadyPromise = withSchemaLock(
|
|
619
|
+
HYPERLIQUID_WHALE_SCHEMA_LOCK_KEY,
|
|
620
|
+
async () => {
|
|
621
|
+
if (hyperliquidWhaleSchemaReady) return;
|
|
622
|
+
const pool = getPool();
|
|
623
|
+
await pool.query("CREATE EXTENSION IF NOT EXISTS timescaledb");
|
|
624
|
+
await pool.query(`
|
|
625
|
+
CREATE TABLE IF NOT EXISTS hyperliquid_whale_trade_events (
|
|
626
|
+
symbol text NOT NULL,
|
|
627
|
+
ts timestamptz NOT NULL,
|
|
628
|
+
tid text NOT NULL,
|
|
629
|
+
price double precision NOT NULL,
|
|
630
|
+
size double precision NOT NULL,
|
|
631
|
+
notional_usd double precision NOT NULL,
|
|
632
|
+
buyer_address text,
|
|
633
|
+
seller_address text,
|
|
634
|
+
buyer_tracked boolean NOT NULL,
|
|
635
|
+
seller_tracked boolean NOT NULL,
|
|
636
|
+
buyer_start_position double precision,
|
|
637
|
+
buyer_end_position double precision,
|
|
638
|
+
buyer_position_action text,
|
|
639
|
+
buyer_closed_pnl double precision,
|
|
640
|
+
buyer_liquidation boolean,
|
|
641
|
+
seller_start_position double precision,
|
|
642
|
+
seller_end_position double precision,
|
|
643
|
+
seller_position_action text,
|
|
644
|
+
seller_closed_pnl double precision,
|
|
645
|
+
seller_liquidation boolean,
|
|
646
|
+
universe_fingerprint text NOT NULL,
|
|
647
|
+
whale_registry_fingerprint text NOT NULL,
|
|
648
|
+
source text,
|
|
649
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
650
|
+
PRIMARY KEY (
|
|
651
|
+
universe_fingerprint,
|
|
652
|
+
whale_registry_fingerprint,
|
|
653
|
+
symbol,
|
|
654
|
+
ts,
|
|
655
|
+
tid
|
|
656
|
+
)
|
|
657
|
+
)
|
|
658
|
+
`);
|
|
659
|
+
await pool.query(`
|
|
660
|
+
ALTER TABLE hyperliquid_whale_trade_events
|
|
661
|
+
ADD COLUMN IF NOT EXISTS buyer_start_position double precision,
|
|
662
|
+
ADD COLUMN IF NOT EXISTS buyer_end_position double precision,
|
|
663
|
+
ADD COLUMN IF NOT EXISTS buyer_position_action text,
|
|
664
|
+
ADD COLUMN IF NOT EXISTS buyer_closed_pnl double precision,
|
|
665
|
+
ADD COLUMN IF NOT EXISTS buyer_liquidation boolean,
|
|
666
|
+
ADD COLUMN IF NOT EXISTS seller_start_position double precision,
|
|
667
|
+
ADD COLUMN IF NOT EXISTS seller_end_position double precision,
|
|
668
|
+
ADD COLUMN IF NOT EXISTS seller_position_action text,
|
|
669
|
+
ADD COLUMN IF NOT EXISTS seller_closed_pnl double precision,
|
|
670
|
+
ADD COLUMN IF NOT EXISTS seller_liquidation boolean
|
|
671
|
+
`);
|
|
672
|
+
await pool.query(`
|
|
673
|
+
SELECT create_hypertable(
|
|
674
|
+
'hyperliquid_whale_trade_events',
|
|
675
|
+
'ts',
|
|
676
|
+
if_not_exists => TRUE,
|
|
677
|
+
chunk_time_interval => interval '1 day'
|
|
678
|
+
)
|
|
679
|
+
`);
|
|
680
|
+
await pool.query(`
|
|
681
|
+
CREATE INDEX IF NOT EXISTS hyperliquid_whale_events_lookup_idx
|
|
682
|
+
ON hyperliquid_whale_trade_events (
|
|
683
|
+
universe_fingerprint,
|
|
684
|
+
whale_registry_fingerprint,
|
|
685
|
+
symbol,
|
|
686
|
+
ts DESC
|
|
687
|
+
)
|
|
688
|
+
`);
|
|
689
|
+
await pool.query(`
|
|
690
|
+
CREATE TABLE IF NOT EXISTS hyperliquid_whale_flow (
|
|
691
|
+
symbol text NOT NULL,
|
|
692
|
+
interval text NOT NULL,
|
|
693
|
+
ts timestamptz NOT NULL,
|
|
694
|
+
trades integer NOT NULL,
|
|
695
|
+
whale_sides integer NOT NULL,
|
|
696
|
+
unique_whales integer NOT NULL,
|
|
697
|
+
whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
698
|
+
buy_notional_usd double precision NOT NULL,
|
|
699
|
+
sell_notional_usd double precision NOT NULL,
|
|
700
|
+
net_notional_usd double precision NOT NULL,
|
|
701
|
+
buy_share_pct double precision,
|
|
702
|
+
position_aware_whale_sides integer NOT NULL DEFAULT 0,
|
|
703
|
+
long_entry_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
704
|
+
short_entry_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
705
|
+
long_exit_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
706
|
+
short_exit_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
707
|
+
long_entry_notional_usd double precision NOT NULL DEFAULT 0,
|
|
708
|
+
short_entry_notional_usd double precision NOT NULL DEFAULT 0,
|
|
709
|
+
long_exit_notional_usd double precision NOT NULL DEFAULT 0,
|
|
710
|
+
short_exit_notional_usd double precision NOT NULL DEFAULT 0,
|
|
711
|
+
entry_net_notional_usd double precision NOT NULL DEFAULT 0,
|
|
712
|
+
entry_long_share_pct double precision,
|
|
713
|
+
universe_fingerprint text NOT NULL,
|
|
714
|
+
whale_registry_fingerprint text NOT NULL,
|
|
715
|
+
source text,
|
|
716
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
717
|
+
PRIMARY KEY (
|
|
718
|
+
universe_fingerprint,
|
|
719
|
+
whale_registry_fingerprint,
|
|
720
|
+
symbol,
|
|
721
|
+
interval,
|
|
722
|
+
ts
|
|
723
|
+
)
|
|
724
|
+
)
|
|
725
|
+
`);
|
|
726
|
+
await pool.query(`
|
|
727
|
+
ALTER TABLE hyperliquid_whale_flow
|
|
728
|
+
ADD COLUMN IF NOT EXISTS position_aware_whale_sides integer NOT NULL DEFAULT 0,
|
|
729
|
+
ADD COLUMN IF NOT EXISTS long_entry_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
730
|
+
ADD COLUMN IF NOT EXISTS short_entry_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
731
|
+
ADD COLUMN IF NOT EXISTS long_exit_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
732
|
+
ADD COLUMN IF NOT EXISTS short_exit_whale_addresses text[] NOT NULL DEFAULT '{}',
|
|
733
|
+
ADD COLUMN IF NOT EXISTS long_entry_notional_usd double precision NOT NULL DEFAULT 0,
|
|
734
|
+
ADD COLUMN IF NOT EXISTS short_entry_notional_usd double precision NOT NULL DEFAULT 0,
|
|
735
|
+
ADD COLUMN IF NOT EXISTS long_exit_notional_usd double precision NOT NULL DEFAULT 0,
|
|
736
|
+
ADD COLUMN IF NOT EXISTS short_exit_notional_usd double precision NOT NULL DEFAULT 0,
|
|
737
|
+
ADD COLUMN IF NOT EXISTS entry_net_notional_usd double precision NOT NULL DEFAULT 0,
|
|
738
|
+
ADD COLUMN IF NOT EXISTS entry_long_share_pct double precision
|
|
739
|
+
`);
|
|
740
|
+
await pool.query(`
|
|
741
|
+
SELECT create_hypertable(
|
|
742
|
+
'hyperliquid_whale_flow',
|
|
743
|
+
'ts',
|
|
744
|
+
if_not_exists => TRUE,
|
|
745
|
+
chunk_time_interval => interval '7 days'
|
|
746
|
+
)
|
|
747
|
+
`);
|
|
748
|
+
await pool.query(`
|
|
749
|
+
CREATE INDEX IF NOT EXISTS hyperliquid_whale_flow_lookup_idx
|
|
750
|
+
ON hyperliquid_whale_flow (
|
|
751
|
+
universe_fingerprint,
|
|
752
|
+
whale_registry_fingerprint,
|
|
753
|
+
symbol,
|
|
754
|
+
interval,
|
|
755
|
+
ts DESC
|
|
756
|
+
)
|
|
757
|
+
`);
|
|
758
|
+
await pool.query(`
|
|
759
|
+
CREATE TABLE IF NOT EXISTS hyperliquid_whale_wallet_coverage (
|
|
760
|
+
universe_fingerprint text NOT NULL,
|
|
761
|
+
whale_registry_fingerprint text NOT NULL,
|
|
762
|
+
address text NOT NULL,
|
|
763
|
+
requested_from_ts timestamptz NOT NULL,
|
|
764
|
+
requested_to_ts timestamptz NOT NULL,
|
|
765
|
+
covered_from_ts timestamptz,
|
|
766
|
+
covered_to_ts timestamptz,
|
|
767
|
+
status text NOT NULL CHECK (status IN ('complete', 'truncated', 'failed')),
|
|
768
|
+
fills_count integer NOT NULL DEFAULT 0,
|
|
769
|
+
error text,
|
|
770
|
+
data_model_version integer NOT NULL DEFAULT 2,
|
|
771
|
+
checked_at timestamptz NOT NULL DEFAULT now(),
|
|
772
|
+
PRIMARY KEY (
|
|
773
|
+
universe_fingerprint,
|
|
774
|
+
whale_registry_fingerprint,
|
|
775
|
+
address,
|
|
776
|
+
requested_from_ts,
|
|
777
|
+
requested_to_ts
|
|
778
|
+
)
|
|
779
|
+
)
|
|
780
|
+
`);
|
|
781
|
+
await pool.query(`
|
|
782
|
+
ALTER TABLE hyperliquid_whale_wallet_coverage
|
|
783
|
+
ADD COLUMN IF NOT EXISTS data_model_version integer NOT NULL DEFAULT 2
|
|
784
|
+
`);
|
|
785
|
+
await pool.query(`
|
|
786
|
+
CREATE INDEX IF NOT EXISTS hyperliquid_whale_wallet_coverage_lookup_idx
|
|
787
|
+
ON hyperliquid_whale_wallet_coverage (
|
|
788
|
+
universe_fingerprint,
|
|
789
|
+
whale_registry_fingerprint,
|
|
790
|
+
address,
|
|
791
|
+
requested_from_ts,
|
|
792
|
+
requested_to_ts
|
|
793
|
+
)
|
|
794
|
+
`);
|
|
795
|
+
await pool.query(`
|
|
796
|
+
CREATE TABLE IF NOT EXISTS hyperliquid_whale_coverage_1m (
|
|
797
|
+
ts timestamptz NOT NULL,
|
|
798
|
+
covered_whales integer NOT NULL,
|
|
799
|
+
expected_whales integer NOT NULL,
|
|
800
|
+
coverage_pct double precision NOT NULL,
|
|
801
|
+
universe_fingerprint text NOT NULL,
|
|
802
|
+
whale_registry_fingerprint text NOT NULL,
|
|
803
|
+
source text,
|
|
804
|
+
data_model_version integer NOT NULL DEFAULT 2,
|
|
805
|
+
ingested_at timestamptz NOT NULL DEFAULT now(),
|
|
806
|
+
PRIMARY KEY (
|
|
807
|
+
universe_fingerprint,
|
|
808
|
+
whale_registry_fingerprint,
|
|
809
|
+
ts
|
|
810
|
+
)
|
|
811
|
+
)
|
|
812
|
+
`);
|
|
813
|
+
await pool.query(`
|
|
814
|
+
ALTER TABLE hyperliquid_whale_coverage_1m
|
|
815
|
+
ADD COLUMN IF NOT EXISTS data_model_version integer NOT NULL DEFAULT 2
|
|
816
|
+
`);
|
|
817
|
+
await pool.query(`
|
|
818
|
+
SELECT create_hypertable(
|
|
819
|
+
'hyperliquid_whale_coverage_1m',
|
|
820
|
+
'ts',
|
|
821
|
+
if_not_exists => TRUE,
|
|
822
|
+
chunk_time_interval => interval '7 days'
|
|
823
|
+
)
|
|
824
|
+
`);
|
|
825
|
+
await pool.query(`
|
|
826
|
+
CREATE INDEX IF NOT EXISTS hyperliquid_whale_coverage_1m_lookup_idx
|
|
827
|
+
ON hyperliquid_whale_coverage_1m (
|
|
828
|
+
universe_fingerprint,
|
|
829
|
+
whale_registry_fingerprint,
|
|
830
|
+
ts DESC
|
|
831
|
+
)
|
|
832
|
+
`);
|
|
833
|
+
hyperliquidWhaleSchemaReady = true;
|
|
834
|
+
}
|
|
835
|
+
).finally(() => {
|
|
836
|
+
hyperliquidWhaleSchemaReadyPromise = null;
|
|
837
|
+
});
|
|
838
|
+
await hyperliquidWhaleSchemaReadyPromise;
|
|
839
|
+
};
|
|
840
|
+
var ensureCoinMarketCapContextSchema = async () => ensureBinanceMarketSchema();
|
|
841
|
+
var ensureMarketContextSchema = async (source) => {
|
|
842
|
+
switch (source) {
|
|
843
|
+
case "binance":
|
|
844
|
+
return ensureBinanceMarketSchema();
|
|
845
|
+
case "coinmarketcap":
|
|
846
|
+
return ensureCoinMarketCapContextSchema();
|
|
847
|
+
case "derivatives":
|
|
848
|
+
return ensureDerivativesSchema();
|
|
849
|
+
case "hyperliquidWhales":
|
|
850
|
+
return ensureHyperliquidWhaleSchema();
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
var MARKET_CONTEXT_SCHEMA_TABLES = {
|
|
854
|
+
binance: ["market_trade_flow", "market_breadth"],
|
|
855
|
+
coinmarketcap: [
|
|
856
|
+
"market_global_context",
|
|
857
|
+
"market_reference_asset_context",
|
|
858
|
+
"market_cmc_exchange_liquidity_context",
|
|
859
|
+
"market_cmc_fear_greed_context",
|
|
860
|
+
"market_cmc_index_context"
|
|
861
|
+
],
|
|
862
|
+
derivatives: ["derivatives_market"],
|
|
863
|
+
hyperliquidWhales: [
|
|
864
|
+
"hyperliquid_whale_flow",
|
|
865
|
+
"hyperliquid_whale_coverage_1m"
|
|
866
|
+
]
|
|
867
|
+
};
|
|
868
|
+
var verifyMarketContextSchema = async (source) => {
|
|
869
|
+
if (verifiedMarketContextSchemas.has(source)) return;
|
|
870
|
+
const tables = MARKET_CONTEXT_SCHEMA_TABLES[source];
|
|
871
|
+
const result = await queryMarketContext(
|
|
872
|
+
`
|
|
873
|
+
SELECT table_name AS "tableName"
|
|
874
|
+
FROM unnest($1::text[]) AS requested(table_name)
|
|
875
|
+
WHERE to_regclass(requested.table_name) IS NULL
|
|
876
|
+
`,
|
|
877
|
+
[tables]
|
|
878
|
+
);
|
|
879
|
+
if (result.rows.length) {
|
|
880
|
+
throw new Error(
|
|
881
|
+
`Timescale ${source} schema is not prepared; missing: ${result.rows.map((row) => row.tableName).filter(Boolean).join(", ")}`
|
|
882
|
+
);
|
|
883
|
+
}
|
|
884
|
+
verifiedMarketContextSchemas.add(source);
|
|
885
|
+
};
|
|
886
|
+
var prepareMarketContextSchemaForRead = async (source) => marketContextSchemaMode === "verify" ? verifyMarketContextSchema(source) : ensureMarketContextSchema(source);
|
|
887
|
+
var ensureMarketContextSchemas = async (sources) => {
|
|
888
|
+
for (const source of new Set(sources)) {
|
|
889
|
+
await ensureMarketContextSchema(source);
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
|
|
893
|
+
export {
|
|
894
|
+
getPool,
|
|
895
|
+
queryMarketContext,
|
|
896
|
+
normalizeCandleProvider,
|
|
897
|
+
normalizeCandleSymbol,
|
|
898
|
+
getSafeBulkInsertRows,
|
|
899
|
+
toMarketFeatureAge,
|
|
900
|
+
configureTimescaleMarketContextSchemaMode,
|
|
901
|
+
closeTimescalePool,
|
|
902
|
+
ensureCandlesSchema,
|
|
903
|
+
ensureDerivativesSchema,
|
|
904
|
+
ensureSpreadSchema,
|
|
905
|
+
ensureBinanceMarketSchema,
|
|
906
|
+
ensureHyperliquidWhaleSchema,
|
|
907
|
+
ensureCoinMarketCapContextSchema,
|
|
908
|
+
prepareMarketContextSchemaForRead,
|
|
909
|
+
ensureMarketContextSchemas
|
|
910
|
+
};
|