AnvilNode
A local anvil node as a scoped resource. It starts when its layer is built, on a port of its own, and is stopped with its files removed when the scope closes or the process that started it dies.
import { NodeServices } from "@effect/platform-node";import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Transport } from "@ethect/core";import { Eth } from "@ethect/core/rpc";import { AnvilNode } from "@ethect/core/testing";
// A fresh node.const Node = AnvilNode.layer().pipe(Layer.provide(NodeServices.layer));
// An HTTP transport that reaches it, wherever it listens.const Http = Layer.unwrap( Effect.map(AnvilNode.AnvilNode, (node) => Transport.http(node.httpUrl)),).pipe(Layer.provide(FetchHttpClient.layer));
const chainId = await Eth.chainId().pipe( Effect.provide(Layer.provideMerge(Http, Node)), Effect.runPromise,);// 31337, and the node is gone when the promise settlesFunctions
Section titled “Functions”| Function | Returns | Description |
|---|---|---|
AnvilNode.layer(options?) |
Layer<AnvilNode, AnvilError, …> |
A layer that provides a fresh node. |
AnvilNode.make(options?) |
Effect<…, AnvilError, … | Scope> |
Starts a node in the current scope. |
AnvilNode.reorg(depth) |
Effect<void, Transport.Error, Transport> |
Replaces the last blocks of the chain by empty ones. |
AnvilNode.placeMulticall3(at?) |
Effect<void, Transport.Error, Transport> |
Puts Multicall3 where every chain has it. |
A layer that provides a fresh AnvilNode. The node lives as long as the layer.
import { NodeServices } from "@effect/platform-node";import { Layer } from "effect";import { AnvilNode } from "@ethect/core/testing";
const Node = AnvilNode.layer({ args: ["--block-time", "1"] }).pipe( Layer.provide(NodeServices.layer),);- Parameters:
options(AnvilNode.Options, optional). - Returns:
Layer<AnvilNode, AnvilError, ChildProcessSpawner | FileSystem>.
Starts a node in the current scope and returns where it listens. Use it to start several nodes in one program.
import { NodeServices } from "@effect/platform-node";import { Effect } from "effect";import { AnvilNode } from "@ethect/core/testing";
const twoChains = Effect.gen(function* () { // each node has its own port, socket and chain const [first, second] = yield* Effect.all( [AnvilNode.make(), AnvilNode.make({ args: ["--chain-id", "1337"] })], { concurrency: "unbounded" }, ); return [first.httpUrl, second.httpUrl];}).pipe(Effect.scoped, Effect.provide(NodeServices.layer));- Parameters:
options(AnvilNode.Options, optional). - Returns:
Effect<AnvilNode service, AnvilError, ChildProcessSpawner | FileSystem | Scope>. Both nodes stop whenEffect.scopedcloses the scope.
The service
Section titled “The service”AnvilNode.AnvilNode is the service tag. Its value says where the node listens.
| Field | Type | Holds |
|---|---|---|
httpUrl |
string |
the HTTP endpoint, http://127.0.0.1:<port> |
wsUrl |
string |
the WebSocket endpoint, on the same port |
ipcPath |
string |
the path of the IPC socket |
One layer per transport, each reaching the node in context:
import { NodeSocket } from "@effect/platform-node";import { Effect, Layer } from "effect";import { Socket } from "effect/socket";import { Transport } from "@ethect/core";import { AnvilNode } from "@ethect/core/testing";
const WebSocket = Layer.unwrap( Effect.map(AnvilNode.AnvilNode, (node) => Transport.webSocket(node.wsUrl)),).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal));
const Ipc = Layer.unwrap( Effect.map(AnvilNode.AnvilNode, (node) => Transport.ipc().pipe(Layer.provide(NodeSocket.layerNet({ path: node.ipcPath }))), ),);Options
Section titled “Options”AnvilNode.Options, every field optional.
| Field | Type | Default | Description |
|---|---|---|---|
binary |
string |
"anvil" |
The anvil binary, looked up on the PATH. |
args |
ReadonlyArray<string> |
[] |
Extra command line arguments, such as ["--block-time", "1"] or a fork URL. |
startTimeout |
a Duration input |
"10 seconds" |
How long anvil may take to say it listens. |
import { NodeServices } from "@effect/platform-node";import { Layer } from "effect";import { AnvilNode } from "@ethect/core/testing";
const Fork = AnvilNode.layer({ args: ["--fork-url", "https://rpc.example.com", "--fork-block-number", "19000000"], startTimeout: "30 seconds",}).pipe(Layer.provide(NodeServices.layer));--port and --ipc are set by AnvilNode: do not pass them.
AnvilError
Section titled “AnvilError”- Type: a tagged error,
_tag: "AnvilError"
Anvil could not be started.
| Field | Type | Holds |
|---|---|---|
message |
string |
what went wrong |
output |
string |
what anvil printed before giving up |
cause |
unknown (optional) |
the underlying failure |
| Situation | message |
|---|---|
the binary is not on the PATH |
says so, with the command that installs Foundry |
| anvil exits before listening, for example on an unknown flag | anvil exited with code …, and output holds its error |
anvil does not print its port within startTimeout |
says it did not listen in time |
import { NodeServices } from "@effect/platform-node";import { Effect } from "effect";import { AnvilNode } from "@ethect/core/testing";
const started = AnvilNode.make({ args: ["--no-such-flag"] }).pipe( Effect.catchTag("AnvilError", (error) => Effect.succeed(`anvil said: ${error.output}`), ), Effect.scoped, Effect.provide(NodeServices.layer),);Behavior
Section titled “Behavior”- A port the OS assigns. anvil is started with
--port 0, and the port is read from what it prints. Nodes never collide, so tests can run concurrently. - Its own directory. Each node gets a directory under
/tmpthat holds its IPC socket. The directory is removed with the node. - Stopped with its scope. Closing the scope stops anvil. With
AnvilNode.layer, that is when the layer is released. - Stopped with its owner. anvil runs under a small shell supervisor that stops it and
removes its directory when the process that started it dies, however it dies: a
SIGKILLor a Ctrl-C of the test runner leaves no node behind. - Ready when the effect returns.
makewaits until anvil says it listens. - Any host. The URLs name the host anvil says it listens on. For a node told to listen on
every address, with
args: ["--host", "0.0.0.0"], they name the loopback,127.0.0.1. - A default anvil chain. Chain id
31337, ten funded development accounts, a block mined per transaction, unlessargssay otherwise.