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 ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"reflect" "reflect"
@ -75,20 +76,24 @@ func (call *Call) AllowFailure() *Call {
// Unpack unpacks and converts EVM outputs and sets struct fields. // Unpack unpacks and converts EVM outputs and sets struct fields.
func (call *Call) Unpack(b []byte) error { 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) out, err := call.Contract.ABI.Unpack(call.Method, b)
if err != nil { if err != nil {
return fmt.Errorf("failed to unpack '%s' outputs: %v", call.Method, err) return fmt.Errorf("failed to unpack '%s' outputs: %v", call.Method, err)
} }
if call.Outputs == nil {
if t.Kind() != reflect.Struct {
call.Outputs = out call.Outputs = out
return nil 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() fieldCount := t.NumField()
for i := 0; i < fieldCount; i++ { for i := 0; i < fieldCount; i++ {
field := t.Field(i) field := t.Field(i)