go-multicall/call.go
cary c6cbb984a3 fix: multiple bug fixes and safety improvements
- call.go: safe type assertion in UnpackResult() to prevent panic
- call.go: warn when ABI returns more values than struct fields
- caller.go: use len() check instead of nil check for variadic multicallAddr
- caller.go: return allCalls (partial results) on CallChunked error
- caller_test.go: strip 4-byte method selector from stub return data

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-18 16:32:52 +08:00

141 lines
3.2 KiB
Go

package multicall
import (
"bytes"
"errors"
"fmt"
"log"
"reflect"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
// Contract wraps the parsed ABI and acts as a call factory.
type Contract struct {
ABI *abi.ABI
Address common.Address
}
// NewContract creates a new call factory.
func NewContract(rawJson, address string) (*Contract, error) {
parsedABI, err := ParseABI(rawJson)
if err != nil {
return nil, err
}
return &Contract{
ABI: parsedABI,
Address: common.HexToAddress(address),
}, nil
}
// ParseABI parses raw ABI JSON.
func ParseABI(rawJson string) (*abi.ABI, error) {
parsed, err := abi.JSON(bytes.NewBufferString(rawJson))
if err != nil {
return nil, fmt.Errorf("failed to parse abi: %v", err)
}
return &parsed, nil
}
// Call wraps a multicall call.
type Call struct {
CallName string
Contract *Contract
Method string
Extend any
Inputs []any
Outputs any
CanFail bool
Failed bool
}
// NewCall creates a new call using given inputs.
// Outputs type is the expected output struct to unpack and set values in.
func (contract *Contract) NewCall(
outputs any, methodName string, inputs ...any,
) *Call {
return &Call{
Contract: contract,
Method: methodName,
Inputs: inputs,
Outputs: outputs,
}
}
// Name sets a name for the call.
func (call *Call) Name(name string) *Call {
call.CallName = name
return call
}
func (call *Call) SetExtend(ext any) *Call {
call.Extend = ext
return call
}
func (call *Call) UnpackResult() []interface{} {
if call.Outputs == nil {
return nil
}
result, ok := call.Outputs.([]interface{})
if !ok {
return nil
}
return result
}
// AllowFailure sets if the call is allowed to fail. This helps avoiding a revert
// when one of the calls in the array fails.
func (call *Call) AllowFailure() *Call {
call.CanFail = true
return call
}
// Unpack unpacks and converts EVM outputs and sets struct fields.
func (call *Call) Unpack(b []byte) error {
out, err := call.Contract.ABI.Unpack(call.Method, b)
if err != nil {
return fmt.Errorf("failed to unpack '%s' outputs: %v", call.Method, err)
}
if call.Outputs == nil {
call.Outputs = out
return nil
}
t := reflect.ValueOf(call.Outputs)
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return errors.New("outputs type is not a struct")
}
fieldCount := t.NumField()
if fieldCount > len(out) {
return fmt.Errorf("struct has %d fields but ABI returned %d values", fieldCount, len(out))
}
if len(out) > fieldCount {
log.Printf("warning: method '%s' returned %d values but struct only has %d fields; extra values ignored", call.Method, len(out), fieldCount)
}
for i := 0; i < fieldCount; i++ {
field := t.Field(i)
if !field.CanSet() {
return fmt.Errorf("field %d (%s) is not settable (unexported?)", i, t.Type().Field(i).Name)
}
converted := abi.ConvertType(out[i], field.Interface())
field.Set(reflect.ValueOf(converted))
}
return nil
}
// Pack converts and packs EVM inputs.
func (call *Call) Pack() ([]byte, error) {
b, err := call.Contract.ABI.Pack(call.Method, call.Inputs...)
if err != nil {
return nil, fmt.Errorf("failed to pack '%s' inputs: %v", call.Method, err)
}
return b, nil
}