Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
144 lines (131 loc) · 4.85 KB
/
Copy pathcli.ts
File metadata and controls
144 lines (131 loc) · 4.85 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
#!/usr/bin/env bun
/**
* substreams-abis CLI
*
* Manages ABI bindings for EVM smart contracts used in Substreams projects.
*/
import "dotenv/config";
import { Command } from "commander";
import { fetchToken } from "./lib/fetch-token";
import { fetchAllTokens } from "./lib/fetch-all-tokens";
import { updateModules } from "./lib/update-modules";
import { filterStandard } from "./lib/filter-standard";
import { findKeccakMatch, computeKeccak } from "./lib/keccak";
import { buildRegistry } from "./lib/build-registry";
const program = new Command();
program
.name("substreams-abis")
.description("CLI tool for managing Substreams ABI bindings")
.version("0.9.0");
// --- fetch-token ---
program
.command("fetch-token")
.description("Fetch ABI and Solidity source for a token contract from a block explorer")
.argument("<token-name>", "Token symbol in CAPS (e.g. SHIB, LINK, UNI)")
.argument("<address>", "The 0x... contract address")
.option("-c, --chain <chain>", "Chain to fetch from", process.env.CHAIN ?? "eth")
.option("-k, --api-key <key>", "Etherscan API key", process.env.ETHERSCAN_API_KEY)
.option("-d, --abi-dir <dir>", "Output directory for ABI files")
.action(async (tokenName: string, address: string, opts) => {
await fetchToken({
tokenName,
address,
chain: opts.chain,
apiKey: opts.apiKey,
abiDir: opts.abiDir,
});
});
// --- fetch-all ---
program
.command("fetch-all")
.description("Batch-fetch ABIs and Solidity source for all tokens in the registry")
.option("-c, --chain <chain>", "Only fetch tokens for this chain", process.env.CHAIN)
.option("-m, --missing", "Only fetch tokens not yet downloaded", false)
.option("--no-duplicates", "Skip tokens that appear on multiple chains")
.option("-r, --registry <path>", "Path to custom registry JSON file")
.option("-k, --api-key <key>", "Etherscan API key", process.env.ETHERSCAN_API_KEY)
.option("-d, --abi-dir <dir>", "Output directory for ABI files")
.action(async (opts) => {
await fetchAllTokens({
chain: opts.chain,
missing: opts.missing,
noDuplicates: !opts.duplicates,
registry: opts.registry,
apiKey: opts.apiKey,
abiDir: opts.abiDir,
});
});
// --- update-modules ---
program
.command("update-modules")
.description("Generate mod.rs for ERC-20 token modules and verify build")
.option("--repo-root <dir>", "Repository root directory")
.action(async (opts) => {
await updateModules({ repoRoot: opts.repoRoot });
});
// --- filter-standard ---
program
.command("filter-standard")
.description("Filter out ERC-20 token ABIs that only contain standard events")
.option("--delete", "Remove standard-only ABI and .sol files")
.option("--move <dir>", "Move standard-only files to the specified directory")
.option("-d, --abi-dir <dir>", "ABI directory to scan")
.action(async (opts) => {
let mode: "dry-run" | "delete" | "move" = "dry-run";
if (opts.delete) mode = "delete";
else if (opts.move) mode = "move";
await filterStandard({
mode,
moveDir: opts.move,
abiDir: opts.abiDir,
});
});
// --- keccak ---
program
.command("keccak")
.description("Compute keccak256 hash of an event signature, or find a matching signature")
.option("-s, --signature <sig>", 'Event signature to hash (e.g. "Transfer(address,address,uint256)")')
.option("-t, --target <hash>", "Target keccak256 hash to find a match for")
.option("-n, --names <names...>", "Event names to try when searching for a match")
.option("--types <types...>", "Parameter types to try (comma-separated sets)")
.action((opts) => {
if (opts.signature) {
const hash = computeKeccak(opts.signature);
console.log(`${opts.signature} => ${hash}`);
return;
}
if (opts.target) {
const names = opts.names ?? [
"TokenCreated",
"TokenCreate",
"NewToken",
"TokenLaunched",
"CreateToken",
"TokenFactoryCreate",
];
const typeSets = opts.types
? [opts.types.map((t: string) => t.split(",")).flat()]
: [["address", "address", "uint256", "uint256", "string", "string"]];
const match = findKeccakMatch({ target: opts.target, names, typeSets });
if (match) {
console.log(`MATCH: ${match}`);
} else {
console.log("No match in tested variants.");
}
return;
}
console.log("Provide --signature to hash, or --target to search. See --help for details.");
});
// --- build-registry ---
program
.command("build-registry")
.description("Build token-registry.json from CoinGecko API")
.option("-o, --output <path>", "Output file path for the registry JSON")
.option("-k, --api-key <key>", "CoinGecko API key", process.env.COINGECKO_API_KEY)
.action(async (opts) => {
await buildRegistry({
output: opts.output,
apiKey: opts.apiKey,
});
});
program.parse();