mirror of
https://github.com/forta-network/go-multicall.git
synced 2026-02-26 07:37:23 +00:00
92 lines
2.1 KiB
Markdown
92 lines
2.1 KiB
Markdown
# go-multicall
|
|

|
|
|
|
A thin Go client for making multiple function calls in single `eth_call` request
|
|
|
|
- Uses the go-ethereum tools and libraries
|
|
- Interfaces with the [MakerDAO `Multicall3` contract](https://github.com/mds1/multicall)
|
|
|
|
_**Warning:** MakerDAO Multicall contracts are different than the [forta-network Multicall contract](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Multicall.sol). Please see [this thread](https://forum.openzeppelin.com/t/multicall-by-oz-and-makerdao-has-a-difference/9350) in the forta-network forum if you are looking for an explanation._
|
|
|
|
## Install
|
|
|
|
```
|
|
go get github.com/forta-network/go-multicall
|
|
```
|
|
|
|
## Example
|
|
|
|
(See other examples under the `examples` directory!)
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"github.com/forta-network/go-multicall"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
const (
|
|
APIURL = "https://cloudflare-eth.com"
|
|
ERC20ABI = `[
|
|
{
|
|
"constant":true,
|
|
"inputs":[
|
|
{
|
|
"name":"tokenOwner",
|
|
"type":"address"
|
|
}
|
|
],
|
|
"name":"balanceOf",
|
|
"outputs":[
|
|
{
|
|
"name":"balance",
|
|
"type":"uint256"
|
|
}
|
|
],
|
|
"payable":false,
|
|
"stateMutability":"view",
|
|
"type":"function"
|
|
}
|
|
]`
|
|
)
|
|
|
|
type balanceOutput struct {
|
|
Balance *big.Int
|
|
}
|
|
|
|
func main() {
|
|
caller, err := multicall.Dial(context.Background(), APIURL)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
contract, err := multicall.NewContract(ERC20ABI, "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
calls, err := caller.Call(nil,
|
|
contract.NewCall(
|
|
new(balanceOutput),
|
|
"balanceOf",
|
|
common.HexToAddress("0xcEe284F754E854890e311e3280b767F80797180d"), // Arbitrum One gateway
|
|
).Name("Arbitrum One gateway balance"),
|
|
contract.NewCall(
|
|
new(balanceOutput),
|
|
"balanceOf",
|
|
common.HexToAddress("0x40ec5B33f54e0E8A33A975908C5BA1c14e5BbbDf"), // Polygon ERC20 bridge
|
|
).Name("Polygon ERC20 bridge balance"),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for _, call := range calls {
|
|
fmt.Println(call.CallName, ":", call.Outputs.(*balanceOutput).Balance)
|
|
}
|
|
}
|
|
```
|