Giter VIP home page Giter VIP logo

web3-multicall-go's Introduction

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.

web3-multicall-go's People

Contributors

bowd avatar mmsqe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.