Skip to content

Call.withBlock

Makes every read inside an effect at one block. A program reads a consistent state, without passing the block to each read.

import { Effect } from "effect";
import { Call, Erc4626, type Address } from "@ethect/core";
// any program that reads
const share = (vault: Address.Address, user: Address.Address) =>
Effect.gen(function* () {
const assets = yield* Erc4626.at(vault).totalAssets();
const shares = yield* Erc4626.at(vault).balanceOf(user);
return [assets, shares] as const;
});
const atBlock = (vault: Address.Address, user: Address.Address) =>
share(vault, user).pipe(Call.withBlock(19_000_000n)); // both reads at that block

Without it, two reads in a row may land on two blocks: a block can be mined between them.

The block option of Call.read wins over the block of the program.

import { Effect } from "effect";
import { Call, Erc20, type Address } from "@ethect/core";
const change = (token: Address.Address, user: Address.Address) =>
Effect.all([
Erc20.at(token).balanceOf(user), // at 19_000_000
Call.read(Erc20.at(token).balanceOf(user), { block: 18_000_000n }), // at 18_000_000
]).pipe(Call.withBlock(19_000_000n));

<A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>

A function that takes an effect and gives it back, with its reads at the block. What the effect succeeds with, fails with and requires does not change.

The block: a number, a tag ("latest", "safe", "finalized", "pending", "earliest"), or an object that names the block by its hash.

  • It applies to every read inside, however deep: a read in a function you call, a call of a view run with yield*.
  • The nearest one counts. Inside an effect that has a block, a part given another block is read at its own.
  • A tag is not a fixed block. "latest" names whatever block is the last when each read is sent. For one state across several reads, give a number or a hash.
  • Only reads follow it. The JSON-RPC methods you call yourself take their own block.

Nothing is sent by Call.withBlock itself. It sets a value that Call.read looks up when it builds its request:

  1. your programthe block of the request is the block option, else the block of Call.withBlock, else "latest"