@@ -20,10 +20,6 @@ import (
2020 "encoding/json"
2121 "fmt"
2222 "io"
23- "reflect"
24- "strings"
25-
26- "github.com/ethereum/go-ethereum/common"
2723)
2824
2925// The ABI holds information about a contract's context and available
@@ -76,106 +72,27 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
7672 return append (method .Id (), arguments ... ), nil
7773}
7874
79- // these variable are used to determine certain types during type assertion for
80- // assignment.
81- var (
82- r_interSlice = reflect .TypeOf ([]interface {}{})
83- r_hash = reflect .TypeOf (common.Hash {})
84- r_bytes = reflect .TypeOf ([]byte {})
85- r_byte = reflect .TypeOf (byte (0 ))
86- )
87-
8875// Unpack output in v according to the abi specification
89- func (abi ABI ) Unpack (v interface {}, name string , output []byte ) error {
90- var method = abi .Methods [name ]
91-
92- if len (output ) == 0 {
93- return fmt .Errorf ("abi: unmarshalling empty output" )
94- }
95-
96- // make sure the passed value is a pointer
97- valueOf := reflect .ValueOf (v )
98- if reflect .Ptr != valueOf .Kind () {
99- return fmt .Errorf ("abi: Unpack(non-pointer %T)" , v )
76+ func (abi ABI ) Unpack (v interface {}, name string , output []byte ) (err error ) {
77+ if err = bytesAreProper (output ); err != nil {
78+ return err
10079 }
101-
102- var (
103- value = valueOf .Elem ()
104- typ = value .Type ()
105- )
106-
107- if len (method .Outputs ) > 1 {
108- switch value .Kind () {
109- // struct will match named return values to the struct's field
110- // names
111- case reflect .Struct :
112- for i := 0 ; i < len (method .Outputs ); i ++ {
113- marshalledValue , err := toGoType (i , method .Outputs [i ], output )
114- if err != nil {
115- return err
116- }
117- reflectValue := reflect .ValueOf (marshalledValue )
118-
119- for j := 0 ; j < typ .NumField (); j ++ {
120- field := typ .Field (j )
121- // TODO read tags: `abi:"fieldName"`
122- if field .Name == strings .ToUpper (method .Outputs [i ].Name [:1 ])+ method .Outputs [i ].Name [1 :] {
123- if err := set (value .Field (j ), reflectValue , method .Outputs [i ]); err != nil {
124- return err
125- }
126- }
127- }
128- }
129- case reflect .Slice :
130- if ! value .Type ().AssignableTo (r_interSlice ) {
131- return fmt .Errorf ("abi: cannot marshal tuple in to slice %T (only []interface{} is supported)" , v )
132- }
133-
134- // if the slice already contains values, set those instead of the interface slice itself.
135- if value .Len () > 0 {
136- if len (method .Outputs ) > value .Len () {
137- return fmt .Errorf ("abi: cannot marshal in to slices of unequal size (require: %v, got: %v)" , len (method .Outputs ), value .Len ())
138- }
139-
140- for i := 0 ; i < len (method .Outputs ); i ++ {
141- marshalledValue , err := toGoType (i , method .Outputs [i ], output )
142- if err != nil {
143- return err
144- }
145- reflectValue := reflect .ValueOf (marshalledValue )
146- if err := set (value .Index (i ).Elem (), reflectValue , method .Outputs [i ]); err != nil {
147- return err
148- }
149- }
150- return nil
151- }
152-
153- // create a new slice and start appending the unmarshalled
154- // values to the new interface slice.
155- z := reflect .MakeSlice (typ , 0 , len (method .Outputs ))
156- for i := 0 ; i < len (method .Outputs ); i ++ {
157- marshalledValue , err := toGoType (i , method .Outputs [i ], output )
158- if err != nil {
159- return err
160- }
161- z = reflect .Append (z , reflect .ValueOf (marshalledValue ))
162- }
163- value .Set (z )
164- default :
165- return fmt .Errorf ("abi: cannot unmarshal tuple in to %v" , typ )
166- }
167-
80+ // since there can't be naming collisions with contracts and events,
81+ // we need to decide whether we're calling a method or an event
82+ var unpack unpacker
83+ if method , ok := abi .Methods [name ]; ok {
84+ unpack = method
85+ } else if event , ok := abi .Events [name ]; ok {
86+ unpack = event
16887 } else {
169- marshalledValue , err := toGoType (0 , method .Outputs [0 ], output )
170- if err != nil {
171- return err
172- }
173- if err := set (value , reflect .ValueOf (marshalledValue ), method .Outputs [0 ]); err != nil {
174- return err
175- }
88+ return fmt .Errorf ("abi: could not locate named method or event." )
17689 }
17790
178- return nil
91+ // requires a struct to unpack into for a tuple return...
92+ if unpack .isTupleReturn () {
93+ return unpack .tupleUnpack (v , output )
94+ }
95+ return unpack .singleUnpack (v , output )
17996}
18097
18198func (abi * ABI ) UnmarshalJSON (data []byte ) error {
0 commit comments