@@ -32,11 +32,20 @@ import (
3232 "golang.org/x/tools/imports"
3333)
3434
35+ // Lang is a target programming language selector to generate bindings for.
36+ type Lang int
37+
38+ const (
39+ LangGo Lang = iota
40+ LangJava
41+ LangObjC
42+ )
43+
3544// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
3645// to be used as is in client code, but rather as an intermediate struct which
3746// enforces compile time type safety and naming convention opposed to having to
3847// manually maintain hard coded strings that break on runtime.
39- func Bind (types []string , abis []string , bytecodes []string , pkg string ) (string , error ) {
48+ func Bind (types []string , abis []string , bytecodes []string , pkg string , lang Lang ) (string , error ) {
4049 // Process each individual contract requested binding
4150 contracts := make (map [string ]* tmplContract )
4251
@@ -62,7 +71,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string) (string
6271 for _ , original := range evmABI .Methods {
6372 // Normalize the method for capital cases and non-anonymous inputs/outputs
6473 normalized := original
65- normalized .Name = capitalise (original .Name )
74+ normalized .Name = methodNormalizer [ lang ] (original .Name )
6675
6776 normalized .Inputs = make ([]abi.Argument , len (original .Inputs ))
6877 copy (normalized .Inputs , original .Inputs )
@@ -78,7 +87,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string) (string
7887 normalized .Outputs [j ].Name = capitalise (output .Name )
7988 }
8089 }
81- // Append the methos to the call or transact lists
90+ // Append the methods to the call or transact lists
8291 if original .Const {
8392 calls [original .Name ] = & tmplMethod {Original : original , Normalized : normalized , Structured : structured (original )}
8493 } else {
@@ -87,7 +96,7 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string) (string
8796 }
8897 contracts [types [i ]] = & tmplContract {
8998 Type : capitalise (types [i ]),
90- InputABI : strippedABI ,
99+ InputABI : strings . Replace ( strippedABI , " \" " , " \\ \" " , - 1 ) ,
91100 InputBin : strings .TrimSpace (bytecodes [i ]),
92101 Constructor : evmABI .Constructor ,
93102 Calls : calls ,
@@ -102,9 +111,12 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string) (string
102111 buffer := new (bytes.Buffer )
103112
104113 funcs := map [string ]interface {}{
105- "bindtype" : bindType ,
114+ "bindtype" : bindType [lang ],
115+ "namedtype" : namedType [lang ],
116+ "capitalise" : capitalise ,
117+ "decapitalise" : decapitalise ,
106118 }
107- tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSource ))
119+ tmpl := template .Must (template .New ("" ).Funcs (funcs ).Parse (tmplSource [ lang ] ))
108120 if err := tmpl .Execute (buffer , data ); err != nil {
109121 return "" , err
110122 }
@@ -116,10 +128,17 @@ func Bind(types []string, abis []string, bytecodes []string, pkg string) (string
116128 return string (code ), nil
117129}
118130
119- // bindType converts a Solidity type to a Go one. Since there is no clear mapping
131+ // bindType is a set of type binders that convert Solidity types to some supported
132+ // programming language.
133+ var bindType = map [Lang ]func (kind abi.Type ) string {
134+ LangGo : bindTypeGo ,
135+ LangJava : bindTypeJava ,
136+ }
137+
138+ // bindTypeGo converts a Solidity type to a Go one. Since there is no clear mapping
120139// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
121140// mapped will use an upscaled type (e.g. *big.Int).
122- func bindType (kind abi.Type ) string {
141+ func bindTypeGo (kind abi.Type ) string {
123142 stringKind := kind .String ()
124143
125144 switch {
@@ -160,11 +179,137 @@ func bindType(kind abi.Type) string {
160179 }
161180}
162181
182+ // bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
183+ // from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
184+ // mapped will use an upscaled type (e.g. BigDecimal).
185+ func bindTypeJava (kind abi.Type ) string {
186+ stringKind := kind .String ()
187+
188+ switch {
189+ case strings .HasPrefix (stringKind , "address" ):
190+ parts := regexp .MustCompile ("address(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (stringKind )
191+ if len (parts ) != 2 {
192+ return stringKind
193+ }
194+ if parts [1 ] == "" {
195+ return fmt .Sprintf ("Address" )
196+ }
197+ return fmt .Sprintf ("Addresses" )
198+
199+ case strings .HasPrefix (stringKind , "bytes" ):
200+ parts := regexp .MustCompile ("bytes([0-9]*)(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (stringKind )
201+ if len (parts ) != 3 {
202+ return stringKind
203+ }
204+ if parts [2 ] != "" {
205+ return "byte[][]"
206+ }
207+ return "byte[]"
208+
209+ case strings .HasPrefix (stringKind , "int" ) || strings .HasPrefix (stringKind , "uint" ):
210+ parts := regexp .MustCompile ("(u)?int([0-9]*)(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (stringKind )
211+ if len (parts ) != 4 {
212+ return stringKind
213+ }
214+ switch parts [2 ] {
215+ case "8" , "16" , "32" , "64" :
216+ if parts [1 ] == "" {
217+ if parts [3 ] == "" {
218+ return fmt .Sprintf ("int%s" , parts [2 ])
219+ }
220+ return fmt .Sprintf ("int%s[]" , parts [2 ])
221+ }
222+ }
223+ if parts [3 ] == "" {
224+ return fmt .Sprintf ("BigInt" )
225+ }
226+ return fmt .Sprintf ("BigInts" )
227+
228+ case strings .HasPrefix (stringKind , "bool" ):
229+ parts := regexp .MustCompile ("bool(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (stringKind )
230+ if len (parts ) != 2 {
231+ return stringKind
232+ }
233+ if parts [1 ] == "" {
234+ return fmt .Sprintf ("bool" )
235+ }
236+ return fmt .Sprintf ("bool[]" )
237+
238+ case strings .HasPrefix (stringKind , "string" ):
239+ parts := regexp .MustCompile ("string(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (stringKind )
240+ if len (parts ) != 2 {
241+ return stringKind
242+ }
243+ if parts [1 ] == "" {
244+ return fmt .Sprintf ("String" )
245+ }
246+ return fmt .Sprintf ("String[]" )
247+
248+ default :
249+ return stringKind
250+ }
251+ }
252+
253+ // namedType is a set of functions that transform language specific types to
254+ // named versions that my be used inside method names.
255+ var namedType = map [Lang ]func (string , abi.Type ) string {
256+ LangGo : func (string , abi.Type ) string { panic ("this shouldn't be needed" ) },
257+ LangJava : namedTypeJava ,
258+ }
259+
260+ // namedTypeJava converts some primitive data types to named variants that can
261+ // be used as parts of method names.
262+ func namedTypeJava (javaKind string , solKind abi.Type ) string {
263+ switch javaKind {
264+ case "byte[]" :
265+ return "Binary"
266+ case "byte[][]" :
267+ return "Binaries"
268+ case "string" :
269+ return "String"
270+ case "string[]" :
271+ return "Strings"
272+ case "bool" :
273+ return "Bool"
274+ case "bool[]" :
275+ return "Bools"
276+ case "BigInt" :
277+ parts := regexp .MustCompile ("(u)?int([0-9]*)(\\ [[0-9]*\\ ])?" ).FindStringSubmatch (solKind .String ())
278+ if len (parts ) != 4 {
279+ return javaKind
280+ }
281+ switch parts [2 ] {
282+ case "8" , "16" , "32" , "64" :
283+ if parts [3 ] == "" {
284+ return capitalise (fmt .Sprintf ("%sint%s" , parts [1 ], parts [2 ]))
285+ }
286+ return capitalise (fmt .Sprintf ("%sint%ss" , parts [1 ], parts [2 ]))
287+
288+ default :
289+ return javaKind
290+ }
291+ default :
292+ return javaKind
293+ }
294+ }
295+
296+ // methodNormalizer is a name transformer that modifies Solidity method names to
297+ // conform to target language naming concentions.
298+ var methodNormalizer = map [Lang ]func (string ) string {
299+ LangGo : capitalise ,
300+ LangJava : decapitalise ,
301+ }
302+
163303// capitalise makes the first character of a string upper case.
164304func capitalise (input string ) string {
165305 return strings .ToUpper (input [:1 ]) + input [1 :]
166306}
167307
308+ // decapitalise makes the first character of a string lower case.
309+ func decapitalise (input string ) string {
310+ return strings .ToLower (input [:1 ]) + input [1 :]
311+ }
312+
168313// structured checks whether a method has enough information to return a proper
169314// Go struct ot if flat returns are needed.
170315func structured (method abi.Method ) bool {
0 commit comments