gleeth/multicall

Multicall3 batching for efficient contract reads.

Batches multiple contract read calls into a single eth_call using the canonical Multicall3 contract (deployed at the same address on all major chains). This reduces RPC round trips when reading state from multiple contracts.

Examples

// Read 3 balances in a single RPC call
let assert Ok(results) =
  multicall.new()
  |> multicall.add(usdc_address, balance_of_calldata_1)
  |> multicall.add(usdc_address, balance_of_calldata_2)
  |> multicall.add(dai_address, balance_of_calldata_3)
  |> multicall.execute(provider)

Types

A single call within a multicall batch.

pub type Call {
  Call(target: String, calldata: String, allow_failure: Bool)
}

Constructors

  • Call(target: String, calldata: String, allow_failure: Bool)

Result of a single call within a multicall batch.

pub type CallResult {
  CallSuccess(data: String)
  CallFailure(data: String)
}

Constructors

  • CallSuccess(data: String)
  • CallFailure(data: String)

A batch of calls to execute through Multicall3.

pub type Multicall {
  Multicall(calls: List(Call))
}

Constructors

  • Multicall(calls: List(Call))

Values

pub fn add(
  batch: Multicall,
  target: String,
  calldata: String,
) -> Multicall

Add a call that must succeed (reverts the whole batch on failure).

Examples

multicall.new()
|> multicall.add("0xA0b8...", "0x70a08231...")
pub fn execute(
  batch: Multicall,
  provider: provider.Provider,
) -> Result(List(CallResult), types.GleethError)

Execute the multicall batch using aggregate3. Returns one CallResult per call in the order they were added.

Examples

let assert Ok(results) =
  multicall.new()
  |> multicall.add(usdc, calldata1)
  |> multicall.add(usdc, calldata2)
  |> multicall.execute(provider)
pub fn execute_at(
  batch: Multicall,
  provider: provider.Provider,
  address: String,
) -> Result(List(CallResult), types.GleethError)

Execute using a custom Multicall3 address (for chains where it’s deployed at a non-standard address).

pub const multicall3_address: String

Canonical Multicall3 address, deployed on all major EVM chains.

pub fn new() -> Multicall

Create an empty multicall batch.

pub fn try_add(
  batch: Multicall,
  target: String,
  calldata: String,
) -> Multicall

Add a call that is allowed to fail without reverting the batch.

Examples

multicall.new()
|> multicall.try_add("0xA0b8...", "0x70a08231...")
Search Document