Skip to content

AnvilNode.placeMulticall3

Puts the code of Multicall3 on an anvil chain, where every chain has it. Reads that are merged are then one plain eth_call, as on a real chain.

import { Effect } from "effect";
import { type Address, Call, Erc20 } from "@ethect/core";
import { AnvilNode } from "@ethect/core/testing";
/** In a test: two reads that leave as one request, which a counter of requests can hold. */
const position = (address: Address.Address, user: Address.Address) =>
Effect.gen(function* () {
yield* AnvilNode.placeMulticall3();
const token = Erc20.at(address);
return yield* Call.all({ symbol: token.symbol(), balance: token.balanceOf(user) });
// one eth_call, to 0xcA11bde05977b3631167028862bE2a173976CA11
});

A fresh anvil has no contract at all. Reads are merged there all the same: the code of Multicall3 is placed for the length of each eth_call, by a state override. That is a first request that Multicall3 does not answer, then one with the override, where a real chain takes one. A test that asserts “one eth_call” puts Multicall3 there first.

It acts on the node of the Transport in context: an anvil started with AnvilNode.layer, or any other.

Effect<void, Transport.Error, Transport>

Nothing. When it ends, the address holds the runtime code of Multicall3.

  • Type: Address.Address
  • Default: 0xcA11bde05977b3631167028862bE2a173976CA11

Where to put the code. Another address is for a program that names its aggregator, with multicall of Call.batching.

  • It deploys nothing. The code is written at the address: no transaction, no block, and no nonce is used.
  • It lasts as long as the chain. A node per test starts without it.
  • It is the code the library places by itself. Multicall3 compiled from its source, the same bytes a state override carries on a chain that has none.
Error When
RpcError the node refused: one that is not anvil has no anvil_setCode
TransportError the node could not be reached

One request to the node of the Transport in context.

  1. your nodeanvil_setCode(at, the runtime code of Multicall3)