/geth-multicall

Go interface for a Multicall contract to batch EVM state reads in a single ethcall

Primary LanguageGoMIT LicenseMIT

Multicall

Wrapper for Multicall which batches calls to contract view functions into a single call and reads all the state in one EVM round-trip.

Initialization

The library requires the Multicall contract to pe deployed on the target chain. We have deployed two variants on Mainnet and Ropsten so far which can be used by using the provided configs.

// Mainnet
eth, _ := ethclient.Client("http://127.0.0.1:8545")
mc, _ := multicall.New(eth, multicall.WithContractAddress(multicall.MainnetAddress))
// Ropsten
mc, _ := multicall.New(eth, multicall.WithContractAddress(multicall.RopstenAddress))

You can also set the gas used for the read transaction:

mc, err := multicall.New(eth, multicall.WithContractAddress(multicall.RopstenAddress), multicall.WithGas(40000))

In this case the contract deployed has to maintain the same function signature as the original one.

Calling

vcs := ViewCalls{
    multicall.NewViewCall(
        "key-1",
        "0x5eb3fa2dfecdde21c950813c665e9364fa609bd2",
        "getLastBlockHash()(bytes32)",
        []interface{}{},
    ),
    multicall.NewViewCall(
        "key-2",
        "0x6b175474e89094c44da98b954eedeac495271d0f",
        "balanceOf(address)(uint256)",
        []interface{}{"0x8134d518e0cef5388136c0de43d7e12278701ac5"},
    ),
}
block := "latest" // default block parameter
res, err := mc.Call(vcs, block)
if err != nil {
    panic(err)
}

lastBlockHashSuccess = res.Calls["key-1"].Success;
lastBlockHash := res.Calls["key-1"].Decoded[0].([32]byte);

someBalanceSuccess := res.Calls["key-2"].Success;
someBalance := res.Calls["key-2"].Decoded[0].(*big.Int);
someBalanceInt := big.Int(*someBalance);

In the example above we batch two calls to two different contracts and get back a map of CallResults which contain the exit value an array of returned values ([]interface{}) which are decoded by the go-ethereum package.