@@ -33,13 +33,6 @@ import (
3333 "github.com/ethereum/go-ethereum/log"
3434)
3535
36- // Lang is a target programming language selector to generate bindings for.
37- type Lang int
38-
39- const (
40- LangGo Lang = iota
41- )
42-
4336func isKeyWord (arg string ) bool {
4437 switch arg {
4538 case "break" :
@@ -81,38 +74,33 @@ func isKeyWord(arg string) bool {
8174// to be used as is in client code, but rather as an intermediate struct which
8275// enforces compile time type safety and naming convention as opposed to having to
8376// manually maintain hard coded strings that break on runtime.
84- func Bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang , libs map [string ]string , aliases map [string ]string ) (string , error ) {
85- data , err := bind (types , abis , bytecodes , fsigs , pkg , lang , libs , aliases )
77+ func Bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , libs map [string ]string , aliases map [string ]string ) (string , error ) {
78+ data , err := bind (types , abis , bytecodes , fsigs , pkg , libs , aliases )
8679 if err != nil {
8780 return "" , err
8881 }
8982 buffer := new (bytes.Buffer )
9083
9184 funcs := map [string ]interface {}{
92- "bindtype" : bindType [lang ],
93- "bindtopictype" : bindTopicType [lang ],
94- "namedtype" : namedType [lang ],
85+ "bindtype" : bindType ,
86+ "bindtopictype" : bindTopicType ,
9587 "capitalise" : capitalise ,
9688 "decapitalise" : decapitalise ,
9789 }
98- tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSource [ lang ] ))
90+ tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSource ))
9991 if err := tmpl .Execute (buffer , data ); err != nil {
10092 return "" , err
10193 }
102- // For Go bindings pass the code through gofmt to clean it up
103- if lang == LangGo {
104- code , err := format .Source (buffer .Bytes ())
105- if err != nil {
106- return "" , fmt .Errorf ("%v\n %s" , err , buffer )
107- }
108- return string (code ), nil
94+ // Pass the code through gofmt to clean it up
95+ code , err := format .Source (buffer .Bytes ())
96+ if err != nil {
97+ return "" , fmt .Errorf ("%v\n %s" , err , buffer )
10998 }
110- // For all others just return as is for now
111- return buffer .String (), nil
99+ return string (code ), nil
112100}
113101
114- func BindV2 (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang , libs map [string ]string , aliases map [string ]string ) (string , error ) {
115- data , err := bind (types , abis , bytecodes , fsigs , pkg , lang , libs , aliases )
102+ func BindV2 (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , libs map [string ]string , aliases map [string ]string ) (string , error ) {
103+ data , err := bind (types , abis , bytecodes , fsigs , pkg , libs , aliases )
116104 if err != nil {
117105 return "" , err
118106 }
@@ -153,29 +141,24 @@ func BindV2(types []string, abis []string, bytecodes []string, fsigs []map[strin
153141 }
154142 buffer := new (bytes.Buffer )
155143 funcs := map [string ]interface {}{
156- "bindtype" : bindType [lang ],
157- "bindtopictype" : bindTopicType [lang ],
158- "namedtype" : namedType [lang ],
144+ "bindtype" : bindType ,
145+ "bindtopictype" : bindTopicType ,
159146 "capitalise" : capitalise ,
160147 "decapitalise" : decapitalise ,
161148 }
162- tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSourceV2 [ lang ] ))
149+ tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSourceV2 ))
163150 if err := tmpl .Execute (buffer , data ); err != nil {
164151 return "" , err
165152 }
166- // For Go bindings pass the code through gofmt to clean it up
167- if lang == LangGo {
168- code , err := format .Source (buffer .Bytes ())
169- if err != nil {
170- return "" , fmt .Errorf ("%v\n %s" , err , buffer )
171- }
172- return string (code ), nil
153+ // Pass the code through gofmt to clean it up
154+ code , err := format .Source (buffer .Bytes ())
155+ if err != nil {
156+ return "" , fmt .Errorf ("%v\n %s" , err , buffer )
173157 }
174- // For all others just return as is for now
175- return buffer .String (), nil
158+ return string (code ), nil
176159}
177160
178- func bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang , libs map [string ]string , aliases map [string ]string ) (* tmplData , error ) {
161+ func bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , libs map [string ]string , aliases map [string ]string ) (* tmplData , error ) {
179162 var (
180163 // contracts is the map of each individual contract requested binding
181164 contracts = make (map [string ]* tmplContract )
@@ -219,14 +202,14 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
219202
220203 for _ , input := range evmABI .Constructor .Inputs {
221204 if hasStruct (input .Type ) {
222- bindStructType [ lang ] (input .Type , structs )
205+ bindStructType (input .Type , structs )
223206 }
224207 }
225208
226209 for _ , original := range evmABI .Methods {
227210 // Normalize the method for capital cases and non-anonymous inputs/outputs
228211 normalized := original
229- normalizedName := methodNormalizer [ lang ] (alias (aliases , original .Name ))
212+ normalizedName := methodNormalizer (alias (aliases , original .Name ))
230213 // Ensure there is no duplicated identifier
231214 var identifiers = callIdentifiers
232215 if ! original .IsConstant () {
@@ -253,7 +236,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
253236 normalized .Inputs [j ].Name = fmt .Sprintf ("arg%d" , j )
254237 }
255238 if hasStruct (input .Type ) {
256- bindStructType [ lang ] (input .Type , structs )
239+ bindStructType (input .Type , structs )
257240 }
258241 }
259242 normalized .Outputs = make ([]abi.Argument , len (original .Outputs ))
@@ -263,7 +246,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
263246 normalized .Outputs [j ].Name = capitalise (output .Name )
264247 }
265248 if hasStruct (output .Type ) {
266- bindStructType [ lang ] (output .Type , structs )
249+ bindStructType (output .Type , structs )
267250 }
268251 }
269252 // Append the methods to the call or transact lists
@@ -282,15 +265,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
282265 normalized := original
283266
284267 // Ensure there is no duplicated identifier
285- normalizedName := methodNormalizer [lang ](alias (aliases , original .Name ))
286- // Name shouldn't start with a digit. It will make the generated code invalid.
287- if len (normalizedName ) > 0 && unicode .IsDigit (rune (normalizedName [0 ])) {
288- normalizedName = fmt .Sprintf ("E%s" , normalizedName )
289- normalizedName = abi .ResolveNameConflict (normalizedName , func (name string ) bool {
290- _ , ok := eventIdentifiers [name ]
291- return ok
292- })
293- }
268+ normalizedName := methodNormalizer (alias (aliases , original .Name ))
294269 if eventIdentifiers [normalizedName ] {
295270 return nil , fmt .Errorf ("duplicated identifier \" %s\" (normalized \" %s\" ), use --alias for renaming" , original .Name , normalizedName )
296271 }
@@ -314,7 +289,7 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
314289 normalized .Inputs [j ].Name = fmt .Sprintf ("%s%d" , normalized .Inputs [j ].Name , index )
315290 }
316291 if hasStruct (input .Type ) {
317- bindStructType [ lang ] (input .Type , structs )
292+ bindStructType (input .Type , structs )
318293 }
319294 }
320295 // Append the event to the accumulator list
@@ -375,14 +350,8 @@ func bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
375350 return data , nil
376351}
377352
378- // bindType is a set of type binders that convert Solidity types to some supported
379- // programming language types.
380- var bindType = map [Lang ]func (kind abi.Type , structs map [string ]* tmplStruct ) string {
381- LangGo : bindTypeGo ,
382- }
383-
384- // bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
385- func bindBasicTypeGo (kind abi.Type ) string {
353+ // bindBasicType converts basic solidity types(except array, slice and tuple) to Go ones.
354+ func bindBasicType (kind abi.Type ) string {
386355 switch kind .T {
387356 case abi .AddressTy :
388357 return "common.Address"
@@ -405,32 +374,26 @@ func bindBasicTypeGo(kind abi.Type) string {
405374 }
406375}
407376
408- // bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
377+ // bindType converts solidity types to Go ones. Since there is no clear mapping
409378// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
410379// mapped will use an upscaled type (e.g. BigDecimal).
411- func bindTypeGo (kind abi.Type , structs map [string ]* tmplStruct ) string {
380+ func bindType (kind abi.Type , structs map [string ]* tmplStruct ) string {
412381 switch kind .T {
413382 case abi .TupleTy :
414383 return structs [kind .TupleRawName + kind .String ()].Name
415384 case abi .ArrayTy :
416- return fmt .Sprintf ("[%d]" , kind .Size ) + bindTypeGo (* kind .Elem , structs )
385+ return fmt .Sprintf ("[%d]" , kind .Size ) + bindType (* kind .Elem , structs )
417386 case abi .SliceTy :
418- return "[]" + bindTypeGo (* kind .Elem , structs )
387+ return "[]" + bindType (* kind .Elem , structs )
419388 default :
420- return bindBasicTypeGo (kind )
389+ return bindBasicType (kind )
421390 }
422391}
423392
424- // bindTopicType is a set of type binders that convert Solidity types to some
425- // supported programming language topic types.
426- var bindTopicType = map [Lang ]func (kind abi.Type , structs map [string ]* tmplStruct ) string {
427- LangGo : bindTopicTypeGo ,
428- }
429-
430- // bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
393+ // bindTopicType converts a Solidity topic type to a Go one. It is almost the same
431394// functionality as for simple types, but dynamic types get converted to hashes.
432- func bindTopicTypeGo (kind abi.Type , structs map [string ]* tmplStruct ) string {
433- bound := bindTypeGo (kind , structs )
395+ func bindTopicType (kind abi.Type , structs map [string ]* tmplStruct ) string {
396+ bound := bindType (kind , structs )
434397
435398 // todo(rjl493456442) according solidity documentation, indexed event
436399 // parameters that are not value types i.e. arrays and structs are not
@@ -444,16 +407,10 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
444407 return bound
445408}
446409
447- // bindStructType is a set of type binders that convert Solidity tuple types to some supported
448- // programming language struct definition.
449- var bindStructType = map [Lang ]func (kind abi.Type , structs map [string ]* tmplStruct ) string {
450- LangGo : bindStructTypeGo ,
451- }
452-
453- // bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
410+ // bindStructType converts a Solidity tuple type to a Go one and records the mapping
454411// in the given map.
455412// Notably, this function will resolve and record nested struct recursively.
456- func bindStructTypeGo (kind abi.Type , structs map [string ]* tmplStruct ) string {
413+ func bindStructType (kind abi.Type , structs map [string ]* tmplStruct ) string {
457414 switch kind .T {
458415 case abi .TupleTy :
459416 // We compose a raw struct name and a canonical parameter expression
@@ -474,7 +431,7 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
474431 name := capitalise (kind .TupleRawNames [i ])
475432 name = abi .ResolveNameConflict (name , func (s string ) bool { return names [s ] })
476433 names [name ] = true
477- fields = append (fields , & tmplField {Type : bindStructTypeGo (* elem , structs ), Name : name , SolKind : * elem })
434+ fields = append (fields , & tmplField {Type : bindStructType (* elem , structs ), Name : name , SolKind : * elem })
478435 }
479436 name := kind .TupleRawName
480437 if name == "" {
@@ -488,20 +445,14 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
488445 }
489446 return name
490447 case abi .ArrayTy :
491- return fmt .Sprintf ("[%d]" , kind .Size ) + bindStructTypeGo (* kind .Elem , structs )
448+ return fmt .Sprintf ("[%d]" , kind .Size ) + bindStructType (* kind .Elem , structs )
492449 case abi .SliceTy :
493- return "[]" + bindStructTypeGo (* kind .Elem , structs )
450+ return "[]" + bindStructType (* kind .Elem , structs )
494451 default :
495- return bindBasicTypeGo (kind )
452+ return bindBasicType (kind )
496453 }
497454}
498455
499- // namedType is a set of functions that transform language specific types to
500- // named versions that may be used inside method names.
501- var namedType = map [Lang ]func (string , abi.Type ) string {
502- LangGo : func (string , abi.Type ) string { panic ("this shouldn't be needed" ) },
503- }
504-
505456// alias returns an alias of the given string based on the aliasing rules
506457// or returns itself if no rule is matched.
507458func alias (aliases map [string ]string , n string ) string {
@@ -512,10 +463,8 @@ func alias(aliases map[string]string, n string) string {
512463}
513464
514465// methodNormalizer is a name transformer that modifies Solidity method names to
515- // conform to target language naming conventions.
516- var methodNormalizer = map [Lang ]func (string ) string {
517- LangGo : abi .ToCamelCase ,
518- }
466+ // conform to Go naming conventions.
467+ var methodNormalizer = abi .ToCamelCase
519468
520469// capitalise makes a camel-case string which starts with an upper case character.
521470var capitalise = abi .ToCamelCase
0 commit comments