Call.all
Reads several calls together, in one request and at one block. A tuple of calls gives a tuple, a record a record, an array an array.
import { Effect } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Call, Erc20, Transport } from "@ethect/core";
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");const USER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () { // one eth_call: both are read at the same block return yield* Call.all({ symbol: Erc20.at(USDC).symbol(), balance: Erc20.at(USDC).balanceOf(USER), });});
const token = await program.pipe( Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")), Effect.provide(FetchHttpClient.layer), Effect.runPromise,); // { symbol: "USDC", balance: 0n }The examples below leave the transport out.
A tuple
Section titled “A tuple”A tuple of calls gives the tuple of what they return, each with its own type.
import { Effect } from "effect";import { Call, Erc20, type Address } from "@ethect/core";
const read = (token: Address.Address, user: Address.Address) => Call.all([Erc20.at(token).symbol(), Erc20.at(token).balanceOf(user)]);// Effect<readonly [string, bigint], …>
const shown = (token: Address.Address, user: Address.Address) => Effect.map(read(token, user), ([name, balance]) => `${balance} ${name}`);An array
Section titled “An array”Any number of calls built at run time: a portfolio, the positions of a list of markets.
import { Call, Erc20, type Address } from "@ethect/core";
const balances = (tokens: ReadonlyArray<Address.Address>, user: Address.Address) => Call.all(tokens.map((token) => Erc20.at(token).balanceOf(user)));// Effect<ReadonlyArray<bigint>, …>Each call with its own outcome
Section titled “Each call with its own outcome”By default the first call that fails fails the whole. With mode: "result" each call has a
Result of its own, and a call that reverts fails nothing.
import { Effect, Result } from "effect";import { Call, Erc721, type Address } from "@ethect/core";
const owners = (nft: Address.Address, ids: ReadonlyArray<bigint>) => Call.all( ids.map((id) => Erc721.at(nft).ownerOf(id)), { mode: "result" }, ).pipe( // a token that does not exist has no owner: its call reverted, the others did not Effect.map((outcomes) => outcomes.map((outcome) => Result.getOrUndefined(outcome))), );With the state of the chain
Section titled “With the state of the chain”The state of the chain is a call like any other. Read here, the
block number comes from the eth_call that reads the balances: it is the number of their
block, and there is no request before it.
import { Call, Erc20, type Address } from "@ethect/core";
const position = (token: Address.Address, user: Address.Address) => Call.all({ block: Call.blockNumber, ether: Call.balance(user), tokens: Erc20.at(token).balanceOf(user), });At a block
Section titled “At a block”The options are those of Call.read, applied to every call.
import { Call, Erc20, type Address } from "@ethect/core";
const settled = (token: Address.Address, user: Address.Address) => Call.all([Erc20.at(token).symbol(), Erc20.at(token).balanceOf(user)], { block: "finalized" });Returns
Section titled “Returns”Effect<Results, Errors | Call.Error, Transport>
calls |
Gives |
|---|---|
a tuple [a, b] |
readonly [A, B] |
a record { a, b } |
{ readonly a: A; readonly b: B } |
| an array | ReadonlyArray<A> |
It fails with the errors every call declares, and with Call.Error.
With mode: "result": Effect<Results, Transport.Error, Transport>, where each value is a
Result<A, E | Revert.Revert | AbiError>: what the call returned, or why it alone failed.
Parameters
Section titled “Parameters”- Type: a tuple, an array or a record of
Call.View<A, E>
Calls of view or pure functions, as fn.at(address)(...args) builds
them, and the state of the chain. A call of a function that writes
is not read: it does not compile.
options.mode (optional)
Section titled “options.mode (optional)”- Type:
"result"
Gives each call its own outcome. Only a failure of the node fails the whole. It takes a tuple or an array of calls.
options.block (optional)
Section titled “options.block (optional)”- Type:
BlockId.BlockId - Default: the block of
Call.withBlock, else"latest"
The block every call is read at.
options.from, options.gas, options.stateOverride, options.blockOverride (optional)
Section titled “options.from, options.gas, options.stateOverride, options.blockOverride (optional)”As for Call.read. Each of them makes every
call a request of its own: a multicall is one call, from one sender, over one state.
import { Call, Erc20, type Address } from "@ethect/core";
// two eth_calls, each from `user`const asUser = (token: Address.Address, user: Address.Address) => Call.all([Erc20.at(token).symbol(), Erc20.at(token).balanceOf(user)], { from: user });options.batch (optional)
Section titled “options.batch (optional)”- Type:
boolean - Default:
true
false sends every call as a request of its own.
Behavior
Section titled “Behavior”- It does not wait. Reads issued separately wait a few milliseconds for each other.
Call.allis complete when it is called, so its request leaves at once. - One block. Every call is answered from the same block, also when the calls take several requests: see the limits.
- The same call twice is asked once. Both places get the answer.
- The first failure fails the whole, in the order of the calls. A revert is decoded with the errors of the call that reverted.
- In
mode: "result", a call that cannot be encoded fails alone too, with anAbiError, and the others are read. - A failure of the node is not an outcome of a call. It fails the whole in both modes.
- Nothing is asked for no call.
Call.all([])gives[]. - A reader that does not batch gets the calls concurrently. With
Call.batching(false), or a reader of your own that has nocallMany, there is one request per call, and no common block unless you give one.
Errors
Section titled “Errors”| Error | When |
|---|---|
| an error a call declares | that call reverted with it |
Revert.Revert |
a call reverted with something else |
AbiError |
an argument of a call does not encode, or an answer does not decode |
RpcError |
the node answered with an error that is not a revert |
TransportError |
the node could not be reached, or did not answer |
In mode: "result" the first three are in the Result of their call, and the effect fails
with the last two only.
Under the hood
Section titled “Under the hood”One service is asked: Call.Reader, which asks the Transport.
With the reader of this package, on a chain that has Multicall3:
Call.all([a, b, c])- encodeeach call: selector + arguments. No request
- Call.Reader
callMany([a, b, c])all of them, at once - your node
eth_call({ to: Multicall3, data: aggregate3([a, b, c]) }, block) - decodeeach answer as what its function returns, each revert with the errors of its function. No request
| Calls | Requests |
|---|---|
| two or more, same options | one eth_call of aggregate3 |
| one | the eth_call of that call |
with from, gas, an override or batch: false |
one eth_call per call |
| with the state of the chain among them | the same eth_call: each value is one more call of its aggregate3 |
with Call.baseFee, on a node that cannot be paid inside a call |
eth_getBlockByNumber first, then the eth_call at the hash of that block |
with Call.blockHash of a block the chain no longer holds |
the eth_call, then eth_getBlockByNumber for that hash |
| more than the limits | eth_blockNumber first, then one eth_call per piece at that number |
Call.batching has the requests of a chain
without Multicall3, and of a node that refuses state overrides.