Skip to content

Commit c044348

Browse files
committed
move abigenv2 into its own package. separate out common dependencies between v1/v2 into their own package
1 parent 332c188 commit c044348

File tree

8 files changed

+679
-531
lines changed

8 files changed

+679
-531
lines changed

accounts/abi/bind/bind.go

Lines changed: 6 additions & 474 deletions
Large diffs are not rendered by default.

accounts/abi/bind/internal/bind.go

Lines changed: 445 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package internal
2+
3+
import "github.com/ethereum/go-ethereum/accounts/abi"
4+
5+
// tmplData is the data structure required to fill the binding template.
6+
type TmplData struct {
7+
Package string // Name of the package to place the generated file in
8+
Contracts map[string]*TmplContract // List of contracts to generate into this file
9+
Libraries map[string]string // Map the bytecode's link pattern to the library name
10+
Structs map[string]*TmplStruct // Contract struct type definitions
11+
}
12+
13+
// tmplContract contains the data needed to generate an individual contract binding.
14+
type TmplContract struct {
15+
Type string // Type name of the main contract binding
16+
InputABI string // JSON ABI used as the input to generate the binding from
17+
InputBin string // Optional EVM bytecode used to generate deploy code from
18+
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
19+
Constructor abi.Method // Contract constructor for deploy parametrization
20+
Calls map[string]*TmplMethod // Contract calls that only read state data
21+
Transacts map[string]*TmplMethod // Contract calls that write state data
22+
Fallback *TmplMethod // Additional special fallback function
23+
Receive *TmplMethod // Additional special receive function
24+
Events map[string]*TmplEvent // Contract events accessors
25+
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
26+
Library bool // Indicator whether the contract is a library
27+
}
28+
29+
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
30+
// and cached data fields.
31+
type TmplMethod struct {
32+
Original abi.Method // Original method as parsed by the abi package
33+
Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
34+
Structured bool // Whether the returns should be accumulated into a struct
35+
}
36+
37+
// tmplEvent is a wrapper around an abi.Event that contains a few preprocessed
38+
// and cached data fields.
39+
type TmplEvent struct {
40+
Original abi.Event // Original event as parsed by the abi package
41+
Normalized abi.Event // Normalized version of the parsed fields
42+
}
43+
44+
// tmplField is a wrapper around a struct field with binding language
45+
// struct type definition and relative filed name.
46+
type TmplField struct {
47+
Type string // Field type representation depends on target binding language
48+
Name string // Field name converted from the raw user-defined field name
49+
SolKind abi.Type // Raw abi type information
50+
}
51+
52+
// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
53+
// struct name.
54+
type TmplStruct struct {
55+
Name string // Auto-generated struct name(before solidity v0.5.11) or raw name.
56+
Fields []*TmplField // Struct fields definition depends on the binding language.
57+
}

accounts/abi/bind/template.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,8 @@ package bind
1818

1919
import (
2020
_ "embed"
21-
22-
"github.com/ethereum/go-ethereum/accounts/abi"
2321
)
2422

25-
// tmplData is the data structure required to fill the binding template.
26-
type tmplData struct {
27-
Package string // Name of the package to place the generated file in
28-
Contracts map[string]*tmplContract // List of contracts to generate into this file
29-
Libraries map[string]string // Map the bytecode's link pattern to the library name
30-
Structs map[string]*tmplStruct // Contract struct type definitions
31-
}
32-
33-
// tmplContract contains the data needed to generate an individual contract binding.
34-
type tmplContract struct {
35-
Type string // Type name of the main contract binding
36-
InputABI string // JSON ABI used as the input to generate the binding from
37-
InputBin string // Optional EVM bytecode used to generate deploy code from
38-
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
39-
Constructor abi.Method // Contract constructor for deploy parametrization
40-
Calls map[string]*tmplMethod // Contract calls that only read state data
41-
Transacts map[string]*tmplMethod // Contract calls that write state data
42-
Fallback *tmplMethod // Additional special fallback function
43-
Receive *tmplMethod // Additional special receive function
44-
Events map[string]*tmplEvent // Contract events accessors
45-
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
46-
Library bool // Indicator whether the contract is a library
47-
}
48-
49-
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
50-
// and cached data fields.
51-
type tmplMethod struct {
52-
Original abi.Method // Original method as parsed by the abi package
53-
Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
54-
Structured bool // Whether the returns should be accumulated into a struct
55-
}
56-
57-
// tmplEvent is a wrapper around an abi.Event that contains a few preprocessed
58-
// and cached data fields.
59-
type tmplEvent struct {
60-
Original abi.Event // Original event as parsed by the abi package
61-
Normalized abi.Event // Normalized version of the parsed fields
62-
}
63-
64-
// tmplField is a wrapper around a struct field with binding language
65-
// struct type definition and relative filed name.
66-
type tmplField struct {
67-
Type string // Field type representation depends on target binding language
68-
Name string // Field name converted from the raw user-defined field name
69-
SolKind abi.Type // Raw abi type information
70-
}
71-
72-
// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
73-
// struct name.
74-
type tmplStruct struct {
75-
Name string // Auto-generated struct name(before solidity v0.5.11) or raw name.
76-
Fields []*tmplField // Struct fields definition depends on the binding language.
77-
}
78-
7923
// tmplSource is the Go source template that the generated Go contract binding
8024
// is based on.
8125
//

