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/util.ts ADDED
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Shared helpers: resilient HTTP JSON fetching, TTL cache, formatting.
3
+ * Everything here is deliberately dependency-free so the $0 budget is
4
+ * structurally guaranteed (no paid SDK, no paid API client).
5
+ */
6
+
7
+ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
8
+
9
+ export interface FetchJsonOptions {
10
+ timeoutMs?: number;
11
+ retries?: number;
12
+ headers?: Record<string, string>;
13
+ method?: string;
14
+ body?: string;
15
+ }
16
+
17
+ export class HttpError extends Error {
18
+ constructor(
19
+ public readonly status: number,
20
+ public readonly body: string,
21
+ public readonly url: string,
22
+ ) {
23
+ super(`HTTP ${status} for ${url}`);
24
+ this.name = 'HttpError';
25
+ }
26
+ }
27
+
28
+ const sleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
29
+
30
+ /**
31
+ * GET/POST JSON with timeout + exponential backoff on 429/5xx.
32
+ * Retry-After is honoured when present.
33
+ */
34
+ export async function fetchJson<T = unknown>(url: string, opts: FetchJsonOptions = {}): Promise<T> {
35
+ const timeoutMs = opts.timeoutMs ?? 20_000;
36
+ const retries = opts.retries ?? 3;
37
+ let lastErr: unknown;
38
+
39
+ for (let attempt = 0; attempt <= retries; attempt++) {
40
+ const controller = new AbortController();
41
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
42
+ try {
43
+ const res = await fetch(url, {
44
+ method: opts.method ?? 'GET',
45
+ headers: {
46
+ accept: 'application/json',
47
+ 'user-agent': 'liqproof/0.1 (+read-only on-chain receipts)',
48
+ ...(opts.headers ?? {}),
49
+ },
50
+ body: opts.body,
51
+ signal: controller.signal,
52
+ });
53
+
54
+ const text = await res.text();
55
+ if (!res.ok) {
56
+ const err = new HttpError(res.status, text.slice(0, 500), url);
57
+ if ((res.status === 429 || res.status >= 500) && attempt < retries) {
58
+ const retryAfter = Number(res.headers.get('retry-after') ?? 0);
59
+ await sleep(retryAfter > 0 ? retryAfter * 1000 : 600 * 2 ** attempt);
60
+ lastErr = err;
61
+ continue;
62
+ }
63
+ throw err;
64
+ }
65
+
66
+ try {
67
+ return JSON.parse(text) as T;
68
+ } catch {
69
+ throw new Error(`invalid JSON from ${url}: ${text.slice(0, 200)}`);
70
+ }
71
+ } catch (err) {
72
+ lastErr = err;
73
+ if (err instanceof HttpError && err.status < 500 && err.status !== 429) throw err;
74
+ if (attempt === retries) break;
75
+ await sleep(600 * 2 ** attempt);
76
+ } finally {
77
+ clearTimeout(timer);
78
+ }
79
+ }
80
+ throw lastErr instanceof Error ? lastErr : new Error(String(lastErr));
81
+ }
82
+
83
+ /** Tiny in-memory TTL cache — avoids burning free-tier rate limits. */
84
+ export class TtlCache<T> {
85
+ private store = new Map<string, { value: T; expires: number }>();
86
+
87
+ constructor(private readonly ttlMs: number) {}
88
+
89
+ get(key: string): T | undefined {
90
+ const hit = this.store.get(key);
91
+ if (!hit) return undefined;
92
+ if (Date.now() > hit.expires) {
93
+ this.store.delete(key);
94
+ return undefined;
95
+ }
96
+ return hit.value;
97
+ }
98
+
99
+ set(key: string, value: T): void {
100
+ this.store.set(key, { value, expires: Date.now() + this.ttlMs });
101
+ }
102
+
103
+ async wrap(key: string, fn: () => Promise<T>): Promise<T> {
104
+ const hit = this.get(key);
105
+ if (hit !== undefined) return hit;
106
+ const value = await fn();
107
+ this.set(key, value);
108
+ return value;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Disk-backed TTL cache.
114
+ *
115
+ * The free aggregator tiers rate-limit hard — we measured GeckoTerminal
116
+ * answering 429 for a whole run, which silently degraded a receipt to a
117
+ * smaller pool and produced a misleading headline. Persisting responses to
118
+ * disk means a rate-limited run can still serve the last known good claim,
119
+ * and a re-run costs no API calls at all.
120
+ *
121
+ * Storage: `.liqproof/cache/<sha1-of-key>.json`, one file per entry, so the
122
+ * cache is inspectable, greppable and safe to delete.
123
+ */
124
+ export class DiskCache<T> {
125
+ private dir: string;
126
+ private mem = new TtlCache<T>(60_000);
127
+
128
+ constructor(
129
+ private readonly ttlMs: number,
130
+ dir = '.liqproof/cache',
131
+ ) {
132
+ this.dir = dir;
133
+ }
134
+
135
+ private pathFor(key: string): string {
136
+ const safe = key.replace(/[^a-zA-Z0-9._-]/g, '_').slice(0, 80);
137
+ const hash = hash32(key);
138
+ return `${this.dir}/${safe}-${hash}.json`;
139
+ }
140
+
141
+ get(key: string): T | undefined {
142
+ const hit = this.mem.get(key);
143
+ if (hit !== undefined) return hit;
144
+ const parsed = this.readEntry(key);
145
+ if (!parsed) return undefined;
146
+ if (Date.now() > parsed.expires) return undefined;
147
+ this.mem.set(key, parsed.value);
148
+ return parsed.value;
149
+ }
150
+
151
+ /**
152
+ * Read an entry even if it has expired.
153
+ *
154
+ * This is the fallback that keeps a receipt honest when an aggregator
155
+ * rate-limits us: a claim that was true 10 minutes ago is far better than
156
+ * no claim at all, and we label the receipt with the age of the data.
157
+ */
158
+ getStale(key: string): { value: T; expires: number } | undefined {
159
+ return this.readEntry(key);
160
+ }
161
+
162
+ private readEntry(key: string): { value: T; expires: number } | undefined {
163
+ try {
164
+ const raw = readFileSync(this.pathFor(key), 'utf8');
165
+ const parsed = JSON.parse(raw) as { value: T; expires: number };
166
+ return parsed;
167
+ } catch {
168
+ return undefined;
169
+ }
170
+ }
171
+
172
+ set(key: string, value: T): void {
173
+ this.mem.set(key, value);
174
+ try {
175
+ mkdirSync(this.dir, { recursive: true });
176
+ writeFileSync(
177
+ this.pathFor(key),
178
+ JSON.stringify({ value, expires: Date.now() + this.ttlMs }),
179
+ );
180
+ } catch {
181
+ /* a read-only filesystem must never break the run */
182
+ }
183
+ }
184
+
185
+ async wrap(key: string, fn: () => Promise<T>): Promise<T> {
186
+ const hit = this.get(key);
187
+ if (hit !== undefined) return hit;
188
+ const value = await fn();
189
+ this.set(key, value);
190
+ return value;
191
+ }
192
+ }
193
+
194
+ /** FNV-1a 32-bit hash — stable, dependency-free, good enough for a filename. */
195
+ function hash32(s: string): string {
196
+ let h = 0x811c9dc5;
197
+ for (let i = 0; i < s.length; i++) {
198
+ h ^= s.charCodeAt(i);
199
+ h = Math.imul(h, 0x01000193) >>> 0;
200
+ }
201
+ return h.toString(16).padStart(8, '0');
202
+ }
203
+
204
+ export function usd(n: number | null | undefined): string {
205
+ if (n === null || n === undefined || !Number.isFinite(n)) return 'n/a';
206
+ const abs = Math.abs(n);
207
+ if (abs >= 1_000_000) return `$${(n / 1_000_000).toFixed(2)}M`;
208
+ if (abs >= 1_000) return `$${(n / 1_000).toFixed(2)}K`;
209
+ if (abs >= 1) return `$${n.toFixed(2)}`;
210
+ if (abs === 0) return '$0.00';
211
+ return `$${n.toPrecision(3)}`;
212
+ }
213
+
214
+ export function num(n: number | null | undefined, digits = 6): string {
215
+ if (n === null || n === undefined || !Number.isFinite(n)) return 'n/a';
216
+ if (n === 0) return '0';
217
+ const abs = Math.abs(n);
218
+ if (abs >= 1_000_000) return n.toExponential(3);
219
+ if (abs < 0.000001) return n.toExponential(3);
220
+ if (abs < 1) {
221
+ // Dust is still evidence. "0.001018 tokens" is a measurement; "0" is a
222
+ // rounding error that hides exactly the balance we are reporting on.
223
+ return String(Number(n.toPrecision(Math.min(digits + 2, 8))));
224
+ }
225
+ return n.toFixed(digits).replace(/\.?0+$/, '');
226
+ }
227
+
228
+ export function isoTime(ts: number | undefined | null): string {
229
+ if (!ts) return 'unknown';
230
+ return new Date(ts * 1000).toISOString().replace('.000Z', 'Z');
231
+ }
232
+
233
+ export function short(sig: string, head = 6, tail = 6): string {
234
+ if (!sig) return '';
235
+ if (sig.length <= head + tail + 1) return sig;
236
+ return `${sig.slice(0, head)}…${sig.slice(-tail)}`;
237
+ }
238
+
239
+ /**
240
+ * Format an overstatement ratio.
241
+ *
242
+ * These ratios get astronomically large — the drained "BCH" pool measures
243
+ * 1.93 × 10¹⁵ — and printing "1,932,062,055,886,225×" is both unreadable
244
+ * and less honest than "1932.06T×", because a wall of digits implies a
245
+ * precision the measurement does not have.
246
+ */
247
+ export function ratio(n: number): string {
248
+ if (!Number.isFinite(n)) return '∞';
249
+ const abs = Math.abs(n);
250
+ if (abs >= 1e12) return `${(n / 1e12).toFixed(2)}T×`;
251
+ if (abs >= 1e9) return `${(n / 1e9).toFixed(2)}B×`;
252
+ if (abs >= 1e6) return `${(n / 1e6).toFixed(2)}M×`;
253
+ if (abs >= 1e3) return `${(n / 1e3).toFixed(2)}K×`;
254
+ if (abs >= 100) return `${n.toFixed(0)}×`;
255
+ return `${n.toFixed(2)}×`;
256
+ }
257
+
258
+ // ── tiny ANSI palette (disabled when NO_COLOR is set) ────────────────────────
259
+ //
260
+ // FORCE_COLOR=1 forces the colours on even when stdout is not a TTY (piping
261
+ // to a file, a CI log, a screen recording). It is the de-facto convention
262
+ // used by curl, npm and every other CLI that ships coloured output, and it
263
+ // is what makes `liqproof receipt … > receipt.txt` keep its formatting.
264
+ const useColor =
265
+ !process.env.NO_COLOR && (process.stdout.isTTY || process.env.FORCE_COLOR === '1');
266
+ export const c = {
267
+ bold: (s: string) => (useColor ? `\x1b[1m${s}\x1b[0m` : s),
268
+ dim: (s: string) => (useColor ? `\x1b[2m${s}\x1b[0m` : s),
269
+ red: (s: string) => (useColor ? `\x1b[31m${s}\x1b[0m` : s),
270
+ green: (s: string) => (useColor ? `\x1b[32m${s}\x1b[0m` : s),
271
+ yellow: (s: string) => (useColor ? `\x1b[33m${s}\x1b[0m` : s),
272
+ blue: (s: string) => (useColor ? `\x1b[34m${s}\x1b[0m` : s),
273
+ magenta: (s: string) => (useColor ? `\x1b[35m${s}\x1b[0m` : s),
274
+ cyan: (s: string) => (useColor ? `\x1b[36m${s}\x1b[0m` : s),
275
+ gray: (s: string) => (useColor ? `\x1b[90m${s}\x1b[0m` : s),
276
+ };