@@ -23,6 +23,8 @@ import (
2323 "regexp"
2424 "strconv"
2525 "strings"
26+
27+ "github.com/ethereum/go-ethereum/common"
2628)
2729
2830// Type enumerator
@@ -45,16 +47,16 @@ const (
4547// Type is the reflection of the supported argument type
4648type Type struct {
4749 Elem * Type
48- Type reflect.Type
4950 Size int
5051 T byte // Our own type checking
5152
5253 stringKind string // holds the unparsed string for deriving signatures
5354
5455 // Tuple relative fields
55- TupleRawName string // Raw struct name defined in source code, may be empty.
56- TupleElems []* Type // Type information of all tuple fields
57- TupleRawNames []string // Raw field name of all tuple fields
56+ TupleRawName string // Raw struct name defined in source code, may be empty.
57+ TupleElems []* Type // Type information of all tuple fields
58+ TupleRawNames []string // Raw field name of all tuple fields
59+ TupleType reflect.Type // Underlying struct of the tuple
5860}
5961
6062var (
@@ -94,7 +96,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
9496 // is a slice
9597 typ .T = SliceTy
9698 typ .Elem = & embeddedType
97- typ .Type = reflect .SliceOf (embeddedType .Type )
9899 typ .stringKind = embeddedType .stringKind + sliced
99100 } else if len (intz ) == 1 {
100101 // is a array
@@ -104,7 +105,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
104105 if err != nil {
105106 return Type {}, fmt .Errorf ("abi: error parsing variable size: %v" , err )
106107 }
107- typ .Type = reflect .ArrayOf (typ .Size , embeddedType .Type )
108108 typ .stringKind = embeddedType .stringKind + sliced
109109 } else {
110110 return Type {}, fmt .Errorf ("invalid formatting of array type" )
@@ -136,31 +136,24 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
136136 // varType is the parsed abi type
137137 switch varType := parsedType [1 ]; varType {
138138 case "int" :
139- typ .Type = reflectIntType (false , varSize )
140139 typ .Size = varSize
141140 typ .T = IntTy
142141 case "uint" :
143- typ .Type = reflectIntType (true , varSize )
144142 typ .Size = varSize
145143 typ .T = UintTy
146144 case "bool" :
147145 typ .T = BoolTy
148- typ .Type = reflect .TypeOf (bool (false ))
149146 case "address" :
150- typ .Type = addressT
151147 typ .Size = 20
152148 typ .T = AddressTy
153149 case "string" :
154- typ .Type = reflect .TypeOf ("" )
155150 typ .T = StringTy
156151 case "bytes" :
157152 if varSize == 0 {
158153 typ .T = BytesTy
159- typ .Type = reflect .SliceOf (reflect .TypeOf (byte (0 )))
160154 } else {
161155 typ .T = FixedBytesTy
162156 typ .Size = varSize
163- typ .Type = reflect .ArrayOf (varSize , reflect .TypeOf (byte (0 )))
164157 }
165158 case "tuple" :
166159 var (
@@ -180,7 +173,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
180173 }
181174 fields = append (fields , reflect.StructField {
182175 Name : ToCamelCase (c .Name ), // reflect.StructOf will panic for any exported field.
183- Type : cType .Type ,
176+ Type : cType .getType () ,
184177 Tag : reflect .StructTag ("json:\" " + c .Name + "\" " ),
185178 })
186179 elems = append (elems , & cType )
@@ -191,7 +184,8 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
191184 }
192185 }
193186 expression += ")"
194- typ .Type = reflect .StructOf (fields )
187+
188+ typ .TupleType = reflect .StructOf (fields )
195189 typ .TupleElems = elems
196190 typ .TupleRawNames = names
197191 typ .T = TupleTy
@@ -210,14 +204,48 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
210204 case "function" :
211205 typ .T = FunctionTy
212206 typ .Size = 24
213- typ .Type = reflect .ArrayOf (24 , reflect .TypeOf (byte (0 )))
214207 default :
215208 return Type {}, fmt .Errorf ("unsupported arg type: %s" , t )
216209 }
217210
218211 return
219212}
220213
214+ func (t Type ) getType () reflect.Type {
215+ switch t .T {
216+ case IntTy :
217+ return reflectIntType (false , t .Size )
218+ case UintTy :
219+ return reflectIntType (true , t .Size )
220+ case BoolTy :
221+ return reflect .TypeOf (false )
222+ case StringTy :
223+ return reflect .TypeOf ("" )
224+ case SliceTy :
225+ return reflect .SliceOf (t .Elem .getType ())
226+ case ArrayTy :
227+ return reflect .ArrayOf (t .Size , t .Elem .getType ())
228+ case TupleTy :
229+ return t .TupleType
230+ case AddressTy :
231+ return reflect .TypeOf (common.Address {})
232+ case FixedBytesTy :
233+ return reflect .ArrayOf (t .Size , reflect .TypeOf (byte (0 )))
234+ case BytesTy :
235+ return reflect .SliceOf (reflect .TypeOf (byte (0 )))
236+ case HashTy :
237+ // hashtype currently not used
238+ return reflect .ArrayOf (32 , reflect .TypeOf (byte (0 )))
239+ case FixedPointTy :
240+ // fixedpoint type currently not used
241+ return reflect .ArrayOf (32 , reflect .TypeOf (byte (0 )))
242+ case FunctionTy :
243+ return reflect .ArrayOf (24 , reflect .TypeOf (byte (0 )))
244+ default :
245+ panic ("Invalid type" )
246+ }
247+ }
248+
221249// String implements Stringer
222250func (t Type ) String () (out string ) {
223251 return t .stringKind
0 commit comments