Outputs type supports nil

This commit is contained in:
ziyeziye 2023-12-11 16:09:42 +08:00
parent 1fbee12081
commit 12c2d287e9

17
call.go
View file

@ -2,6 +2,7 @@ package multicall
import (
"bytes"
"errors"
"fmt"
"reflect"
@ -75,20 +76,24 @@ func (call *Call) AllowFailure() *Call {
// Unpack unpacks and converts EVM outputs and sets struct fields.
func (call *Call) Unpack(b []byte) error {
t := reflect.ValueOf(call.Outputs)
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
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 t.Kind() != reflect.Struct {
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()
for i := 0; i < fieldCount; i++ {
field := t.Field(i)