Repository navigation
Expand file tree
/
Copy pathawesome-scan.mjs
More file actions
241 lines (201 loc) · 10.7 KB
/
Copy pathawesome-scan.mjs
File metadata and controls
241 lines (201 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Scans an awesome-list README for hook repos and reports which ones actually target the
// current v4 API. Read-only by design: it prints candidates and never touches hooks/*.yml,
// because compiling against current v4-core is a floor, not a reason to list something.
//
// node scripts/awesome-scan.mjs scan fewwwww/awesome-uniswap-hooks
// node scripts/awesome-scan.mjs --all include rejected repos and the reasons
// node scripts/awesome-scan.mjs --json machine-readable
// node scripts/awesome-scan.mjs --refresh bypass the response cache
// node scripts/awesome-scan.mjs owner/repo scan a different list
//
// Two mistakes made this worth committing rather than re-improvising each time; both are
// guarded below. See linksIn() for the extraction bug and stripComments() for the grep one.
import fs from "node:fs";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { parse } from "yaml";
const root = process.cwd();
const argv = process.argv.slice(2);
const opts = new Set(argv.filter((a) => a.startsWith("--")));
const LIST = argv.find((a) => !a.startsWith("--")) ?? "fewwwww/awesome-uniswap-hooks";
const asJson = opts.has("--json");
const showAll = opts.has("--all");
const log = (...m) => { if (!asJson) console.log(...m); };
// ---------------------------------------------------------------------------
// Link extraction
// ---------------------------------------------------------------------------
// THE FIX. The throwaway version of this scan anchored on `^- \[name\]\((url)\)` and took
// the first capture, so it silently dropped every repo written as a trailing parenthetical:
//
// - [SuckerPunch](https://ethglobal.com/showcase/...) ([source](https://github.com/popfendi/suckerpunch)): ...
//
// 23 repos vanished that way and were miscounted as "showcase page, no source available".
// Match every link on the line and let repoOf() decide which one is the code.
const LINK = /\[[^\]]*\]\(\s*([^)\s]+)/g;
const linksIn = (line) => [...line.matchAll(LINK)].map((m) => m[1]);
// github.com/orgs/x and friends look like owner/repo but aren't.
const NOT_OWNERS = new Set(["orgs", "topics", "search", "marketplace", "sponsors", "collections", "apps"]);
function repoOf(url) {
const m = /^https?:\/\/(?:www\.)?github\.com\/([^/#?]+)\/([^/#?]+)/.exec(url);
if (!m) return null;
const [, owner, name] = m;
if (NOT_OWNERS.has(owner.toLowerCase())) return null;
return `${owner}/${name.replace(/\.git$/, "")}`;
}
// Runs on every invocation. This exact line used to yield nothing; if it ever does again
// the scan fails loudly instead of quietly under-reporting.
const CANARY = "- [SuckerPunch](https://ethglobal.com/showcase/x) ([source](https://github.com/popfendi/suckerpunch)): dynamic fee hook.";
if (linksIn(CANARY).map(repoOf).filter(Boolean).join(",") !== "popfendi/suckerpunch") {
throw new Error("link extraction regressed: a trailing ([source](...)) repo was dropped");
}
// ---------------------------------------------------------------------------
// Source audit
// ---------------------------------------------------------------------------
const schema = JSON.parse(fs.readFileSync(path.join(root, "schema/hook.schema.json"), "utf8"));
const FLAG_ORDER = schema.properties.flags.items.enum;
// Strip comments before grepping for any tell. PureFi's VerifierHook carries a NatSpec line
// weighing tx.origin as an option and never calls it — grepping the raw file reports that as
// a live use and rejects a listing that is fine.
const stripComments = (src) => src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/[^\n]*/g, "");
// External callbacks must be gated to the PoolManager. Matches onlyPoolManager,
// onlyByPoolManager, poolManagerOnly and selfOnly.
const CALLBACK = /function\s+(?:before|after)(?:Initialize|AddLiquidity|RemoveLiquidity|Swap|Donate)\s*\([^)]*\)([^{;]*?)\breturns\b/g;
// v4 renamed the permission accessor on the way to launch: getHooksCalls()/Hooks.Calls
// through 2023, then getHookPermissions()/Hooks.Permissions. Match both — a repo that only
// has the 2023 spelling is still a hook, and reporting it as "not a hook" buries it.
const PERMS = /(getHookPermissions|getHooksCalls)[\s\S]*?\}\);/;
function auditHook(src) {
const block = PERMS.exec(src);
if (!block) return null; // genuinely not a hook
const lines = block[0].split("\n").map((l) => l.trim());
const declared = FLAG_ORDER.filter((f) => lines.some((l) => l.startsWith(`${f}:`)));
const on = FLAG_ORDER.filter((f) => lines.some((l) => l.startsWith(`${f}:`) && l.includes("true")));
const code = stripComments(src);
const rejects = [];
// The decisive test, and the one the old scan lacked. Merely having a getHookPermissions
// proves nothing — every 2024 hackathon hook has one. Hooks.Permissions carried 10 fields
// before v4 shipped; the four *ReturnDelta flags landed with the launch. A 10-field struct
// cannot compile against current v4-core, whatever else the contract does.
if (block[1] === "getHooksCalls") {
rejects.push("getHooksCalls/Hooks.Calls — 2023 API, renamed to getHookPermissions");
} else if (declared.length !== FLAG_ORDER.length) {
rejects.push(`${declared.length}/${FLAG_ORDER.length} permission fields — pre-launch struct`);
}
if (/\bupdateDynamicSwapFee\b/.test(code)) {
rejects.push("updateDynamicSwapFee — removed from v4, superseded by OVERRIDE_FEE_FLAG");
}
if (/["']v4-periphery\/BaseHook\.sol["']/.test(code)) {
rejects.push("old BaseHook import path — now v4-periphery/src/utils/BaseHook.sol");
}
if (/\btx\.origin\b/.test(code)) rejects.push("tx.origin as caller identity");
const ungated = [...code.matchAll(CALLBACK)]
.some(([, mods]) => /\b(?:external|public)\b/.test(mods) && !/only/i.test(mods));
if (ungated) rejects.push("external callback with no PoolManager gate");
return { fields: declared.length, flags: on, rejects };
}
// ---------------------------------------------------------------------------
// GitHub, cached
// ---------------------------------------------------------------------------
const cacheDir = path.join(root, "node_modules/.cache/awesome-scan");
fs.mkdirSync(cacheDir, { recursive: true });
const MISS = "