accounts/abi/bind/v2/bind.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package v2
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/ethereum/go-ethereum/accounts/abi"
7+
"github.com/ethereum/go-ethereum/accounts/abi/bind/internal"
8+
"go/format"
9+
"strings"
10+
"text/template"
11+
)
12+
13+
// bindType converts solidity types to Go ones. Since there is no clear mapping
14+
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
15+
// mapped will use an upscaled type (e.g. BigDecimal).
16+
func bindType(kind abi.Type, structs map[string]*internal.TmplStruct) string {
17+
switch kind.T {
18+
case abi.TupleTy:
19+
return structs[kind.TupleRawName+kind.String()].Name
20+
case abi.ArrayTy:
21+
return fmt.Sprintf("[%d]", kind.Size) + bindType(*kind.Elem, structs)
22+
case abi.SliceTy:
23+
return "[]" + bindType(*kind.Elem, structs)
24+
default:
25+
return internal.BindBasicType(kind)
26+
}
27+
}
28+
29+
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
30+
data, err := internal.Bind(types, abis, bytecodes, fsigs, pkg, libs, aliases)
31+
if err != nil {
32+
return "", err
33+
}
34+
for _, c := range data.Contracts {
35+
// We want pack/unpack methods for all existing methods.
36+
for name, t := range c.Transacts {
37+
c.Calls[name] = t
38+
}
39+
c.Transacts = nil
40+
41+
// Make sure we return one argument. If multiple exist
42+
// merge them into a struct.
43+
for _, call := range c.Calls {
44+
if call.Structured {
45+
continue
46+
}
47+
if len(call.Normalized.Outputs) < 2 {
48+
continue
49+
}
50+
// Build up dictionary of existing arg names.
51+
keys := make(map[string]struct{})
52+
for _, o := range call.Normalized.Outputs {
53+
if o.Name != "" {
54+
keys[strings.ToLower(o.Name)] = struct{}{}
55+
}
56+
}
57+
// Assign names to anonymous fields.
58+
for i, o := range call.Normalized.Outputs {
59+
if o.Name != "" {
60+
continue
61+
}
62+
o.Name = internal.Capitalise(abi.ResolveNameConflict("arg", func(name string) bool { _, ok := keys[name]; return ok }))
63+
call.Normalized.Outputs[i] = o
64+
keys[strings.ToLower(o.Name)] = struct{}{}
65+
}
66+
call.Structured = true
67+
}
68+
}
69+
buffer := new(bytes.Buffer)
70+
funcs := map[string]interface{}{
71+
"bindtype": bindType,
72+
"bindtopictype": internal.BindTopicType,
73+
"capitalise": internal.Capitalise,
74+
"decapitalise": internal.Decapitalise,
75+
}
76+
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource))
77+
if err := tmpl.Execute(buffer, data); err != nil {
78+
return "", err
79+
}
80+
// Pass the code through gofmt to clean it up
81+
code, err := format.Source(buffer.Bytes())
82+
if err != nil {
83+
return "", fmt.Errorf("%v\n%s", err, buffer)
84+
}
85+
return string(code), nil
86+
}
File renamed without changes.

accounts/abi/bind/v2/template.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package v2
18+
19+
import (
20+
_ "embed"
21+
22+
"github.com/ethereum/go-ethereum/accounts/abi"
23+
)
24+
25+
// tmplData is the data structure required to fill the binding template.
26+
type tmplData struct {
27+
Package string // Name of the package to place the generated file in
28+
Contracts map[string]*tmplContract // List of contracts to generate into this file
29+
Libraries map[string]string // Map the bytecode's link pattern to the library name
30+
Structs map[string]*tmplStruct // Contract struct type definitions
31+
}
32+
33+
// tmplContract contains the data needed to generate an individual contract binding.
34+
type tmplContract struct {
35+
Type string // Type name of the main contract binding
36+
InputABI string // JSON ABI used as the input to generate the binding from
37+
InputBin string // Optional EVM bytecode used to generate deploy code from
38+
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
39+
Constructor abi.Method // Contract constructor for deploy parametrization
40+
Calls map[string]*tmplMethod // Contract calls that only read state data
41+
Transacts map[string]*tmplMethod // Contract calls that write state data
42+
Fallback *tmplMethod // Additional special fallback function
43+
Receive *tmplMethod // Additional special receive function
44+
Events map[string]*tmplEvent // Contract events accessors
45+
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
46+
Library bool // Indicator whether the contract is a library
47+
}
48+
49+
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
50+
// and cached data fields.
51+
type tmplMethod struct {
52+
Original abi.Method // Original method as parsed by the abi package
53+
Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
54+
Structured bool // Whether the returns should be accumulated into a struct
55+
}
56+
57+
// tmplEvent is a wrapper around an abi.Event that contains a few preprocessed
58+
// and cached data fields.
59+
type tmplEvent struct {
60+
Original abi.Event // Original event as parsed by the abi package
61+
Normalized abi.Event // Normalized version of the parsed fields
62+
}
63+
64+
// tmplField is a wrapper around a struct field with binding language
65+
// struct type definition and relative filed name.
66+
type tmplField struct {
67+
Type string // Field type representation depends on target binding language
68+
Name string // Field name converted from the raw user-defined field name
69+
SolKind abi.Type // Raw abi type information
70+
}
71+
72+
// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
73+
// struct name.
74+
type tmplStruct struct {
75+
Name string // Auto-generated struct name(before solidity v0.5.11) or raw name.
76+
Fields []*tmplField // Struct fields definition depends on the binding language.
77+
}
78+
79+
// tmplSource is the Go source template that the generated Go contract binding
80+
// is based on.
81+
//
82+
//go:embed source.go.tpl
83+
var tmplSource string

cmd/abigen/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
v2 "github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
2223
"io"
2324
"os"
2425
"regexp"
@@ -213,7 +214,7 @@ func abigen(c *cli.Context) error {
213214
err error
214215
)
215216
if c.IsSet(v2Flag.Name) {
216-
code, err = bind.BindV2(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
217+
code, err = v2.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
217218
} else {
218219
code, err = bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
219220
}

0 commit comments

Comments
 (0)