intrepidshape/elm-web3/1.1.0

NOTE: the latest version is 3.0.0

Web3.Multicall

Batch multiple contract reads into a single eth_call via Multicall3.

The Multicall3 contract (0xcA11bde05977b3631167028862bE2a173976CA11) is deployed on Ethereum, PulseChain, and most EVM networks. It lets you pack N read calls into one RPC round-trip.

import Web3.Multicall as Multicall
import Web3.Abi.Encode as Encode


let
    req =
        Multicall.batch "balance-check"
            [ Multicall.callSpec tokenAddress "balanceOf(address)" [ Encode.address myAddr ]
            , Multicall.callSpec tokenAddress "totalSupply()" []
            ]
in
-- Outgoing port: web3Cmd (Multicall.encode req)
-- Incoming port: match on { tag = "multicallResult" } and apply responseDecoder

A single call specification for inclusion in a multicall batch.

callSpec : Address -> String -> List Value -> CallSpec

Build a CallSpec for a contract read.

Multicall.callSpec
    pairAddress
    "getReserves()"
    []

A batched multicall request with a correlation id.

type alias CallResult =
{ success : Bool
, data : String
}

The result of a single call within a multicall batch.

data is the raw ABI-encoded return value as a hex string (0x-prefixed). Apply your specific decoder (e.g. from Web3.Abi.Decode) to parse it.

batch : String -> List CallSpec -> MulticallRequest

Batch a list of call specs into a multicall request.

The id is echoed back in the multicallResult response so you can match responses to requests when multiple batches are in flight.

Encode a multicall request for the JS port.

Produces:

{ "tag": "multicall"
, "id": "..."
, "calls": [ { "contract": "0x...", "method": "...", "args": [...] }, ... ]
}
responseDecoder : Decoder (List CallResult)

Decoder for the multicallResult port response.

Expects:

{ "tag": "multicallResult"
, "id": "..."
, "results": [ { "success": true, "data": "0x..." }, ... ]
}

Results are in the same order as the original call specs.