accounts/abi/bind: move code generator to accounts/abi/abigen

This commit is contained in:
Felix Lange 2025-01-22 19:58:02 +01:00
parent 25481e04bb
commit 169a42448c
31 changed files with 60 additions and 18 deletions

View file

@ -14,11 +14,11 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // 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: // 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 // https://geth.ethereum.org/docs/developers/dapp-developer/native-bindings
package bind package abigen
import ( import (
"bytes" "bytes"

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package bind package abigen
import ( import (
"fmt" "fmt"
@ -2079,13 +2079,15 @@ func TestBindings(t *testing.T) {
if !common.FileExist(gocmd) { if !common.FileExist(gocmd) {
t.Skip("go sdk not found for testing") 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") pkg := filepath.Join(ws, "bindtest")
if err := os.MkdirAll(pkg, 0700); err != nil { if err := os.MkdirAll(pkg, 0700); err != nil {
t.Fatalf("failed to create package: %v", err) t.Fatalf("failed to create package: %v", err)
} }
t.Log("tmpdir", pkg)
// Generate the test suite for all the contracts // Generate the test suite for all the contracts
for i, tt := range bindTests { for i, tt := range bindTests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View file

@ -1,4 +1,20 @@
package bind // Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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 abigen
import ( import (
"bytes" "bytes"

View file

@ -1,7 +1,22 @@
package bind // Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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 abigen
import ( import (
_ "embed"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -249,7 +264,7 @@ var bindTests2 = []bindV2Test{
// that no mutations occurred compared to the expected output included under internal/convertedv1bindtests. // that no mutations occurred compared to the expected output included under internal/convertedv1bindtests.
func TestBindingV2ConvertedV1Tests(t *testing.T) { func TestBindingV2ConvertedV1Tests(t *testing.T) {
for _, tc := range bindTests2 { for _, tc := range bindTests2 {
fname := fmt.Sprintf("v2/internal/convertedv1bindtests/%v.go", strings.ToLower(tc.name)) fname := fmt.Sprintf("testdata/v2/%v.go.txt", strings.ToLower(tc.name))
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
if tc.types == nil { if tc.types == nil {
tc.types = []string{tc.name} tc.types = []string{tc.name}

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package bind package abigen
import ( import (
_ "embed" _ "embed"

View file

@ -22,11 +22,18 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/abigen"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// Bind generates a v1 contract binding.
// Deprecated: binding generation has moved to github.com/ethereum/go-ethereum/accounts/abi/abigen
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
return abigen.Bind(types, abis, bytecodes, fsigs, pkg, libs, aliases)
}
// WaitMined waits for tx to be mined on the blockchain. // WaitMined waits for tx to be mined on the blockchain.
// It stops waiting when the context is canceled. // It stops waiting when the context is canceled.
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {

View file

@ -23,12 +23,14 @@ import (
"strings" "strings"
"testing" "testing"
bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/abigen"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common/compiler" "github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
// Run go generate to recreate the test bindings.
//
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/db/combined-abi.json -type DBStats -pkg db -out internal/contracts/db/bindings.go //go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/db/combined-abi.json -type DBStats -pkg db -out internal/contracts/db/bindings.go
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/events/combined-abi.json -type C -pkg events -out internal/contracts/events/bindings.go //go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/events/combined-abi.json -type C -pkg events -out internal/contracts/events/bindings.go
//go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/nested_libraries/combined-abi.json -type C1 -pkg nested_libraries -out internal/contracts/nested_libraries/bindings.go //go:generate go run github.com/ethereum/go-ethereum/cmd/abigen -v2 -combined-json internal/contracts/nested_libraries/combined-abi.json -type C1 -pkg nested_libraries -out internal/contracts/nested_libraries/bindings.go
@ -84,7 +86,7 @@ func TestBindingGeneration(t *testing.T) {
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x
libs[libPattern] = typeName libs[libPattern] = typeName
} }
code, err := bind1.BindV2(types, abis, bins, dir, libs, make(map[string]string)) code, err := abigen.BindV2(types, abis, bins, dir, libs, make(map[string]string))
if err != nil { if err != nil {
t.Fatalf("error creating bindings for package %s: %v", dir, err) t.Fatalf("error creating bindings for package %s: %v", dir, err)
} }

View file

@ -24,7 +24,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/abigen"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common/compiler" "github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -88,10 +88,10 @@ func init() {
aliasFlag, aliasFlag,
v2Flag, v2Flag,
} }
app.Action = abigen app.Action = generate
} }
func abigen(c *cli.Context) error { func generate(c *cli.Context) error {
utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected.
if c.String(pkgFlag.Name) == "" { if c.String(pkgFlag.Name) == "" {
@ -213,9 +213,9 @@ func abigen(c *cli.Context) error {
err error err error
) )
if c.IsSet(v2Flag.Name) { if c.IsSet(v2Flag.Name) {
code, err = bind.BindV2(types, abis, bins, c.String(pkgFlag.Name), libs, aliases) code, err = abigen.BindV2(types, abis, bins, c.String(pkgFlag.Name), libs, aliases)
} else { } else {
code, err = bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases) code, err = abigen.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), libs, aliases)
} }
if err != nil { if err != nil {
utils.Fatalf("Failed to generate ABI binding: %v", err) utils.Fatalf("Failed to generate ABI binding: %v", err)