batch
Runs several reads together, in as few round trips as possible. Parameters, return value, errors and examples for batch in @ensforge/sdk.
Runs several reads together, in as few round trips as possible.
When to use
Use to load a page's data at once: profiles, names, prices, permissions.
Details
requestsis a list (results come back as a tuple) or a record (results keep its keys).- Contract reads coalesce into Multicall3 calls, per block; resolution reads (records, primary names) keep their Universal Resolver call, so CCIP-Read and off-chain names work.
- With
allowFailure(default) each result is{ status: "success", result }or{ status: "failure", error }; withallowFailure: falsethe first failure rejects. - Only reads with
.requestcan be batched; writes combine withPlan.merge.
Signature
function batch<const requests extends Requests, allowFailure extends boolean = true>(
client: Like,
options: BatchOptions<requests, allowFailure>,
): Promise<batch.ReturnType<requests, allowFailure>>Call it as ens.batch(options) on a client, or Actions.batch(client, options).
Parameters
| Option | Type | Default | Description |
|---|---|---|---|
requests (required) | Requests | Deferred reads from action.request(options), as a list or a record. | |
allowFailure | boolean | true | Report each request's failure in its result instead of failing the batch. |
signal | AbortSignal | Cancels the batch; the Promise rejects with signal.reason. |
Returns
Resolves to { readonly results: Results<requests, allowFailure>; }. Results in the requests' shape.
| Field | Type | Description |
|---|---|---|
results | Results<Requests, boolean> |
Errors
| Error | Codes | When |
|---|---|---|
InvalidInputError | INVALID_RECORD_VALUE | a request that cannot be batched. |
The first request's error with allowFailure: false. |
It can also fail with the errors every read can (Errors.CommonError: ConfigError, ContractRevertError, InternalError, NetworkError).
See How errors work.
Examples
import { Actions } from "@ensforge/sdk"
const { results } = await ens.batch({
requests: {
address: Actions.record.getAddress.request({ name: "nick.eth" }),
avatar: Actions.record.getText.request({ name: "nick.eth", key: "avatar" }),
},
})import { Actions } from "@ensforge/sdk"
const { results: [state, primary] } = await Actions.batch(publicClient, {
requests: [Actions.name.get.request({ name: "nick.eth" }), Actions.primaryName.get.request({ address })],
allowFailure: false,
})See also
name.getand every other read's.request
Was this page helpful?