Skip to content

Calls and reads (Call)

A call is a message to a contract, as a plain value: building one does nothing. A call of a view function is also the effect of reading it, and any call can be sent as a transaction with Tx.

import { Effect } from "effect";
import { Address, Call, Erc20 } from "@ethect/core";
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () {
// a call: the function, the address, the arguments. Nothing is encoded or sent
const call = Erc20.at(USDC).balanceOf(USER);
// a call of a view is the effect of reading it
const balance = yield* call;
// the same, with options
const before = yield* Call.read(call, { block: 19_000_000n });
return balance - before;
});
Export Kind Description
Call.read(call, options?) function Reads a call: what its function returns, or the error it reverts with.
Call.all(calls, options?) function Reads several calls together, at one block: a tuple or a record of them.
Call.make(call) function A raw call: no ABI, the bytes as they are. Without to, a deployment.
Call.encode(call) function The call as a transaction carries it. The only place a call is encoded.
Call.key(call) function A stable name for a call: a cache key. It cannot fail.
Call.withBlock(block) function Makes every read inside an effect at one block.
Call.batching(options) function A layer that tunes how reads are merged into multicalls, or turns it off with false.
Call.blockNumber, Call.timestamp, Call.chainId, Call.baseFee, Call.gasLimit call The state of the chain, as calls to read beside others.
Call.balance(account), Call.blockHash(block) function The same, for an account and for a block.
Call.Reader reference What runs eth_calls. By default, it merges reads into multicalls.
Call.direct reader One eth_call per read.
Call.TypeId constant The key under which a call holds what reading it gives and fails with.
Call.Call<A, E> type A call: fn, to, args, value, and data for a raw call.
Call.View<A, E> type A call of a view or pure function: also an Effect.
Call.Encoded type A call as a transaction carries it: to, data, value.
Call.ReadOptions type How a read is made: block, from, gas, stateOverride, blockOverride, batch.
Call.Error type What any read can fail with, beyond the errors its function declares.
Call.Request type One read as a reader gets it: encoded, with its block decided.
Call.Batching type The options of Call.batching: window, maxCalls, maxBytes, multicall.

A call is built by fn.at(address)(...args), or by Call.make for raw bytes. It is plain data.

Field Type Is
fn Fn.Any | undefined the function called. Absent for a raw call
to Address.Address | undefined the address called. Absent for a deployment
args ReadonlyArray<unknown> the arguments, as they were given. Not encoded
value bigint the ether the call carries, in wei
data Hex.Hex, optional the bytes of a raw call
Built from The call is You can
a view or a pure function a Call.View<A, E>: also an Effect read it, by running it or with Call.read
a nonpayable or a payable function a Call.Call<A, E> send it with Tx
Call.make a Call.Call<Hex, never> send it with Tx

A is what the function returns, and E the errors it declares. Any call can be encoded and named.

Failure When
an error the function declares the call reverted with it
Revert.Revert the call reverted with something else
AbiError the arguments could not be encoded, or the answer could not be decoded
Transport.Error the node answered with an error, or did not answer

Only the node is asked. Each function has its requests on its page.

  1. Call.read(call), and a view call that is yielded

    1. your programthe selector and the arguments are encoded. No request
    2. your nodeeth_call({ to, data }, block)
    3. your programwhat the function returns is decoded, or its revert with the errors it declares. No request
  2. Call.all([a, b, c]), and reads issued together

    1. your nodeeth_call({ to: Multicall3, data: aggregate3([a, b, c]) }, block)all of them, in one request

See Call.read, Call.all and Call.batching.