gleeth/rpc/batch

Batch JSON-RPC requests into a single HTTP call.

Reduces HTTP overhead when making multiple independent queries by sending them as a JSON array per the JSON-RPC batch specification.

Examples

let assert Ok(results) =
  batch.new()
  |> batch.add("eth_blockNumber", [])
  |> batch.add("eth_gasPrice", [])
  |> batch.execute_strings(provider)

// results: [Ok("0x1234"), Ok("0x3b9aca00")]

Types

A batch of JSON-RPC requests to be sent together.

pub type Batch {
  Batch(requests: List(BatchRequest))
}

Constructors

A single request within a batch, with an auto-assigned ID.

pub opaque type BatchRequest

Values

pub fn add(
  batch: Batch,
  method: String,
  params: List(json.Json),
) -> Batch

Add a raw method name and params to the batch.

pub fn add_method(
  batch: Batch,
  method: types.EthMethod,
  params: List(json.Json),
) -> Batch

Add a request using an EthMethod value.

pub fn execute_raw(
  batch: Batch,
  provider: provider.Provider,
) -> Result(
  List(Result(String, types.GleethError)),
  types.GleethError,
)

Execute the batch and return raw JSON response bodies per request. Each string is the full {"jsonrpc":"2.0","id":N,"result":...} for that request, suitable for passing to response_utils.decode_rpc_response with a custom decoder.

pub fn execute_strings(
  batch: Batch,
  provider: provider.Provider,
) -> Result(
  List(Result(String, types.GleethError)),
  types.GleethError,
)

Execute the batch and decode each response as a string. Returns one Result(String, GleethError) per request, in order. This is the most common use case - batching simple hex-returning RPC calls.

pub fn new() -> Batch

Create an empty batch.

Search Document