web3-multicall-go

module
v0.0.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2021 License: MIT

README

CircleCI

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.

Usage

The library is used in conjunction with web3-go, and the first parameter to multicall.New is an ethrpc.ETHInterface as defined in the package.

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
mc, err := multicall.New(eth, multicall.ContractAddress(multicall.MainnetAddress))
// Ropsten
mc, err := multicall.New(eth, multicall.ContractAddress(multicall.RopstenAddress))

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

mc, err := multicall.New(eth, multicall.ContractAddress(multicall.RopstenAddress), multicall.SetGas(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].(*multicall.BigIntJSONString);
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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL