generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
403 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,4 +87,4 @@ | |
] | ||
}, | ||
"packageManager": "yarn@4.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { spawnSync } from "child_process"; | ||
import { RPCHandler } from "../dist/"; | ||
|
||
class Anvil { | ||
rpcs: string[] = []; | ||
rpcHandler: RPCHandler | null = null; | ||
|
||
async init() { | ||
this.rpcHandler = new RPCHandler({ | ||
autoStorage: false, | ||
cacheRefreshCycles: 3, | ||
networkId: 100, | ||
networkName: "test", | ||
rpcTimeout: 1000, | ||
runtimeRpcs: null, | ||
networkRpcs: null, | ||
proxySettings: { | ||
logger: null, | ||
logTier: "ok", | ||
retryCount: 3, | ||
retryDelay: 100, | ||
strictLogs: true, | ||
moduleName: "TestModule", | ||
}, | ||
}); | ||
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", "31337", "--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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.