# @viem/anvil

> TypeScript wrapper for Foundry [Anvil](https://github.com/foundry-rs/foundry/tree/master/crates/anvil). AnvilJS provides a simple API to create and manage Anvil instances programmatically.

Latest version **0.0.10** (published 2024-04-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @viem/anvil
pnpm add @viem/anvil
yarn add @viem/anvil
bun add @viem/anvil
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2024-04-30 |
| First published | 2023-04-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 210.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 113 |
| Maintainers | awkweb, jmoxey |

## Links

- npm: https://www.npmjs.com/package/@viem/anvil
- Repository: https://github.com/wagmi-dev/anvil.js
- Homepage: https://github.com/wagmi-dev/anvil.js#readme
- Issues: https://github.com/wagmi-dev/anvil.js/issues
- npm.io page: https://npm.io/package/@viem/anvil

## Dependencies (4)

- [ws](https://npm.io/package/ws.md) ^8.13.0
- [execa](https://npm.io/package/execa.md) ^7.1.1
- [get-port](https://npm.io/package/get-port.md) ^6.1.2
- [http-proxy](https://npm.io/package/http-proxy.md) ^1.18.1

## Recent versions

- 0.0.10 (latest) — 2024-04-30
- 0.0.0-main-20240313224506 (main) — 2024-03-13
- 0.0.0-debug-20230502160649 (debug) — 2023-05-02
- 0.0.9 — 2024-03-13
- 0.0.8 — 2024-03-06
- 0.0.7 — 2023-12-14
- 0.0.6 — 2023-05-23
- 0.0.0-main-20230523105206 — 2023-05-23
- 0.0.5 — 2023-05-01
- 0.0.0-main-20230501215104 — 2023-05-01
- 0.0.4 — 2023-04-29
- 0.0.0-main-20230429103639 — 2023-04-29
- 0.0.0-main-20230429102653 — 2023-04-29
- 0.0.3 — 2023-04-26
- 0.0.0-main-20230426160347 — 2023-04-26
- … 13 more at https://npm.io/package/@viem/anvil/versions

## README

# AnvilJS

TypeScript wrapper for Foundry [Anvil](https://github.com/foundry-rs/foundry/tree/master/crates/anvil). AnvilJS provides a simple API to create and manage Anvil instances programmatically.

```ts
import { createAnvil } from "@viem/anvil";

const anvil = createAnvil({
  forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
  forkBlockNumber: 12345678n,
});

await anvil.start();
await anvil.stop();
```

## Install

```bash
pnpm add @viem/anvil
```

```bash
npm i @viem/anvil
```

```bash
yarn add @viem/anvil
```

> **Note**
> Anvil is required to use `@viem/anvil`. Please refer to the [foundry book](https://book.getfoundry.sh) for Anvil [installation instructions](https://book.getfoundry.sh/getting-started/installation).

## API

### `createAnvil`

Creates anvil instance.

| Name         | Description                             | Type                   |
| ------------ | --------------------------------------- | ---------------------- |
| `options`    | Options used to create anvil instance   | `CreateAnvilOptions`   |
| returns      | Anvil instance                          | `Anvil`                |

#### Usage

```ts
import { createAnvil } from "@viem/anvil";

const anvil = createAnvil({
  // All anvil options are supported & typed.
  forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
  forkBlockNumber: 12345678n,
});

await anvil.start();
await anvil.stop();
```

### `getVersion`

Get `anvil` version.

| Name         | Description                             | Type       |
| ------------ | --------------------------------------- | ---------- |
| `command`    | Path to anvil command. Default `anvil`. | `string`   |
| returns      | `anvil version`                         | `string`   |

#### Usage

```ts
import { getVersion } from "@viem/anvil";

const version = await getVersion();
```

### `createProxy`

Creates and starts a proxy server that spawns anvil instance on demand (e.g. per test file or per test case).

| Name      | Description                            | Type                 |
| --------- | -------------------------------------- | -------------------- |
| `options` | Options used to create proxy server.   | `CreateProxyOptions` |
| returns   | Server instance                        | `Server`             |

#### Usage

```ts
import { createProxy, createPool } from "@viem/anvil";

const server = await createProxy({
  pool: createPool(),
  options: {
    forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
    forkBlockNumber: 12345678n,
  },
});

server.listen(8545, "::", () => {
  console.log("Proxy server listening on http://0.0.0.0:8545");
});
```

### `createPool`

Creates pool of anvil instances.

| Name      | Description                       | Type                 |
| --------- | --------------------------------- | -------------------- |
| `options` | Options used to create pool.      | `CreatePoolOptions`  |
| returns   | Pool                              | `Pool`               |

#### Usage

```ts
import { createPool } from "@viem/anvil";

const pool = createPool();
await pool.start(1, {
  forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
  forkBlockNumber: 12345678n,
});
await pool.start(2, {
  forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
  forkBlockNumber: 12345678n,
});
```

### `startProxy`

Creates and starts a proxy server that spawns anvil instance on demand (e.g. per test file or per test case).

| Name      | Description                                                      | Type                      |
| --------- | ---------------------------------------------------------------- | ------------------------- |
| `options` | Options used to spawn anvil instance.                            | `StartProxyOptions`       |
| returns   | Function to shut down the proxy and all spawned anvil instances. | `() => Awaitable<void>`   |

#### Usage

```ts
import { startProxy } from "@viem/anvil";

// Returns a function to shut down the proxy and all spawned anvil instances.
const shutdown = await startProxy({
  port: 8555,
  options: {
    forkUrl: "https://eth-mainnet.alchemyapi.io/v2/<API_KEY>",
    forkBlockNumber: 12345678n,
  },
});

// Shut down the proxy and all spawned anvil instances.
await shutdown();
```

### `fetchLogs`

Fetches logs for anvil instances.

| Name     | Description             | Type                |
| -------- | ----------------------- | ------------------- |
| `url`    | URL to anvil proxy.     | `string`            |
| `id`     | ID of test worker.      | `number`            |
| returns  | Logs of anvil instance. | `string[]`          |

#### Usage

```ts
import { fetchLogs } from "@viem/anvil";

const logs = await fetchLogs("http://localhost:8545", 1);
// Only print the 20 most recent log messages.
console.log(...logs.slice(-20));
```

## Types

```ts
import type {
  Anvil,
  AnvilOptions,
  AnvilProxyOptions,
  AnvilProxyOptionsFn,
  CreateAnvilOptions,
  CreateProxyOptions,
  CreatePoolOptions,
  Pool,
  ProxyRequestContext,
  ProxyRequestHandler,
  ProxyResponse,
  ProxyResponseFailure,
  ProxyResponseSuccess,
  StartProxyOptions,
} from "@viem/anvil";
```

## Contributing

If you're interested in contributing, please read the [contributing docs](/.github/CONTRIBUTING.md) **before submitting a pull request**.

## Authors

- [@fubhy](https://github.com/fubhy) (fubhy.eth, [Twitter](https://twitter.com/thefubhy))

## License

[MIT](/LICENSE) License

---
_Source: https://npm.io/package/@viem/anvil · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
