accounts/abi: More Gary feedback

This commit is contained in:
Guillaume Ballet 2019-07-07 18:27:52 +02:00
parent b7fb49c624
commit 4ee8f14ee0
2 changed files with 17 additions and 2 deletions

View file

@ -51,6 +51,9 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
// Process each individual contract requested binding // Process each individual contract requested binding
contracts := make(map[string]*tmplContract) contracts := make(map[string]*tmplContract)
// Map used to flag each encountered library as such
isLib := make(map[string]struct{})
for i := 0; i < len(types); i++ { for i := 0; i < len(types); i++ {
// Parse the actual ABI to generate the binding for // Parse the actual ABI to generate the binding for
evmABI, err := abi.JSON(strings.NewReader(abis[i])) evmABI, err := abi.JSON(strings.NewReader(abis[i]))
@ -146,6 +149,13 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
Libraries: make(map[string]string), Libraries: make(map[string]string),
Structs: structs, Structs: structs,
} }
// check if that type has already been identified as a library
_, ok := isLib[types[i]]
contracts[types[i]].Library = ok
// function 4-byte signatures are stored in the same sequence
// as types, if available.
if len(fsigs) > i { if len(fsigs) > i {
contracts[types[i]].FuncSigs = fsigs[i] contracts[types[i]].FuncSigs = fsigs[i]
} }
@ -157,6 +167,10 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
} }
if matched { if matched {
contracts[types[i]].Libraries[pattern] = name contracts[types[i]].Libraries[pattern] = name
// keep track that this type is a library
if _, ok := isLib[name]; !ok {
isLib[name] = struct{}{}
}
} }
} }
} }

View file

@ -37,6 +37,7 @@ type tmplContract struct {
Events map[string]*tmplEvent // Contract events accessors Events map[string]*tmplEvent // Contract events accessors
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
Structs map[string]*tmplStruct // Contract struct type definitions Structs map[string]*tmplStruct // Contract struct type definitions
Library bool
} }
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed // tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
@ -509,7 +510,7 @@ import java.util.*;
{{range $contract := .Contracts}} {{range $contract := .Contracts}}
{{$structs := $contract.Structs}} {{$structs := $contract.Structs}}
public class {{.Type}} { {{if not .Library}}public {{end}}class {{.Type}} {
// ABI is the input ABI used to generate the binding from. // ABI is the input ABI used to generate the binding from.
public final static String ABI = "{{.InputABI}}"; public final static String ABI = "{{.InputABI}}";
{{if $contract.FuncSigs}} {{if $contract.FuncSigs}}
@ -536,7 +537,7 @@ public class {{.Type}} {
// "link" contract to dependent libraries by deploying them first. // "link" contract to dependent libraries by deploying them first.
{{range $pattern, $name := .Libraries}} {{range $pattern, $name := .Libraries}}
{{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client); {{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client);
bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address); bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address.getHex().substring(2));
{{end}} {{end}}
{{end}} {{end}}
{{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); {{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}});