Skip to content

jyozq/web3-multicall-go

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This fork removed the type BigIntJSONString in favor of the type big.Int.

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].(*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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%