liqproof 0.0.0-stage → 0.1.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +536 -2
  3. package/bugs/README.md +144 -0
  4. package/bugs/coin-gecko-liquidity-report.md +283 -0
  5. package/bugs/collect-ds-aggregate.mjs +213 -0
  6. package/bugs/collect-evidence.mjs +458 -0
  7. package/bugs/collect-gt-aggregate.mjs +215 -0
  8. package/bugs/collect-raw-rpc.mjs +165 -0
  9. package/bugs/collect-sweep-context.mjs +162 -0
  10. package/bugs/dexscreener-fake-liquidity-report.md +310 -0
  11. package/bugs/evidence/aggregator-liquidity-claims.json +648 -0
  12. package/bugs/evidence/aggregator-liquidity-claims.md +103 -0
  13. package/bugs/evidence/bat-native-pool-drained.json +136 -0
  14. package/bugs/evidence/bat-native-pool-drained.md +51 -0
  15. package/bugs/evidence/chz-impersonator-pool-drained.json +136 -0
  16. package/bugs/evidence/chz-impersonator-pool-drained.md +51 -0
  17. package/bugs/evidence/gt-stale-reserve-after-drain.json +141 -0
  18. package/bugs/evidence/gt-stale-reserve-after-drain.md +31 -0
  19. package/bugs/evidence/raw-rpc-transcript.json +683 -0
  20. package/bugs/evidence/raw-rpc-transcript.md +308 -0
  21. package/bugs/evidence/sweep-context.json +325 -0
  22. package/bugs/evidence/sweep-context.md +55 -0
  23. package/bugs/evidence/verified-aggregate.json +526 -0
  24. package/bugs/evidence/verified-aggregate.md +24 -0
  25. package/bugs/evidence/verified-ds-liquidity-usd.json +1197 -0
  26. package/bugs/evidence/verified-ds-liquidity-usd.md +250 -0
  27. package/bugs/evidence/verified-gt-reserve-in-usd.json +1002 -0
  28. package/bugs/evidence/verified-gt-reserve-in-usd.md +218 -0
  29. package/bugs/fix-bundle-titles.mjs +72 -0
  30. package/bugs/geckoterminal-stale-reserve-after-drain.md +137 -0
  31. package/data/dataset-summary.md +103 -0
  32. package/data/impersonators.json +12888 -0
  33. package/data/verified-impersonators.json +725 -0
  34. package/data/wallets.json +23 -0
  35. package/dist/base58.js +70 -0
  36. package/dist/base58.js.map +1 -0
  37. package/dist/canonical.js +372 -0
  38. package/dist/canonical.js.map +1 -0
  39. package/dist/cli.js +830 -0
  40. package/dist/cli.js.map +1 -0
  41. package/dist/curve.js +255 -0
  42. package/dist/curve.js.map +1 -0
  43. package/dist/dashboard.js +485 -0
  44. package/dist/dashboard.js.map +1 -0
  45. package/dist/keccak.js +197 -0
  46. package/dist/keccak.js.map +1 -0
  47. package/dist/mcp.js +604 -0
  48. package/dist/mcp.js.map +1 -0
  49. package/dist/operator.js +180 -0
  50. package/dist/operator.js.map +1 -0
  51. package/dist/pda.js +86 -0
  52. package/dist/pda.js.map +1 -0
  53. package/dist/receipt.js +897 -0
  54. package/dist/receipt.js.map +1 -0
  55. package/dist/render.js +335 -0
  56. package/dist/render.js.map +1 -0
  57. package/dist/rpc.js +377 -0
  58. package/dist/rpc.js.map +1 -0
  59. package/dist/sources.js +599 -0
  60. package/dist/sources.js.map +1 -0
  61. package/dist/store.js +330 -0
  62. package/dist/store.js.map +1 -0
  63. package/dist/tokenmeta.js +424 -0
  64. package/dist/tokenmeta.js.map +1 -0
  65. package/dist/util.js +273 -0
  66. package/dist/util.js.map +1 -0
  67. package/dist/vaults.js +510 -0
  68. package/dist/vaults.js.map +1 -0
  69. package/dist/wallet.js +404 -0
  70. package/dist/wallet.js.map +1 -0
  71. package/dist/warnings.js +41 -0
  72. package/dist/warnings.js.map +1 -0
  73. package/dist/worker.js +857 -0
  74. package/dist/worker.js.map +1 -0
  75. package/dist/x402.js +549 -0
  76. package/dist/x402.js.map +1 -0
  77. package/docs/HACKATHON-SUBMISSION.md +264 -0
  78. package/docs/liqproof-plan.md +221 -0
  79. package/docs/llms.txt +32 -0
  80. package/docs/mcp-registry.md +165 -0
  81. package/docs/screenshots/01-dashboard.png +0 -0
  82. package/docs/screenshots/02-receipt-drained.png +0 -0
  83. package/docs/screenshots/03-receipt-impersonator.png +0 -0
  84. package/docs/screenshots/04-receipt-canonical.png +0 -0
  85. package/docs/x402-pricing.md +171 -0
  86. package/docs/zero-xyz.md +250 -0
  87. package/out/6eftxVbSAunVEoxUWdGhPdxg5UdsJ8Wkwy5w5YFuxouw.json +1171 -0
  88. package/out/6eftxVbSAunVEoxUWdGhPdxg5UdsJ8Wkwy5w5YFuxouw.md +52 -0
  89. package/out/6eftxVbSAunVEoxUWdGhPdxg5UdsJ8Wkwy5w5YFuxouw.svg +25 -0
  90. package/out/BTNiaZb6HaEFWeEtND4SXtZ7T6mgtDwSryHTAj6PuPyp.json +3975 -0
  91. package/out/BTNiaZb6HaEFWeEtND4SXtZ7T6mgtDwSryHTAj6PuPyp.md +90 -0
  92. package/out/BTNiaZb6HaEFWeEtND4SXtZ7T6mgtDwSryHTAj6PuPyp.svg +25 -0
  93. package/out/ENGkoidj2jNs73A7GZuseVmnkRg6EVF9tDtViwPz8o85.json +1446 -0
  94. package/out/ENGkoidj2jNs73A7GZuseVmnkRg6EVF9tDtViwPz8o85.md +66 -0
  95. package/out/ENGkoidj2jNs73A7GZuseVmnkRg6EVF9tDtViwPz8o85.svg +25 -0
  96. package/out/dataset.csv +67 -0
  97. package/out/impersonation-table.md +156 -0
  98. package/out/index.html +1179 -0
  99. package/package.json +78 -4
  100. package/scripts/assert-no-canonical-accusations.mjs +193 -0
  101. package/scripts/backfill.mjs +1063 -0
  102. package/scripts/bundle-evidence.mjs +237 -0
  103. package/scripts/bundle-stale-claim.mjs +417 -0
  104. package/scripts/capture-bch-evidence.mjs +58 -0
  105. package/scripts/check-orientation-fix.mjs +40 -0
  106. package/scripts/check-wallet.mjs +54 -0
  107. package/scripts/daily-refresh.mjs +451 -0
  108. package/scripts/decode-clmm-vaults.mjs +180 -0
  109. package/scripts/falsifiability.mjs +263 -0
  110. package/scripts/impersonator-mining.mjs +156 -0
  111. package/scripts/inspect-matches.mjs +73 -0
  112. package/scripts/operator-sweep.mjs +252 -0
  113. package/scripts/publish-release.mjs +248 -0
  114. package/scripts/refresh-store.mjs +78 -0
  115. package/scripts/survival-miner.mjs +951 -0
  116. package/scripts/test-keccak.mjs +103 -0
  117. package/scripts/verify-bch-1p95b.mjs +124 -0
  118. package/scripts/verify-clmm-1p95b.mjs +127 -0
  119. package/scripts/verify-clmm-control.mjs +84 -0
  120. package/scripts/verify-impersonators.mjs +138 -0
  121. package/scripts/verify-raw.mjs +110 -0
  122. package/scripts/verify-raw2.mjs +99 -0
  123. package/server.json +34 -0
  124. package/src/base58.ts +68 -0
  125. package/src/canonical.ts +447 -0
  126. package/src/cli.ts +941 -0
  127. package/src/curve.ts +349 -0
  128. package/src/dashboard.ts +504 -0
  129. package/src/keccak.ts +228 -0
  130. package/src/mcp.ts +735 -0
  131. package/src/operator.ts +219 -0
  132. package/src/receipt.ts +1219 -0
  133. package/src/render.ts +396 -0
  134. package/src/rpc.ts +435 -0
  135. package/src/sources.ts +714 -0
  136. package/src/store.ts +428 -0
  137. package/src/tokenmeta.ts +460 -0
  138. package/src/util.ts +276 -0
  139. package/src/vaults.ts +574 -0
  140. package/src/wallet.ts +459 -0
  141. package/src/warnings.ts +40 -0
  142. package/src/worker.ts +1086 -0
  143. package/src/x402.ts +717 -0
package/src/curve.ts ADDED
@@ -0,0 +1,349 @@
1
+ /**
2
+ * curve.ts — reconstruct the historical liquidity curve of a pool, from
3
+ * on-chain transaction metadata only (no archive node, no paid indexer).
4
+ *
5
+ * The trick: every Solana transaction carries `meta.preTokenBalances` and
6
+ * `meta.postTokenBalances` — the SPL token balances of every account the
7
+ * transaction touched, before and after. A pool's vault token accounts are
8
+ * touched by every swap, so the absolute vault balance at any historical
9
+ * slot can be recovered by reading ONE transaction.
10
+ *
11
+ * THE RATE-LIMIT PROBLEM:
12
+ * `getTransaction` is limited to 10 calls / 10s on the free public tier.
13
+ * A 2,800-tx pool would take ~47 minutes to scan linearly.
14
+ *
15
+ * THE SOLUTION — bisect the drain:
16
+ * The signature list is ordered newest → oldest. The vault balance as a
17
+ * function of index is a step function. We binary-search for the index
18
+ * where the balance falls off a cliff, spending O(log n) calls
19
+ * (~12 for 2,800 txs instead of 2,800).
20
+ *
21
+ * We also take a sparse linear sample around the cliff to render a real
22
+ * timeline, and we can afford a full scan for pools under `fullScanMax`.
23
+ *
24
+ * Result: a timestamped, slot-stamped liquidity curve ending exactly at the
25
+ * drain transaction. This is what nobody in the market publishes:
26
+ * CoinUFO has 0 timeline events, RugCheck returns `events: []`.
27
+ */
28
+
29
+ import { RpcPool } from './rpc.js';
30
+ import { accountKeyMap } from './vaults.js';
31
+
32
+ export interface CurvePoint {
33
+ slot: number;
34
+ timestamp: number | null;
35
+ signature: string;
36
+ /** vault balance AFTER this tx, per mint (raw amounts) */
37
+ balances: Record<string, string>;
38
+ /** vault balance BEFORE this tx */
39
+ before: Record<string, string>;
40
+ /** USD valuation of the vault balances after this tx */
41
+ usdValue: number;
42
+ /** net USD change caused by this tx */
43
+ deltaUsd: number;
44
+ /** index in the signature list (0 = newest) */
45
+ index: number;
46
+ }
47
+
48
+ export interface LiquidityCurve {
49
+ pool: string;
50
+ points: CurvePoint[];
51
+ oldestSlot: number | null;
52
+ newestSlot: number | null;
53
+ /** the tx where liquidity collapsed the most */
54
+ drain?: CurvePoint;
55
+ /** total txs scanned */
56
+ scanned: number;
57
+ /** txs whose metadata was unavailable (non-archive node / pruned) */
58
+ unavailable: number;
59
+ truncated: boolean;
60
+ /** how the curve was obtained */
61
+ strategy: 'full' | 'bisect' | 'sample';
62
+ /** signatures available in total */
63
+ totalSignatures: number;
64
+ }
65
+
66
+ export interface CurveOptions {
67
+ /** max signatures to pull (paginated) */
68
+ maxSignatures?: number;
69
+ /** max txs to actually fetch (the newest N) */
70
+ maxTransactions?: number;
71
+ /** price map: mint → USD, used for valuation */
72
+ prices?: Record<string, number>;
73
+ /** stop early once we've scanned this many */
74
+ stopAfter?: number;
75
+ /** full linear scan below this many signatures (cheap enough) */
76
+ fullScanMax?: number;
77
+ /** number of points to sample around a detected cliff */
78
+ samplePoints?: number;
79
+ onProgress?: (done: number, total: number) => void;
80
+ }
81
+
82
+ interface TokenBalance {
83
+ accountIndex: number;
84
+ mint: string;
85
+ owner?: string;
86
+ amount: string;
87
+ decimals: number;
88
+ uiAmount: number;
89
+ }
90
+
91
+ /** Fetch all signatures for an address, newest → oldest, paginated. */
92
+ export async function fetchSignatures(
93
+ rpc: RpcPool,
94
+ address: string,
95
+ max: number,
96
+ ): Promise<Array<{ signature: string; slot: number; blockTime: number | null; err: any }>> {
97
+ const out: Array<{ signature: string; slot: number; blockTime: number | null; err: any }> = [];
98
+ let before: string | undefined;
99
+
100
+ while (out.length < max) {
101
+ const params: any[] = before
102
+ ? [address, { limit: Math.min(1000, max - out.length), before }]
103
+ : [address, { limit: Math.min(1000, max - out.length) }];
104
+ const pageRes = await rpc.callDetailed<any[]>('getSignaturesForAddress', params);
105
+ if (!pageRes.ok) break;
106
+ const page = pageRes.value;
107
+ if (!page || page.length === 0) break;
108
+ out.push(
109
+ ...page.map((s) => ({
110
+ signature: s.signature,
111
+ slot: s.slot,
112
+ blockTime: s.blockTime ?? null,
113
+ err: s.err ?? null,
114
+ })),
115
+ );
116
+ if (page.length < 1000) break;
117
+ before = page[page.length - 1]!.signature;
118
+ }
119
+ return out.slice(0, max);
120
+ }
121
+
122
+ function indexBalances(list: any[] | undefined): Map<string, TokenBalance> {
123
+ const map = new Map<string, TokenBalance>();
124
+ for (const b of list ?? []) {
125
+ const raw = b.uiTokenAmount?.amount;
126
+ if (raw === undefined) continue;
127
+ map.set(String(b.accountIndex), {
128
+ accountIndex: Number(b.accountIndex),
129
+ mint: String(b.mint),
130
+ owner: b.owner,
131
+ amount: String(raw),
132
+ decimals: Number(b.uiTokenAmount?.decimals ?? 0),
133
+ uiAmount: Number(b.uiTokenAmount?.uiAmount ?? 0),
134
+ });
135
+ }
136
+ return map;
137
+ }
138
+
139
+ /** Read the vault balances touched by one transaction. */
140
+ async function readPoint(
141
+ rpc: RpcPool,
142
+ sig: { signature: string; slot: number; blockTime: number | null },
143
+ index: number,
144
+ vaultAddresses: string[],
145
+ vaultMints: Record<string, string>,
146
+ prices: Record<string, number>,
147
+ ): Promise<CurvePoint | null> {
148
+ const txRes = await rpc.callDetailed<any>('getTransaction', [
149
+ sig.signature,
150
+ { encoding: 'jsonParsed', maxSupportedTransactionVersion: 1 },
151
+ ]);
152
+ if (!txRes.ok) return null;
153
+ const tx = txRes.value;
154
+ if (!tx || !tx.meta) return null;
155
+
156
+ const pre = indexBalances(tx.meta.preTokenBalances);
157
+ const post = indexBalances(tx.meta.postTokenBalances);
158
+ const keys = accountKeyMap(tx);
159
+
160
+ const balances: Record<string, string> = {};
161
+ const before: Record<string, string> = {};
162
+ let usdValue = 0;
163
+ let deltaUsd = 0;
164
+
165
+ for (const vault of vaultAddresses) {
166
+ const mint = vaultMints[vault];
167
+ if (!mint) continue;
168
+
169
+ let postBal: TokenBalance | undefined;
170
+ let preBal: TokenBalance | undefined;
171
+ for (const b of post.values()) {
172
+ if (keys[b.accountIndex] === vault) {
173
+ postBal = b;
174
+ break;
175
+ }
176
+ }
177
+ for (const b of pre.values()) {
178
+ if (keys[b.accountIndex] === vault) {
179
+ preBal = b;
180
+ break;
181
+ }
182
+ }
183
+ if (!postBal && !preBal) continue;
184
+
185
+ const postAmt = postBal?.amount ?? '0';
186
+ const preAmt = preBal?.amount ?? '0';
187
+ balances[mint] = postAmt;
188
+ before[mint] = preAmt;
189
+
190
+ const price = prices[mint] ?? 0;
191
+ const dec = postBal?.decimals ?? preBal?.decimals ?? 0;
192
+ const scale = 10 ** dec;
193
+ const postVal = (Number(postAmt) / scale) * price;
194
+ const preVal = (Number(preAmt) / scale) * price;
195
+ usdValue += postVal;
196
+ deltaUsd += postVal - preVal;
197
+ }
198
+
199
+ if (Object.keys(balances).length === 0) return null;
200
+
201
+ return {
202
+ slot: sig.slot,
203
+ timestamp: sig.blockTime,
204
+ signature: sig.signature,
205
+ balances,
206
+ before,
207
+ usdValue,
208
+ deltaUsd,
209
+ index,
210
+ };
211
+ }
212
+
213
+ /**
214
+ * Reconstruct the liquidity curve of `pool`.
215
+ * `vaultAddresses` maps the vault token-account addresses to their mints.
216
+ */
217
+ export async function liquidityCurve(
218
+ rpc: RpcPool,
219
+ pool: string,
220
+ vaultAddresses: string[],
221
+ vaultMints: Record<string, string>,
222
+ opts: CurveOptions = {},
223
+ ): Promise<LiquidityCurve> {
224
+ const maxSignatures = opts.maxSignatures ?? 5000;
225
+ const maxTransactions = opts.maxTransactions ?? 600;
226
+ const fullScanMax = opts.fullScanMax ?? 120;
227
+ const samplePoints = opts.samplePoints ?? 14;
228
+ const prices = opts.prices ?? {};
229
+
230
+ const sigs = await fetchSignatures(rpc, pool, maxSignatures);
231
+ const truncated = sigs.length >= maxSignatures;
232
+ const totalSignatures = sigs.length;
233
+
234
+ const points: CurvePoint[] = [];
235
+ let unavailable = 0;
236
+ let done = 0;
237
+
238
+ const read = async (idx: number): Promise<CurvePoint | null> => {
239
+ const sig = sigs[idx];
240
+ if (!sig) return null;
241
+ done++;
242
+ const p = await readPoint(rpc, sig, idx, vaultAddresses, vaultMints, prices);
243
+ if (!p) unavailable++;
244
+ opts.onProgress?.(done, Math.min(sigs.length, maxTransactions));
245
+ return p;
246
+ };
247
+
248
+ // ── strategy 1: full linear scan for small pools ────────────────────────
249
+ if (sigs.length <= fullScanMax) {
250
+ for (let i = 0; i < Math.min(sigs.length, maxTransactions); i++) {
251
+ const p = await read(i);
252
+ if (p) points.push(p);
253
+ }
254
+ points.reverse();
255
+ return finalize(pool, points, done, unavailable, truncated, 'full', totalSignatures);
256
+ }
257
+
258
+ // ── strategy 2: bisect for the liquidity cliff ──────────────────────────
259
+ // Probe a logarithmic ladder first to find the biggest balance drop.
260
+ const ladder: number[] = [];
261
+ let step = 1;
262
+ while (step < sigs.length) {
263
+ ladder.push(step);
264
+ step *= 2;
265
+ }
266
+ ladder.push(sigs.length - 1);
267
+
268
+ const probed = new Map<number, CurvePoint>();
269
+ for (const idx of ladder) {
270
+ const p = await read(idx);
271
+ if (p) probed.set(idx, p);
272
+ }
273
+
274
+ // Find the adjacent pair with the largest negative delta.
275
+ const sorted = [...probed.entries()].sort((a, b) => a[0] - b[0]);
276
+ let cliffLow = -1;
277
+ let cliffHigh = -1;
278
+ let bestDelta = 0;
279
+ for (let i = 0; i + 1 < sorted.length; i++) {
280
+ const [loIdx, lo] = sorted[i]!;
281
+ const [hiIdx, hi] = sorted[i + 1]!;
282
+ // `hi` is OLDER than `lo`. A drain shows up as: older balance >> newer balance.
283
+ const drop = hi.usdValue - lo.usdValue;
284
+ if (drop > bestDelta) {
285
+ bestDelta = drop;
286
+ cliffLow = hiIdx;
287
+ cliffHigh = loIdx;
288
+ }
289
+ }
290
+
291
+ const selected = new Map<number, CurvePoint>(probed);
292
+
293
+ // Narrow the cliff with a proper binary search between cliffLow (older,
294
+ // high balance) and cliffHigh (newer, low balance).
295
+ if (cliffLow >= 0 && cliffHigh > cliffLow) {
296
+ let lo = cliffLow;
297
+ let hi = cliffHigh;
298
+ while (hi - lo > 1) {
299
+ const mid = Math.floor((lo + hi) / 2);
300
+ const p = await read(mid);
301
+ if (p) selected.set(mid, p);
302
+ // mid is between lo (older, high) and hi (newer, low)
303
+ if (p && p.usdValue > 0.5 * (selected.get(lo)?.usdValue ?? 0)) lo = mid;
304
+ else hi = mid;
305
+ }
306
+ }
307
+
308
+ // ── strategy 3: sparse linear sample for a readable timeline ───────────
309
+ const sampleCount = Math.min(samplePoints, Math.max(8, Math.floor(sigs.length / 40)));
310
+ const sampleStride = Math.max(1, Math.floor(sigs.length / sampleCount));
311
+ for (let i = 0; i < sigs.length; i += sampleStride) {
312
+ if (selected.has(i)) continue;
313
+ const p = await read(i);
314
+ if (p) selected.set(i, p);
315
+ }
316
+
317
+ const ordered = [...selected.entries()]
318
+ .sort((a, b) => b[0] - a[0]) // oldest first (higher index = older)
319
+ .map(([, p]) => p);
320
+
321
+ return finalize(pool, ordered, done, unavailable, truncated, 'bisect', totalSignatures);
322
+ }
323
+
324
+ function finalize(
325
+ pool: string,
326
+ points: CurvePoint[],
327
+ scanned: number,
328
+ unavailable: number,
329
+ truncated: boolean,
330
+ strategy: LiquidityCurve['strategy'],
331
+ totalSignatures: number,
332
+ ): LiquidityCurve {
333
+ let drain: CurvePoint | undefined;
334
+ for (const p of points) {
335
+ if (!drain || p.deltaUsd < drain.deltaUsd) drain = p;
336
+ }
337
+ return {
338
+ pool,
339
+ points,
340
+ oldestSlot: points.length ? points[0]!.slot : null,
341
+ newestSlot: points.length ? points[points.length - 1]!.slot : null,
342
+ drain: drain && drain.deltaUsd < 0 ? drain : undefined,
343
+ scanned,
344
+ unavailable,
345
+ truncated,
346
+ strategy,
347
+ totalSignatures,
348
+ };
349
+ }