@@ -47,13 +47,17 @@ const (
4747// to be used as is in client code, but rather as an intermediate struct which
4848// enforces compile time type safety and naming convention opposed to having to
4949// manually maintain hard coded strings that break on runtime.
50- func Bind (types []string , abis []string , bytecodes []string , fsigs []map [string ]string , pkg string , lang Lang , libs map [string ]string ) (string , error ) {
51- // Process each individual contract requested binding
52- contracts := make (map [string ]* tmplContract )
50+ 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 ) {
51+ var (
52+ // contracts is the map of each individual contract requested binding
53+ contracts = make (map [string ]* tmplContract )
5354
54- // Map used to flag each encountered library as such
55- isLib : = make (map [string ]struct {} )
55+ // structs is the map of all reclared structs shared by passed contracts.
56+ structs = make (map [string ]* tmplStruct )
5657
58+ // isLib is the map used to flag each encountered library as such
59+ isLib = make (map [string ]struct {})
60+ )
5761 for i := 0 ; i < len (types ); i ++ {
5862 // Parse the actual ABI to generate the binding for
5963 evmABI , err := abi .JSON (strings .NewReader (abis [i ]))
@@ -73,13 +77,29 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
7377 calls = make (map [string ]* tmplMethod )
7478 transacts = make (map [string ]* tmplMethod )
7579 events = make (map [string ]* tmplEvent )
76- structs = make (map [string ]* tmplStruct )
80+
81+ // identifiers are used to detect duplicated identifier of function
82+ // and event. For all calls, transacts and events, abigen will generate
83+ // corresponding bindings. However we have to ensure there is no
84+ // identifier coliision in the bindings of these categories.
85+ callIdentifiers = make (map [string ]bool )
86+ transactIdentifiers = make (map [string ]bool )
87+ eventIdentifiers = make (map [string ]bool )
7788 )
7889 for _ , original := range evmABI .Methods {
7990 // Normalize the method for capital cases and non-anonymous inputs/outputs
8091 normalized := original
81- normalized .Name = methodNormalizer [lang ](original .Name )
82-
92+ normalizedName := methodNormalizer [lang ](alias (aliases , original .Name ))
93+ // Ensure there is no duplicated identifier
94+ var identifiers = callIdentifiers
95+ if ! original .Const {
96+ identifiers = transactIdentifiers
97+ }
98+ if identifiers [normalizedName ] {
99+ return "" , fmt .Errorf ("duplicated identifier \" %s\" (normalized \" %s\" ), use --alias for renaming" , original .Name , normalizedName )
100+ }
101+ identifiers [normalizedName ] = true
102+ normalized .Name = normalizedName
83103 normalized .Inputs = make ([]abi.Argument , len (original .Inputs ))
84104 copy (normalized .Inputs , original .Inputs )
85105 for j , input := range normalized .Inputs {
@@ -114,7 +134,14 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
114134 }
115135 // Normalize the event for capital cases and non-anonymous outputs
116136 normalized := original
117- normalized .Name = methodNormalizer [lang ](original .Name )
137+
138+ // Ensure there is no duplicated identifier
139+ normalizedName := methodNormalizer [lang ](alias (aliases , original .Name ))
140+ if eventIdentifiers [normalizedName ] {
141+ return "" , fmt .Errorf ("duplicated identifier \" %s\" (normalized \" %s\" ), use --alias for renaming" , original .Name , normalizedName )
142+ }
143+ eventIdentifiers [normalizedName ] = true
144+ normalized .Name = normalizedName
118145
119146 normalized .Inputs = make ([]abi.Argument , len (original .Inputs ))
120147 copy (normalized .Inputs , original .Inputs )
@@ -144,7 +171,6 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
144171 Transacts : transacts ,
145172 Events : events ,
146173 Libraries : make (map [string ]string ),
147- Structs : structs ,
148174 }
149175 // Function 4-byte signatures are stored in the same sequence
150176 // as types, if available.
@@ -176,6 +202,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
176202 Package : pkg ,
177203 Contracts : contracts ,
178204 Libraries : libs ,
205+ Structs : structs ,
179206 }
180207 buffer := new (bytes.Buffer )
181208
@@ -483,6 +510,15 @@ func namedTypeJava(javaKind string, solKind abi.Type) string {
483510 }
484511}
485512
513+ // alias returns an alias of the given string based on the aliasing rules
514+ // or returns itself if no rule is matched.
515+ func alias (aliases map [string ]string , n string ) string {
516+ if alias , exist := aliases [n ]; exist {
517+ return alias
518+ }
519+ return n
520+ }
521+
486522// methodNormalizer is a name transformer that modifies Solidity method names to
487523// conform to target language naming concentions.
488524var methodNormalizer = map [Lang ]func (string ) string {
0 commit comments