Skip to content

cmd/abigen, accounts/abi/bind: implement abigen version 2 #26782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 204 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
ed627cc
accounts/abi: abigen v2
s1na Jan 6, 2023
d4ccdc3
add copyright header to lib.go
s1na Mar 1, 2023
6b69f4c
drop unnecessary imports
s1na Mar 1, 2023
7b6f0ea
replace fmt with errors
s1na Mar 1, 2023
ee1263a
drop fmt
s1na Mar 1, 2023
040f808
skip unpack method when no return args
s1na Mar 1, 2023
e2746e7
define contract instance type
s1na Mar 1, 2023
f7084f4
Add deploy code to contract struct
s1na Mar 2, 2023
b504552
Pass pointer to log for unpack
s1na Mar 2, 2023
c14bd3d
clean lang selection leftovers
s1na Mar 2, 2023
135ef69
fix test
s1na Mar 3, 2023
f7fa3fe
add back code that got mistakenly removed in the rebase
jwasinger Oct 31, 2024
cb0d1ea
fix last rebase regression
jwasinger Oct 31, 2024
9c08681
isolate V2 contract interaction API into its own package
jwasinger Nov 4, 2024
0aac198
move v2 contract interaction methods into v2 package. add test
jwasinger Nov 4, 2024
61263dc
stage point before I try to dedup contract interaction api
jwasinger Nov 6, 2024
26491ae
still in a wip state
jwasinger Nov 20, 2024
6d9104e
dependency graph calculation working
jwasinger Nov 20, 2024
cc04aa4
commit of wip-code: rework the contract deployment code to be cleaner
jwasinger Nov 24, 2024
5ba939f
rework contract deployment API
jwasinger Nov 24, 2024
7c95aa1
closer to test passing now...
jwasinger Nov 24, 2024
42d159f
fix contract
jwasinger Nov 24, 2024
41b77f9
update library test contract to have constructor with inputs. remove…
jwasinger Nov 25, 2024
03a6ade
format improvements
jwasinger Nov 25, 2024
b2aec5a
add missing files from testdata. refactor contract deployment API to…
jwasinger Nov 26, 2024
ab97d38
more docs. fix nested library contract. fix associated test case.
jwasinger Nov 26, 2024
0cff488
fix v2 test
jwasinger Nov 26, 2024
0bdf69c
some more docs
jwasinger Nov 26, 2024
b612cab
simplify generated code for list of library dependencies
jwasinger Nov 26, 2024
afe887f
remove old half-written test case with events (I will add it back in …
jwasinger Nov 26, 2024
26641a1
make tests run again
jwasinger Nov 26, 2024
d0a8ead
add copyright notices. move v2 template to its own file. Rename V2B…
jwasinger Nov 26, 2024
77158e5
various small fixes: better error message, remove unecessary comment…
jwasinger Nov 26, 2024
1e8aca9
add test for overrides
jwasinger Nov 27, 2024
cd18d99
add test coverage for events watching
jwasinger Nov 27, 2024
5120718
add coverage for event filtering
jwasinger Nov 27, 2024
f2d78fc
return multiple results in exported struct (instead of anonymous one)…
jwasinger Nov 28, 2024
4fc62e9
add errors to emitted code.
jwasinger Nov 28, 2024
1f25c68
add error unpacking. simplify WatchLogs api.
jwasinger Dec 2, 2024
d3d6926
fix contract filter test. rename *Logs methods to *Events, to reflec…
jwasinger Dec 2, 2024
6b5967a
delete unused test data. remove useless v2 helper methods. update t…
jwasinger Dec 2, 2024
2df68eb
remove unused error test, other unused code in tests
jwasinger Dec 2, 2024
57e1359
small cleanups
jwasinger Dec 2, 2024
69e6f29
add binding generation test
jwasinger Dec 2, 2024
28d5700
add generic log watching/filtering. skip adding generic call/transac…
jwasinger Dec 3, 2024
a048516
add generic call/transact
jwasinger Dec 3, 2024
b50593e
add db example from sina
jwasinger Dec 3, 2024
4872d7f
remove unecessary abi parameter from WatchEvents. simplify event tests
jwasinger Dec 3, 2024
ba68e74
mv test contracts to internal directory under v2
jwasinger Dec 3, 2024
c8b17ca
staging changes for linking tests. rework LinkAndDeploy to decouple …
jwasinger Dec 5, 2024
b06406a
wip
jwasinger Dec 6, 2024
44420cb
basic linking test working
jwasinger Dec 6, 2024
4821fba
add test cases for contract linking that I forgot to push earlier. F…
jwasinger Dec 6, 2024
81289d7
wip: update
jwasinger Dec 6, 2024
463c3b6
fix test cases
jwasinger Dec 9, 2024
f9186b7
one more test
jwasinger Dec 9, 2024
e4c0163
make link test case input be its own struct
jwasinger Dec 9, 2024
60fc16d
rewrite contract link/deploy again
jwasinger Dec 10, 2024
5cc853d
fix linking tests
jwasinger Dec 10, 2024
c293caa
tests working now
jwasinger Dec 10, 2024
e7e1426
remove debug prints. fix contract linking test condition
jwasinger Dec 10, 2024
9a59bdb
generation of custom error unpacking builds (not tested yet)
jwasinger Dec 12, 2024
9552b30
wip (just committing so that I can rebase off master to use ethclient…
jwasinger Dec 12, 2024
736d587
full support for errors with working test
jwasinger Dec 12, 2024
47192a7
change semantics of contract overrides again: any direct/indirect de…
jwasinger Dec 13, 2024
645c844
clean up link/deploy code. add docs
jwasinger Dec 13, 2024
709ff38
gofmt files. add a TODO comment and remove another useless comment
jwasinger Dec 13, 2024
7000583
fix regex for solc pattern matching
jwasinger Dec 13, 2024
2421cc3
move contract dep tree, link-and-deploy to bind package. This is so …
jwasinger Dec 15, 2024
780dd76
add some docs
jwasinger Dec 15, 2024
89fcfac
move abigen2 specific bind code into BindV2. fix broken v1 tests
jwasinger Dec 16, 2024
3129593
export event names in bindings. remove added APIs Filter/Watch-Logs …
jwasinger Dec 16, 2024
d93a152
update dep tree
jwasinger Dec 16, 2024
fce1885
remove TODO comment about adding a test case that I have added
jwasinger Dec 16, 2024
2c9c9cf
regenerate bindings
jwasinger Dec 16, 2024
02ff41a
add comment for tmplError struct and remove unused v2 backend interface
jwasinger Dec 16, 2024
bbddf0d
remove field that I added to abi.Error and never used
jwasinger Dec 16, 2024
5da8d5a
wip...
jwasinger Dec 17, 2024
b6d6ba9
refactor builds
jwasinger Dec 17, 2024
b1f3970
tests pass
jwasinger Dec 18, 2024
adc4ab6
remove unecessarily-added method
jwasinger Dec 18, 2024
26d0d78
add docs
jwasinger Dec 18, 2024
484c723
move BindV2 and methods it depends on into their own file.
jwasinger Dec 18, 2024
f90a88e
make DeploymentParams fields private. instantiate it with a construc…
jwasinger Dec 18, 2024
915c7e8
fix comment for bindError method
jwasinger Dec 18, 2024
4f7949f
Update accounts/abi/bind/dep_tree.go
jwasinger Dec 18, 2024
de00bb3
Update accounts/abi/bind/dep_tree.go
jwasinger Dec 18, 2024
c89c24c
Update accounts/abi/bind/dep_tree.go
jwasinger Dec 18, 2024
eb10c47
wip: embed library relations in metadata
jwasinger Dec 18, 2024
2949b1a
embed direct library relations in metadata in bindings. simplify dep…
jwasinger Dec 19, 2024
3436fb9
accounts/abi/bind: simplifications
holiman Dec 19, 2024
b4f1a3c
clarifications
holiman Dec 19, 2024
7f30122
accounts/abi/bind: make link test case failure output the index of t…
jwasinger Dec 19, 2024
6afe90d
accounts/abi/bind: don't define link test case table as a package-pri…
jwasinger Dec 19, 2024
5eb7271
accounts/abi/bind: remove unused fields from template structs
jwasinger Dec 19, 2024
6a007ca
wip
jwasinger Dec 20, 2024
ec0b3e5
fix events/error args normalization capitalization. add unit test th…
jwasinger Dec 23, 2024
ddc897e
simplify vars naming in normalizeArgs
jwasinger Dec 23, 2024
f4acb0c
remove capitalize helper method. simplify call/event/error symbol na…
jwasinger Dec 23, 2024
708dc59
copy DeploymentParams overrides before creating tree deployer (preven…
jwasinger Dec 23, 2024
e50bfdd
remove error return from generated pack method for constructors. add…
jwasinger Dec 23, 2024
fe2827e
accounts/abi/bind/v2: fix lib_test.go (update test to reflect that bi…
jwasinger Dec 30, 2024
0953522
accounts/abi/bind: remove duplicate convertedv1bindtests
jwasinger Dec 30, 2024
0b09319
accounts/abi/bind/v2/internal/contracts: regenerate bindings (they w…
jwasinger Dec 30, 2024
41a0cff
accounts/abi/bind: deduplicate logic to normalize struct elements, ev…
jwasinger Jan 6, 2025
3d974b2
accounts/abi/bind/v2/internal/contracts: update bindings
jwasinger Jan 6, 2025
8ea9929
accounts/abi/bind: fix some ugliness in the generated binding code wh…
jwasinger Jan 6, 2025
64c13eb
update converted v1 bind test cases. rename test case that checks co…
jwasinger Jan 6, 2025
a934729
remove useless todo in v2 template
jwasinger Jan 6, 2025
9b453b0
better naming for instance of self-struct in member methods of struct…
jwasinger Jan 6, 2025
b896ed2
accounts/abi/bind, accounts/abi/bind/v2: move DeployContractRaw into …
jwasinger Jan 6, 2025
f703442
accounts/abi/bind: simplify bindv2: create constructors for contrac…
jwasinger Jan 6, 2025
1dd531a
misc template additions I forgot to include in previous commits. int…
jwasinger Jan 7, 2025
bcc305d
remove unused comment. add method description for RawCreationTransact
jwasinger Jan 7, 2025
9dd006f
update converted v1 bindings
jwasinger Jan 7, 2025
c7ff648
reenable TestBindingV2ConvertedV1Tests
jwasinger Jan 7, 2025
fb26a58
address some recent feedback. add docs about what the binder and con…
jwasinger Jan 7, 2025
c92220f
add docs explaining internal variables on the v2 binder/contractBinde…
jwasinger Jan 8, 2025
1b7aece
don't emit constructor input packing method if the contract construct…
jwasinger Jan 8, 2025
4d2706c
add TODO note about cleaning up template code for return values
jwasinger Jan 8, 2025
8c20a2c
accounts/abi/bind/v2: fix Call helper method: don't return a pointe…
jwasinger Jan 8, 2025
5ae1de4
accounts/abi/bind: remove unecessary TODO from v2 template
jwasinger Jan 9, 2025
0a751f8
accounts/abi/bind/v2: fix test that doesn't compile
jwasinger Jan 9, 2025
2ab719c
accounts/abi/bind/v2: remove unecessary TODOs. Fix TestErrors compil…
jwasinger Jan 9, 2025
8d3fc41
accounts/abi/bind, accounts/abi/bind/v2: fix error unpacking. updat…
jwasinger Jan 9, 2025
bda5e4c
fix test 'BindingV2ConvertedV1Tests'. add more comprehensive output …
jwasinger Jan 10, 2025
45d51ce
accounts/abi/bind: un-embed+ make test nicer
holiman Jan 10, 2025
dbe316a
accounts/abi/bind/v2: remove some unnecessary type parameters
fjl Jan 22, 2025
0e856b5
accounts/abi/bind/v2: rename package to "bind" and reexport some v1 s…
fjl Jan 22, 2025
0775568
accounts/abi/bind/v2: add ContractInstance constructor
fjl Jan 22, 2025
46e7e44
accounts/abi/bind: rename field in MetaData
fjl Jan 22, 2025
1bdc820
accounts/abi/bind/v2: pass contract instance as value
fjl Jan 22, 2025
d330916
accounts/abi/bind: update v2 template
fjl Jan 22, 2025
1602eb8
accounts/abi/bind/v2: update generated bindings for test
fjl Jan 22, 2025
56502a9
accounts/abi/bind/v2: support passing nil unpack function in Call
fjl Jan 22, 2025
5598edf
accounts/abi/bind/v2: update tests
fjl Jan 22, 2025
92b1f74
accounts/abi/bind: update converted v1 bind tests
fjl Jan 22, 2025
d6316e0
accounts/abi/bind: remove third return value of DeployContractRaw
fjl Jan 22, 2025
faabbf7
accounts/abi/bind: fix warning about assignment to blank
fjl Jan 22, 2025
25481e0
accounts/abi/bind: fix some more warnings
fjl Jan 22, 2025
169a424
accounts/abi/bind: move code generator to accounts/abi/abigen
fjl Jan 22, 2025
4e43eec
accounts/abi/bind: add missing function
fjl Jan 22, 2025
ead086b
accounts/abi/bind: move implementation to v2 package
fjl Jan 22, 2025
593a97f
accounts/abi/bind/v2: remove deprecated TransactOpts constructors in v2
fjl Jan 22, 2025
d532301
accounts/abi/bind: fixup
fjl Jan 22, 2025
dbe60f0
accounts/abi/abigen: remove error in v2 Pack function
fjl Jan 23, 2025
1be2215
accounts/abi/abigen: use tabs for indent in v2 template
fjl Jan 23, 2025
1013c1c
accounts/abi/abigen: add panic when encoding constructor input fails
fjl Jan 23, 2025
92f6ec1
accounts/abi/abigen: update test bindings
fjl Jan 23, 2025
888bbcc
accounts/abi/bind/v2: fix test
fjl Jan 23, 2025
5387d49
accounts/abi/bind/v2: rename GetAbi -> ParseABI
fjl Jan 27, 2025
c9da576
accounts/abi/bind/v2: change interface{} -> any
fjl Jan 27, 2025
7c5c0bb
accounts/abi/bind/v2: remove ContractInstance
fjl Jan 27, 2025
f937417
accounts/abi/abigen: change for removal of ContractInstance
fjl Jan 27, 2025
8200bbf
accounts/abi/bind/v2: provide only one version of WaitMined, WaitDepl…
fjl Jan 27, 2025
d3dfbf9
accounts/abi/bind/v2: export DeploymentParams fields
fjl Jan 27, 2025
d5481a6
accounts/abi/bind/v2: document MetaData fields and remove Sigs
fjl Jan 27, 2025
176a4b3
accounts/abi/bind/v2: add Backend type
fjl Jan 27, 2025
fd3bb35
accounts/abi/bind: make MetaData independent of v2
fjl Jan 27, 2025
c452fb1
accounts/abi/abigen: create ContractEventName method
fjl Jan 27, 2025
47784b9
accounts/abi/bind/v2: infer event name from type
fjl Jan 27, 2025
a4480b2
accounts/abi/bind/v2: remove instances copying of MetaData in contrac…
jwasinger Jan 29, 2025
07ab96e
accounts/abi/abigen: add binding generation test that mirrors the --a…
jwasinger Jan 30, 2025
6d3e3b2
accounts/abi/abigen/testdata/v2: update converted v1 test binding (t…
jwasinger Jan 30, 2025
20bf7bd
only populate Pattern field on binding metdata if the bindings were b…
jwasinger Jan 30, 2025
ddb9390
update generated bindings
jwasinger Jan 31, 2025
d43e375
accounts/abi: rename contract MetaData field 'Pattern' to 'Id'. Thi…
jwasinger Feb 3, 2025
7fc8c6d
update generated bindings for tests
jwasinger Feb 3, 2025
3eb1f8a
accounts/abi: rename MetaData field 'Id' to 'ID'
jwasinger Feb 3, 2025
741ed92
accounts/abi/abigen/testdata/v2,accounts/abi/bind/v2/internal/contrac…
jwasinger Feb 3, 2025
028ef2f
accounts/abi/bind/v2: add separate BoundContract struct for v2. Prev…
jwasinger Feb 4, 2025
6c70418
accounts/abi/bind/v2: improvement on the previous commit: remove all …
jwasinger Feb 4, 2025
82886ff
update tests
jwasinger Feb 4, 2025
3a96aaa
fix lint
jwasinger Feb 4, 2025
cf0d15d
remove redundant method
jwasinger Feb 4, 2025
4841649
accounts/abi: regenerate test data
jwasinger Feb 4, 2025
14f84d3
accounts/abi/abigend: ensure that the bindings produced for a single …
jwasinger Feb 5, 2025
c9cab4d
update tests
jwasinger Feb 5, 2025
e8259bf
cmd/abigen: for '--alias' flag descriptor, mention that errors are al…
jwasinger Feb 5, 2025
6680b83
accounts/abi/bind/v2: modify v2 DeployContract to take packed input t…
jwasinger Feb 5, 2025
8a4a5a6
accounts/abi/abigen, account/abi/bind/v2: polish code
rjl493456442 Feb 6, 2025
0df5b17
accounts/abi/bind/v2: simplify DefaultDeployer func signature
jwasinger Feb 6, 2025
de5fb74
revert 6c70418313b56f96522d2dcf56f21931ce71a13d, 028ef2f263f484b750e0…
jwasinger Feb 6, 2025
a3b95f0
update tests
jwasinger Feb 6, 2025
2241fbb
accounts/abi/bind/v2: add Transact utility method for v2. There is a…
jwasinger Feb 6, 2025
9ab62d6
accounts/abi/abigen: improve v2 template
jwasinger Feb 7, 2025
868e3c6
accounts/abi/abigen: fix generation of event struct fields in bindings
jwasinger Feb 10, 2025
6868cd4
abigen: replace space-prefixed lines with tabs
jwasinger Feb 10, 2025
c88e387
accounts/abi: attempt to improve documentation throughout the PR. Ma…
jwasinger Feb 11, 2025
9d5003c
accounts/abi: regenerate test bindings
jwasinger Feb 11, 2025
9d7001c
accounts/abi/bind/v2: clean up lib.go:
jwasinger Feb 12, 2025
a9b2384
accounts/abi/bind/v2: for v2 utility API: restrict func doc comments…
jwasinger Feb 12, 2025
9eddba4
accounts/abi/abigen: remove unused template helper function
jwasinger Feb 17, 2025
ab6643b
cmd/abigen: remove regression: ensure that either --abi or --combined…
jwasinger Feb 18, 2025
e97cef6
accounts/abi/abigen: simplify calculation whether or not a method's o…
jwasinger Feb 18, 2025
8b506f5
accounts/abi/abigen: remove duplicated method normalization logic
jwasinger Feb 18, 2025
c5fa9c4
accounts/abi: don't capitalise argument names in pack functions. nor…
jwasinger Feb 18, 2025
9b7dfc4
accounts/abi/bind/v2/internal/contracts/db: revert unintended change …
jwasinger Feb 18, 2025
24dbfc8
accounts/abi/bind/v2/internal/contracts: recompile all contracts to u…
jwasinger Mar 7, 2025
829c3cb
accounts/abi/bind/v2: simplify EventIterator.Next interface: don't r…
jwasinger Mar 7, 2025
b33c6c9
accounts/abi/bind/v2: add package-level documentation summarizing the…
jwasinger Mar 8, 2025
9f5d77b
accounts/abi/bind/v2: clarify that v2 utility methods are intended to…
jwasinger Mar 8, 2025
e9d795d
Update lib.go
fjl Mar 8, 2025
14ab4d5
Update lib.go
fjl Mar 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 51 additions & 90 deletions accounts/abi/bind/bind.go → accounts/abi/abigen/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package bind generates Ethereum contract Go bindings.
// Package abigen generates Ethereum contract Go bindings.
//
// Detailed usage document and tutorial available on the go-ethereum Wiki page:
// https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts
package bind
// https://geth.ethereum.org/docs/developers/dapp-developer/native-bindings
package abigen

import (
"bytes"
Expand All @@ -33,13 +33,6 @@ import (
"github.com/ethereum/go-ethereum/log"
)

// Lang is a target programming language selector to generate bindings for.
type Lang int

const (
LangGo Lang = iota
)

func isKeyWord(arg string) bool {
switch arg {
case "break":
Expand Down Expand Up @@ -81,7 +74,7 @@ func isKeyWord(arg string) bool {
// to be used as is in client code, but rather as an intermediate struct which
// enforces compile time type safety and naming convention as opposed to having to
// manually maintain hard coded strings that break on runtime.
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) {
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
var (
// contracts is the map of each individual contract requested binding
contracts = make(map[string]*tmplContract)
Expand Down Expand Up @@ -125,14 +118,14 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]

for _, input := range evmABI.Constructor.Inputs {
if hasStruct(input.Type) {
bindStructType[lang](input.Type, structs)
bindStructType(input.Type, structs)
}
}

for _, original := range evmABI.Methods {
// Normalize the method for capital cases and non-anonymous inputs/outputs
normalized := original
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
normalizedName := abi.ToCamelCase(alias(aliases, original.Name))
// Ensure there is no duplicated identifier
var identifiers = callIdentifiers
if !original.IsConstant() {
Expand All @@ -159,17 +152,17 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j)
}
if hasStruct(input.Type) {
bindStructType[lang](input.Type, structs)
bindStructType(input.Type, structs)
}
}
normalized.Outputs = make([]abi.Argument, len(original.Outputs))
copy(normalized.Outputs, original.Outputs)
for j, output := range normalized.Outputs {
if output.Name != "" {
normalized.Outputs[j].Name = capitalise(output.Name)
normalized.Outputs[j].Name = abi.ToCamelCase(output.Name)
}
if hasStruct(output.Type) {
bindStructType[lang](output.Type, structs)
bindStructType(output.Type, structs)
}
}
// Append the methods to the call or transact lists
Expand All @@ -188,7 +181,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
normalized := original

// Ensure there is no duplicated identifier
normalizedName := methodNormalizer[lang](alias(aliases, original.Name))
normalizedName := abi.ToCamelCase(alias(aliases, original.Name))
// Name shouldn't start with a digit. It will make the generated code invalid.
if len(normalizedName) > 0 && unicode.IsDigit(rune(normalizedName[0])) {
normalizedName = fmt.Sprintf("E%s", normalizedName)
Expand All @@ -213,14 +206,14 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
// Event is a bit special, we need to define event struct in binding,
// ensure there is no camel-case-style name conflict.
for index := 0; ; index++ {
if !used[capitalise(normalized.Inputs[j].Name)] {
used[capitalise(normalized.Inputs[j].Name)] = true
if !used[abi.ToCamelCase(normalized.Inputs[j].Name)] {
used[abi.ToCamelCase(normalized.Inputs[j].Name)] = true
break
}
normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index)
}
if hasStruct(input.Type) {
bindStructType[lang](input.Type, structs)
bindStructType(input.Type, structs)
}
}
// Append the event to the accumulator list
Expand All @@ -233,8 +226,9 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
if evmABI.HasReceive() {
receive = &tmplMethod{Original: evmABI.Receive}
}

contracts[types[i]] = &tmplContract{
Type: capitalise(types[i]),
Type: abi.ToCamelCase(types[i]),
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""),
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"),
Constructor: evmABI.Constructor,
Expand All @@ -245,6 +239,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
Events: events,
Libraries: make(map[string]string),
}

// Function 4-byte signatures are stored in the same sequence
// as types, if available.
if len(fsigs) > i {
Expand All @@ -270,6 +265,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
_, ok := isLib[types[i]]
contracts[types[i]].Library = ok
}

// Generate the contract template data content and render it
data := &tmplData{
Package: pkg,
Expand All @@ -280,36 +276,25 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]
buffer := new(bytes.Buffer)

funcs := map[string]interface{}{
"bindtype": bindType[lang],
"bindtopictype": bindTopicType[lang],
"namedtype": namedType[lang],
"capitalise": capitalise,
"bindtype": bindType,
"bindtopictype": bindTopicType,
"capitalise": abi.ToCamelCase,
"decapitalise": decapitalise,
}
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang]))
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource))
if err := tmpl.Execute(buffer, data); err != nil {
return "", err
}
// For Go bindings pass the code through gofmt to clean it up
if lang == LangGo {
code, err := format.Source(buffer.Bytes())
if err != nil {
return "", fmt.Errorf("%v\n%s", err, buffer)
}
return string(code), nil
// Pass the code through gofmt to clean it up
code, err := format.Source(buffer.Bytes())
if err != nil {
return "", fmt.Errorf("%v\n%s", err, buffer)
}
// For all others just return as is for now
return buffer.String(), nil
}

// bindType is a set of type binders that convert Solidity types to some supported
// programming language types.
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
LangGo: bindTypeGo,
return string(code), nil
}

// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
func bindBasicTypeGo(kind abi.Type) string {
// bindBasicType converts basic solidity types(except array, slice and tuple) to Go ones.
func bindBasicType(kind abi.Type) string {
switch kind.T {
case abi.AddressTy:
return "common.Address"
Expand All @@ -332,32 +317,26 @@ func bindBasicTypeGo(kind abi.Type) string {
}
}

// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
// bindType converts solidity types to Go ones. Since there is no clear mapping
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
// mapped will use an upscaled type (e.g. BigDecimal).
func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
func bindType(kind abi.Type, structs map[string]*tmplStruct) string {
switch kind.T {
case abi.TupleTy:
return structs[kind.TupleRawName+kind.String()].Name
case abi.ArrayTy:
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs)
return fmt.Sprintf("[%d]", kind.Size) + bindType(*kind.Elem, structs)
case abi.SliceTy:
return "[]" + bindTypeGo(*kind.Elem, structs)
return "[]" + bindType(*kind.Elem, structs)
default:
return bindBasicTypeGo(kind)
return bindBasicType(kind)
}
}

// bindTopicType is a set of type binders that convert Solidity types to some
// supported programming language topic types.
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
LangGo: bindTopicTypeGo,
}

// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
// bindTopicType converts a Solidity topic type to a Go one. It is almost the same
// functionality as for simple types, but dynamic types get converted to hashes.
func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
bound := bindTypeGo(kind, structs)
func bindTopicType(kind abi.Type, structs map[string]*tmplStruct) string {
bound := bindType(kind, structs)

// todo(rjl493456442) according solidity documentation, indexed event
// parameters that are not value types i.e. arrays and structs are not
Expand All @@ -371,16 +350,10 @@ func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
return bound
}

// bindStructType is a set of type binders that convert Solidity tuple types to some supported
// programming language struct definition.
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{
LangGo: bindStructTypeGo,
}

// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
// in the given map.
// Notably, this function will resolve and record nested struct recursively.
func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
// bindStructType converts a Solidity tuple type to a Go one and records the mapping
// in the given map. Notably, this function will resolve and record nested struct
// recursively.
func bindStructType(kind abi.Type, structs map[string]*tmplStruct) string {
switch kind.T {
case abi.TupleTy:
// We compose a raw struct name and a canonical parameter expression
Expand All @@ -398,37 +371,35 @@ func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string {
fields []*tmplField
)
for i, elem := range kind.TupleElems {
name := capitalise(kind.TupleRawNames[i])
name := abi.ToCamelCase(kind.TupleRawNames[i])
name = abi.ResolveNameConflict(name, func(s string) bool { return names[s] })
names[name] = true
fields = append(fields, &tmplField{Type: bindStructTypeGo(*elem, structs), Name: name, SolKind: *elem})
fields = append(fields, &tmplField{
Type: bindStructType(*elem, structs),
Name: name,
SolKind: *elem,
})
}
name := kind.TupleRawName
if name == "" {
name = fmt.Sprintf("Struct%d", len(structs))
}
name = capitalise(name)
name = abi.ToCamelCase(name)

structs[id] = &tmplStruct{
Name: name,
Fields: fields,
}
return name
case abi.ArrayTy:
return fmt.Sprintf("[%d]", kind.Size) + bindStructTypeGo(*kind.Elem, structs)
return fmt.Sprintf("[%d]", kind.Size) + bindStructType(*kind.Elem, structs)
case abi.SliceTy:
return "[]" + bindStructTypeGo(*kind.Elem, structs)
return "[]" + bindStructType(*kind.Elem, structs)
default:
return bindBasicTypeGo(kind)
return bindBasicType(kind)
}
}

// namedType is a set of functions that transform language specific types to
// named versions that may be used inside method names.
var namedType = map[Lang]func(string, abi.Type) string{
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") },
}

// alias returns an alias of the given string based on the aliasing rules
// or returns itself if no rule is matched.
func alias(aliases map[string]string, n string) string {
Expand All @@ -438,21 +409,11 @@ func alias(aliases map[string]string, n string) string {
return n
}

// methodNormalizer is a name transformer that modifies Solidity method names to
// conform to target language naming conventions.
var methodNormalizer = map[Lang]func(string) string{
LangGo: abi.ToCamelCase,
}

// capitalise makes a camel-case string which starts with an upper case character.
var capitalise = abi.ToCamelCase

// decapitalise makes a camel-case string which starts with a lower case character.
func decapitalise(input string) string {
if len(input) == 0 {
return input
}

goForm := abi.ToCamelCase(input)
return strings.ToLower(goForm[:1]) + goForm[1:]
}
Expand All @@ -471,7 +432,7 @@ func structured(args abi.Arguments) bool {
}
// If the field name is empty when normalized or collides (var, Var, _var, _Var),
// we can't organize into a struct
field := capitalise(out.Name)
field := abi.ToCamelCase(out.Name)
if field == "" || exists[field] {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package bind
package abigen

import (
"fmt"
Expand Down Expand Up @@ -2072,20 +2072,22 @@ var bindTests = []struct {

// Tests that packages generated by the binder can be successfully compiled and
// the requested tester run against it.
func TestGolangBindings(t *testing.T) {
func TestBindings(t *testing.T) {
t.Parallel()
// Skip the test if no Go command can be found
gocmd := runtime.GOROOT() + "/bin/go"
if !common.FileExist(gocmd) {
t.Skip("go sdk not found for testing")
}
// Create a temporary workspace for the test suite
ws := t.TempDir()

// Create a temporary workspace for the test suite
ws, _ := os.MkdirTemp("", "abigen")
pkg := filepath.Join(ws, "bindtest")
if err := os.MkdirAll(pkg, 0700); err != nil {
t.Fatalf("failed to create package: %v", err)
}
t.Log("tmpdir", pkg)

// Generate the test suite for all the contracts
for i, tt := range bindTests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -2096,7 +2098,7 @@ func TestGolangBindings(t *testing.T) {
types = []string{tt.name}
}
// Generate the binding and create a Go source file in the workspace
bind, err := Bind(types, tt.abi, tt.bytecode, tt.fsigs, "bindtest", LangGo, tt.libs, tt.aliases)
bind, err := Bind(types, tt.abi, tt.bytecode, tt.fsigs, "bindtest", tt.libs, tt.aliases)
if err != nil {
t.Fatalf("test %d: failed to generate binding: %v", i, err)
}
Expand Down
Loading