Last active
May 14, 2024 19:31
-
-
Save dalechyn/003757568c354512de2850047732d58e to your computer and use it in GitHub Desktop.
Start an anvil and otterscan with bun
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
import { createAnvil } from "@viem/anvil"; | |
// import fs from "fs"; | |
import "dotenv/config"; | |
import { $ } from "bun"; | |
import path from "path"; | |
import z from "zod"; | |
import { fromError } from "zod-validation-error"; | |
////////////////////// | |
// Env validation | |
const envSchema = z.object({ | |
ANVIL_FORK_URL: z.string().url(), | |
}); | |
export const envParseResult = envSchema.safeParse({ | |
ANVIL_FORK_URL: process.env.ANVIL_FORK_URL, | |
}); | |
if (!envParseResult.success) throw fromError(envParseResult.error).toString(); | |
const env = envParseResult.data; | |
const statePath = path.resolve(__dirname + "/" + ".anvil.state.json"); | |
//////////////////////////////////// | |
// Bootstrap Anvil | |
const anvil = createAnvil({ | |
dumpState: statePath, | |
// Not loading state because of this issue https://github.com/foundry-rs/foundry/issues/7502 | |
// loadState: fs.existsSync(statePath) ? statePath : undefined, | |
forkUrl: env.ANVIL_FORK_URL, | |
// forkBlockNumber: 13846757, | |
// forkChainId: 31337, | |
// test mnemonic | |
mnemonic: | |
"middle social float point stock tank blouse boy clever impose depth session tip deal catch", | |
}); | |
anvil.on("message", (message) => console.log(`Anvil: ${message}`)); | |
await anvil.start(); | |
/////////// | |
// Bootstrap Otterscan | |
const id = | |
await $`docker run --rm -p 5100:80 --name otterscan -d otterscan/otterscan:latest`.quiet(); | |
console.info( | |
`Otterscan listening at "http://localhost:5100", container ${id.text()}`, | |
); | |
///////// | |
// Exit handling | |
// do something when app is closing | |
process.on("exit", async () => { | |
await anvil.stop(); | |
await $`docker stop otterscan`; | |
console.warn("Anvil stopped! – Cleanup"); | |
}); | |
// catches ctrl+c event | |
process.on("SIGINT", async () => { | |
await anvil.stop(); | |
await $`docker stop otterscan`; | |
console.warn("Anvil stopped! – SIGINT"); | |
}); | |
// catches "kill pid" (for example: nodemon restart) | |
process.on("SIGUSR1", async () => { | |
await anvil.stop(); | |
await $`docker stop otterscan`; | |
console.warn("Anvil stopped! – SIGUSR1 (killed pid)"); | |
}); | |
process.on("SIGUSR2", async () => { | |
await anvil.stop(); | |
await $`docker stop otterscan`; | |
console.warn("Anvil stopped! – SIGUSR2 (killed pid)"); | |
}); | |
// catches uncaught exceptions | |
process.on("uncaughtException", async () => { | |
await anvil.stop(); | |
await $`docker stop otterscan`; | |
console.warn("Anvil stopped! – Uncaught exception"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment