Skip to content

Call.Reader

The service that runs reads. Call.read builds a request and asks it. Call.direct is the reader that sends one request per read.

Replace the reader to change how reads reach the node. This one counts them.

import { Effect } from "effect";
import { Call, Erc20, type Address } from "@ethect/core";
const count = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
Effect.gen(function* () {
let reads = 0;
const counting: Call.Reader = {
call: (request) => {
reads++;
return Call.direct.call(request);
},
};
const result = yield* Effect.provideService(effect, Call.Reader, counting);
return [result, reads] as const;
});
const twice = (token: Address.Address, user: Address.Address) => {
const balance = Erc20.at(token).balanceOf(user);
return count(Effect.all([balance, balance])); // [[…, …], 2]
};

Call.Reader is a Context.Reference: it has a default, so a program that provides none still reads. The default merges reads into multicalls: Call.batching tunes it, or turns it off.

A reader that answers from memory tests a program without a node.

import { Effect } from "effect";
import { Call, Erc20, Transport, type Address, type Hex } from "@ethect/core";
// what the contract would return: one word, 42
const fortyTwo: Hex.Hex = `0x${"2a".padStart(64, "0")}`;
const fake: Call.Reader = { call: () => Effect.succeed(fortyTwo) };
const balance = (token: Address.Address, user: Address.Address) =>
Erc20.at(token)
.balanceOf(user)
.pipe(
Effect.provideService(Call.Reader, fake),
// a read is typed as needing a transport: this one is never asked
Effect.provideService(
Transport.Transport,
Transport.Transport.of({ request: () => Effect.die("no node in this test") }),
),
); // 42n
import type { Effect, Result } from "effect";
import type { Call, Hex, Revert, Transport } from "@ethect/core";
interface Reader {
readonly call: (
request: Call.Request,
) => Effect.Effect<Hex.Hex, Revert.Unknown | Transport.Error, Transport.Transport>;
readonly callMany?: (
requests: ReadonlyArray<Call.Request>,
) => Effect.Effect<
ReadonlyArray<Result.Result<Hex.Hex, Revert.Unknown>>,
Transport.Error,
Transport.Transport
>;
}
Member Gives
call(request) the bytes the call returned. A revert is a failure: a Revert.Unknown with the revert data, which Call.read then decodes
callMany(requests) (optional) the same for several requests at once, each with its own outcome. Call.all uses it to leave without waiting. Without it, Call.all asks call for each, concurrently

Call.Request is one read as a reader gets it: the call is encoded, and its block is decided.

Field Type Is
to Address.Address | undefined the address called
data Hex.Hex the calldata
block BlockId.BlockId the block to read at: "latest" when nothing was said
from, gas optional the sender and the gas limit, when the read set them
stateOverride, blockOverride optional the overrides, when the read set them
batch boolean whether the read may share a request with others. false with from, gas or an override, and when the read said batch: false
state optional set for a call that reads the state of the chain itself, such as its block number: a reader may answer it with a plain method

Call.direct is the reader that does not batch: one request per read. Call.batching(false) puts it in place for a whole program.

import { Effect } from "effect";
import { Call } from "@ethect/core";
const unbatched = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
Effect.provideService(effect, Call.Reader, Call.direct);
  • A reader sees no ABI. It gets bytes and gives bytes. Encoding the call and decoding its answer and its revert are done by Call.read, around it.
  • A revert is a failure of call, never a success with odd bytes. Call.direct turns the node’s “execution reverted” into a Revert.Unknown with the data the node gave, or with 0x when it gave none.
  • Any other error of the node goes through as it is: an RpcError or a TransportError.
  • A reader uses the Transport of whoever reads. It keeps none of its own, so the same reader serves every chain of a program.

Call.direct asks the Transport, once per request.

  1. a request

    1. your nodeeth_call({ to, data, from?, gas? }, block, stateOverride?, blockOverride?)
    2. the node answers a revertfails with Revert.Unknown { selector, data }

  2. a request with state: the plain method that gives it

    1. blockNumbereth_blockNumbernothing when the read is at a block number
    2. chainIdeth_chainIdasked once per transport
    3. balanceeth_getBalance(account, block)
    4. timestamp, gasLimit, baseFeeeth_getBlockByNumber(block, false)or eth_getBlockByHash
    5. blockHasheth_getBlockByNumber(thatBlock, false)
    6. any, with an overridethe eth_call above, to its getter: only a contract sees one

The answer to a request with state is given as one word, as a contract would return it, so that it decodes like any other read.