-
Notifications
You must be signed in to change notification settings - Fork 43
/
anvil.ts
64 lines (52 loc) · 1.81 KB
/
anvil.ts
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
import { spawnSync } from "child_process";
import { useHandler } from "../../static/scripts/rewards/web3/use-rpc-handler";
import { RPCHandler } from "@ubiquity-dao/rpc-handler";
class Anvil {
rpcs: string[] = [];
rpcHandler: RPCHandler | null = null;
networkIdToFork = 100;
networkIdForAnvilToForkAs = 31337;
async init() {
this.rpcHandler = useHandler(this.networkIdToFork);
console.log(`[RPCHandler] Fetching RPCs...`);
await this.rpcHandler.testRpcPerformance();
const latencies: Record<string, number> = this.rpcHandler.getLatencies();
const sorted = Object.entries(latencies).sort(([, a], [, b]) => a - b);
console.log(
`Fetched ${sorted.length} RPCs.\nFastest: ${sorted[0][0]} (${sorted[0][1]}ms)\nSlowest: ${sorted[sorted.length - 1][0]} (${sorted[sorted.length - 1][1]}ms)`
);
this.rpcs = sorted.map(([rpc]) => rpc.split("__")[1]);
}
async run() {
await this.init();
console.log(`Starting Anvil...`);
const isSuccess = await this.spawner(this.rpcs.shift());
if (!isSuccess) {
throw new Error(`Anvil failed to start`);
}
}
async spawner(rpc?: string): Promise<boolean> {
if (!rpc) {
console.log(`No RPCs left to try`);
return false;
}
console.log(`Forking with RPC: ${rpc}`);
const anvil = spawnSync("anvil", ["--chain-id", `${this.networkIdForAnvilToForkAs}`, "--fork-url", rpc, "--host", "127.0.0.1", "--port", "8545"], {
stdio: "inherit",
});
if (anvil.status !== 0) {
console.log(`Anvil failed to start with RPC: ${rpc}`);
console.log(`Retrying with next RPC...`);
return this.spawner(this.rpcs.shift());
}
return true;
}
}
async function main() {
const anvil = new Anvil();
await anvil.run();
}
main().catch((error) => {
console.error(error);
process.exit(1);
});