Documentation
¶
Index ¶
Constants ¶
const DefaultMulticall3Address = "0xcA11bde05977b3631167028862bE2a173976CA11"
DefaultMulticall3Address is the standard deployment address for Multicall3 contract (universal across all EVM chains)
Variables ¶
var ( // TryPack functions - encode method calls into calldata (return error on failure) TryPackGetBasefee = codec.TryPackGetBasefee TryPackGetBlockHash = codec.TryPackGetBlockHash TryPackGetBlockNumber = codec.TryPackGetBlockNumber TryPackGetChainId = codec.TryPackGetChainId TryPackGetCurrentBlockCoinbase = codec.TryPackGetCurrentBlockCoinbase TryPackGetCurrentBlockDifficulty = codec.TryPackGetCurrentBlockDifficulty TryPackGetCurrentBlockGasLimit = codec.TryPackGetCurrentBlockGasLimit TryPackGetCurrentBlockTimestamp = codec.TryPackGetCurrentBlockTimestamp TryPackGetEthBalance = codec.TryPackGetEthBalance TryPackGetLastBlockHash = codec.TryPackGetLastBlockHash // Unpack functions - decode result data into Go types UnpackGetBasefee = codec.UnpackGetBasefee UnpackGetBlockHash = codec.UnpackGetBlockHash UnpackGetBlockNumber = codec.UnpackGetBlockNumber UnpackGetChainId = codec.UnpackGetChainId UnpackGetCurrentBlockCoinbase = codec.UnpackGetCurrentBlockCoinbase UnpackGetCurrentBlockDifficulty = codec.UnpackGetCurrentBlockDifficulty UnpackGetCurrentBlockGasLimit = codec.UnpackGetCurrentBlockGasLimit UnpackGetCurrentBlockTimestamp = codec.UnpackGetCurrentBlockTimestamp UnpackGetEthBalance = codec.UnpackGetEthBalance UnpackGetLastBlockHash = codec.UnpackGetLastBlockHash )
var ( // ErrResultMissing result count does not match call count ErrResultMissing = errors.New("multicall: result missing from response") )
Functions ¶
func AddWithCallback ¶
func AddWithCallback[T any]( b *Batch, addr common.Address, pack func() ([]byte, error), unpack func([]byte) (T, error), callback func(T, error), )
AddWithCallback adds a call to the batch, automatically dispatches results via callback on Execute Suitable for batch operations, no need to process results in an external loop
Usage:
for _, p := range pairs {
p := p
multicall.AddWithCallback(batch, p.Address,
pair.TryPackGetReserves,
pair.UnpackGetReserves,
func(r GetReservesOutput, err error) {
if err != nil { return }
p.Reserve0 = r.Reserve0
p.Reserve1 = r.Reserve1
},
)
}
batch.Execute(ctx)
func IsCallFailed ¶
IsCallFailed checks if error is an individual call failure
func RevertReason ¶
RevertReason extracts revert reason from error, returns empty string if not CallFailedError
Types ¶
type Batch ¶
type Batch struct {
// contains filtered or unexported fields
}
Batch represents a batch of pending calls
type CallFailedError ¶
type CallFailedError struct {
Reason string // Decoded revert reason
Data []byte // Raw return data
}
CallFailedError error for individual call failure, includes revert reason (if any)
func (*CallFailedError) Error ¶
func (e *CallFailedError) Error() string
type Caller ¶
type Caller interface {
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
}
Caller is the underlying RPC call interface, implemented by *ethclient.Client
type Config ¶
type Config struct {
Address common.Address // Multicall3 contract address
BatchSize int // Max calls per batch
Concurrency int // Concurrent batch count
AllowFailure bool // Whether to allow individual call failures by default
Aggregate bool // Use aggregate instead of aggregate3 (reverts on any failure)
}
Config client configuration
type Multicall ¶
type Multicall struct {
// contains filtered or unexported fields
}
Multicall client
type Option ¶
type Option func(*Config)
Option configuration option
func WithAddress ¶
WithAddress sets custom Multicall3 contract address
func WithAggregate ¶ added in v0.0.2
func WithAggregate() Option
WithAggregate uses the aggregate method instead of aggregate3. Aggregate reverts the entire batch if any single call fails.
func WithAllowFailure ¶
WithAllowFailure sets whether to allow individual call failures by default
func WithConcurrency ¶
WithConcurrency sets concurrent batch count
type Result ¶
Result generic result container, read after Execute
func Add ¶
func Add[T any]( b *Batch, addr common.Address, pack func() ([]byte, error), unpack func([]byte) (T, error), ) *Result[T]
Add adds a call to the batch, returns a Result reference pack: abigen v2 TryPack* method (or wrapper closure) unpack: abigen v2 Unpack* method
Usage:
r := multicall.Add(batch, pairAddr, pair.TryPackGetReserves, pair.UnpackGetReserves) batch.Execute(ctx) fmt.Println(r.Value.Reserve0)
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
builtin
command
Package main demonstrates using Multicall3 built-in methods These methods are available as package-level functions for convenience.
|
Package main demonstrates using Multicall3 built-in methods These methods are available as package-level functions for convenience. |
|
callback-based
command
Package main demonstrates Pattern 2: Callback-Based usage of multicall Add calls with a callback function that automatically receives results during Execute().
|
Package main demonstrates Pattern 2: Callback-Based usage of multicall Add calls with a callback function that automatically receives results during Execute(). |
|
result-based
command
Package main demonstrates Pattern 1: Result-Based usage of multicall Add calls and get a Result reference.
|
Package main demonstrates Pattern 1: Result-Based usage of multicall Add calls and get a Result reference. |