From 12c2d287e92d9849b9ea5957bebd0336999879b2 Mon Sep 17 00:00:00 2001 From: ziyeziye Date: Mon, 11 Dec 2023 16:09:42 +0800 Subject: [PATCH] Outputs type supports nil --- call.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/call.go b/call.go index a4c6b54..30f0ab7 100644 --- a/call.go +++ b/call.go @@ -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)