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 readsconst 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 blockWithout it, two reads in a row may land on two blocks: a block can be mined between them.
The block of one read
Section titled “The block of one read”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));Returns
Section titled “Returns”<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.
Parameters
Section titled “Parameters”- Type:
BlockId.BlockId
The block: a number, a tag ("latest", "safe", "finalized", "pending", "earliest"), or
an object that names the block by its hash.
Behavior
Section titled “Behavior”- 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.
Under the hood
Section titled “Under the hood”Nothing is sent by Call.withBlock itself. It sets a value that Call.read looks up when it
builds its request:
- your programthe block of the request is the
blockoption, else the block ofCall.withBlock, else"latest"