Giter Club home page Giter Club logo

multicall-go's Introduction


DePocket Multicall

Multicall used on DePocket platforms to optimize onchain index experience
View Integrated dApps · Report Bug · Request Feature


Usage

Download and install it:

go get github.com/depocket/multicall-go

Canonical example:

package main

import (
	"fmt"
	"github.com/depocket/multicall-go/call"
	"log"
	"math/big"
)

var ContractExamples = map[string]call.Chain{
	"0xB8c77482e45F1F44dE1745F52C74426C631bDD52": call.Ethereum, // map[BNB:chain]
	"0x0000000000000000000000000000000000000000": call.Ethereum, // map[null_address:chain]
}

var Contracts = []string{
	"0xB8c77482e45F1F44dE1745F52C74426C631bDD52", // BNB on Ethereum
	"0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT on Ethereum
	"0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95", // UniswapFactory on Ethereum
}

func main() {
	ExampleMultiCallV1()
	ExampleMultiCallV2()
	ExampleMultiCallWithBatch()
}

func ExampleMultiCallV1() {
	for address, chain := range ContractExamples {
		caller := call.NewContractBuilder().
			WithChainConfig(call.DefaultChainConfigs[chain]).
			AddMethod("totalSupply()(uint256)")
		_, result, err := caller.AddCall("ts", address, "totalSupply").Call(nil)
		if err != nil {
			fmt.Printf("Error to call %s contract on %s\n", address, chain)
		} else {
			fmt.Printf("Call %s success with total supply is %d in decimals\n", address, result["ts"][0].(*big.Int))
		}
	}
}

func ExampleMultiCallV2() {
	for address, chain := range ContractExamples {
		caller := call.NewContractBuilder().
			WithChainConfig(call.DefaultChainConfigs[chain]).
			AddMethod("totalSupply()(uint256)")
		result, err := caller.AddCall("ts", address, "totalSupply").FlexibleCall(true)
		if err != nil {
			fmt.Printf("Error to call %s contract on %s\n", address, chain)
		} else {
			success := result["ts"].Success
			if success {
				fmt.Printf("Call %s success with total supply is %d\n", address, result["ts"].ReturnData[0].(*big.Int))
			} else {
				fmt.Printf("Call %s failed\n", address)
			}
		}
	}
}

func ExampleMultiCallWithBatch() {
	caller := call.NewContractBuilder().
		WithChainConfig(call.DefaultChainConfigs[call.Ethereum]).
		AddMethod("totalSupply()(uint256)")
	for _, address := range Contracts {
		caller.AddCall(address, address, "totalSupply")
	}

	results, err := caller.FlexibleCall(false)

	if err != nil {
		log.Fatal(err)
	} else {
		for key, result := range results {
			success := result.Success
			if success {
				fmt.Printf("Call %s success with total supply is %d\n", key, result.ReturnData[0].(*big.Int))
			} else {
				fmt.Printf("Call %s failed\n", key)
			}
		}
	}
}

multicall-go's People

Contributors

haihv avatar himitsuko avatar ngovietanh21 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.