accounts/abi/bind/v2: rename package to "bind" and reexport some v1 stuff

This commit is contained in:
Felix Lange 2025-01-22 12:54:54 +01:00
parent dbe316a48d
commit 0e856b512a
3 changed files with 72 additions and 41 deletions

View file

@ -14,12 +14,12 @@
// 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 v2
package bind
import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
@ -30,14 +30,14 @@ import (
// for new logs, call, transact).
type ContractInstance struct {
Address common.Address
Backend bind.ContractBackend
Backend ContractBackend
abi abi.ABI
}
// FilterEvents returns an EventIterator instance for filtering historical events based on the event id and a block range.
func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
func FilterEvents[T any](instance *ContractInstance, opts *FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
c := bind1.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.FilterLogs(opts, eventName, topics...)
if err != nil {
return nil, err
@ -49,9 +49,9 @@ func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, even
// contract to be intercepted, unpacked, and forwarded to sink. If
// unpack returns an error, the returned subscription is closed with the
// error.
func WatchEvents[T any](instance *ContractInstance, opts *bind.WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
func WatchEvents[T any](instance *ContractInstance, opts *WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
c := bind1.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.WatchLogs(opts, eventName, topics...)
if err != nil {
return nil, err
@ -156,21 +156,21 @@ func (it *EventIterator[T]) Close() error {
// Transact creates and submits a transaction to the bound contract instance
// using the provided abi-encoded input (or nil).
func Transact(instance *ContractInstance, opts *bind.TransactOpts, input []byte) (*types.Transaction, error) {
func Transact(instance *ContractInstance, opts *TransactOpts, input []byte) (*types.Transaction, error) {
var (
addr = instance.Address
backend = instance.Backend
)
c := bind.NewBoundContract(addr, instance.abi, backend, backend, backend)
c := bind1.NewBoundContract(addr, instance.abi, backend, backend, backend)
return c.RawTransact(opts, input)
}
// Call performs an eth_call on the given bound contract instance, using the
// provided abi-encoded input (or nil).
func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []byte, unpack func([]byte) (T, error)) (T, error) {
func Call[T any](instance *ContractInstance, opts *CallOpts, packedInput []byte, unpack func([]byte) (T, error)) (T, error) {
var defaultResult T
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
c := bind1.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
packedOutput, err := c.CallRaw(opts, packedInput)
if err != nil {
return defaultResult, err
@ -185,8 +185,8 @@ func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []
// DeployContractRaw deploys a contract onto the Ethereum blockchain and binds the
// deployment address with a Go wrapper. It expects its parameters to be abi-encoded
// bytes.
func DeployContractRaw(opts *bind.TransactOpts, bytecode []byte, backend bind.ContractBackend, packedParams []byte) (common.Address, *types.Transaction, *bind.BoundContract, error) {
c := bind.NewBoundContract(common.Address{}, abi.ABI{}, backend, backend, backend)
func DeployContractRaw(opts *TransactOpts, bytecode []byte, backend ContractBackend, packedParams []byte) (common.Address, *types.Transaction, *bind1.BoundContract, error) {
c := bind1.NewBoundContract(common.Address{}, abi.ABI{}, backend, backend, backend)
tx, err := c.RawCreationTransact(opts, append(bytecode, packedParams...))
if err != nil {
return common.Address{}, nil, nil, err

View file

@ -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 v2
package bind
import (
"context"
@ -27,12 +27,12 @@ import (
"testing"
"time"
bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/v2/internal/contracts/events"
"github.com/ethereum/go-ethereum/accounts/abi/bind/v2/internal/contracts/nested_libraries"
"github.com/ethereum/go-ethereum/accounts/abi/bind/v2/internal/contracts/solc_errors"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
@ -59,7 +59,7 @@ func JSON(reader io.Reader) (abi.ABI, error) {
return instance, nil
}
func testSetup() (*bind.TransactOpts, *backends.SimulatedBackend, error) {
func testSetup() (*TransactOpts, *backends.SimulatedBackend, error) {
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
backend := simulated.NewBackend(
types.GenesisAlloc{
@ -71,7 +71,7 @@ func testSetup() (*bind.TransactOpts, *backends.SimulatedBackend, error) {
)
signer := types.LatestSigner(params.AllDevChainProtocolChanges)
opts := &bind.TransactOpts{
opts := &TransactOpts{
From: testAddr,
Nonce: nil,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
@ -97,7 +97,7 @@ func testSetup() (*bind.TransactOpts, *backends.SimulatedBackend, error) {
return opts, &bindBackend, nil
}
func makeTestDeployer(auth *bind.TransactOpts, backend bind.ContractBackend) func(input, deployer []byte) (common.Address, *types.Transaction, error) {
func makeTestDeployer(auth *TransactOpts, backend ContractBackend) func(input, deployer []byte) (common.Address, *types.Transaction, error) {
return func(input, deployer []byte) (common.Address, *types.Transaction, error) {
addr, tx, _, err := DeployContractRaw(auth, deployer, backend, input)
return addr, tx, err
@ -119,9 +119,9 @@ func TestDeploymentLibraries(t *testing.T) {
}
constructorInput := ctrct.PackConstructor(big.NewInt(42), big.NewInt(1))
deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, nil)
deploymentParams := bind1.NewDeploymentParams([]*MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, nil)
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
res, err := bind1.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil {
t.Fatalf("err: %+v\n", err)
}
@ -131,7 +131,7 @@ func TestDeploymentLibraries(t *testing.T) {
t.Fatalf("deployment should have generated 5 addresses. got %d", len(res.Addrs))
}
for _, tx := range res.Txs {
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
_, err = bind1.WaitDeployed(context.Background(), bindBackend, tx)
if err != nil {
t.Fatalf("error deploying library: %+v", err)
}
@ -146,7 +146,7 @@ func TestDeploymentLibraries(t *testing.T) {
}
contractAddr := res.Addrs[nested_libraries.C1MetaData.Pattern]
callOpts := &bind.CallOpts{
callOpts := &CallOpts{
From: common.Address{},
Context: context.Background(),
}
@ -174,9 +174,9 @@ func TestDeploymentWithOverrides(t *testing.T) {
defer bindBackend.Backend.Close()
// deploy all the library dependencies of our target contract, but not the target contract itself.
deploymentParams := bind.NewDeploymentParams(nested_libraries.C1MetaData.Deps, nil, nil)
deploymentParams := bind1.NewDeploymentParams(nested_libraries.C1MetaData.Deps, nil, nil)
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
res, err := bind1.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil {
t.Fatalf("err: %+v\n", err)
}
@ -186,7 +186,7 @@ func TestDeploymentWithOverrides(t *testing.T) {
t.Fatalf("deployment should have generated 4 addresses. got %d", len(res.Addrs))
}
for _, tx := range res.Txs {
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
_, err = bind1.WaitDeployed(context.Background(), bindBackend, tx)
if err != nil {
t.Fatalf("error deploying library: %+v", err)
}
@ -199,8 +199,8 @@ func TestDeploymentWithOverrides(t *testing.T) {
constructorInput := ctrct.PackConstructor(big.NewInt(42), big.NewInt(1))
overrides := res.Addrs
// deploy the contract
deploymentParams = bind.NewDeploymentParams([]*bind.MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, overrides)
res, err = bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
deploymentParams = bind1.NewDeploymentParams([]*MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, overrides)
res, err = bind1.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil {
t.Fatalf("err: %+v\n", err)
}
@ -210,7 +210,7 @@ func TestDeploymentWithOverrides(t *testing.T) {
t.Fatalf("deployment should have generated 1 address. got %d", len(res.Addrs))
}
for _, tx := range res.Txs {
_, err = bind.WaitDeployed(context.Background(), bindBackend, tx)
_, err = bind1.WaitDeployed(context.Background(), bindBackend, tx)
if err != nil {
t.Fatalf("error deploying library: %+v", err)
}
@ -231,8 +231,8 @@ func TestDeploymentWithOverrides(t *testing.T) {
t.Fatalf("error getting abi object: %v", err)
}
contractAddr := res.Addrs[nested_libraries.C1MetaData.Pattern]
boundContract := bind.NewBoundContract(contractAddr, *cABI, bindBackend, bindBackend, bindBackend)
callOpts := &bind.CallOpts{
boundContract := bind1.NewBoundContract(contractAddr, *cABI, bindBackend, bindBackend, bindBackend)
callOpts := &CallOpts{
From: common.Address{},
Context: context.Background(),
}
@ -256,14 +256,14 @@ func TestEvents(t *testing.T) {
t.Fatalf("error setting up testing env: %v", err)
}
deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&events.CMetaData}, nil, nil)
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
deploymentParams := bind1.NewDeploymentParams([]*MetaData{&events.CMetaData}, nil, nil)
res, err := bind1.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
if err != nil {
t.Fatalf("error deploying contract for testing: %v", err)
}
backend.Commit()
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[events.CMetaData.Pattern]); err != nil {
if _, err := bind1.WaitDeployed(context.Background(), backend, res.Txs[events.CMetaData.Pattern]); err != nil {
t.Fatalf("WaitDeployed failed %v", err)
}
@ -281,7 +281,7 @@ func TestEvents(t *testing.T) {
newCBasic1Ch := make(chan *events.CBasic1)
newCBasic2Ch := make(chan *events.CBasic2)
watchOpts := &bind.WatchOpts{
watchOpts := &bind1.WatchOpts{
Start: nil,
Context: context.Background(),
}
@ -302,7 +302,7 @@ func TestEvents(t *testing.T) {
t.Fatalf("failed to send transaction: %v", err)
}
backend.Commit()
if _, err := bind.WaitMined(context.Background(), backend, tx); err != nil {
if _, err := bind1.WaitMined(context.Background(), backend, tx); err != nil {
t.Fatalf("error waiting for tx to be mined: %v", err)
}
@ -332,7 +332,7 @@ done:
// now, test that we can filter those same logs after they were included in the chain
filterOpts := &bind.FilterOpts{
filterOpts := &FilterOpts{
Start: 0,
Context: context.Background(),
}
@ -367,21 +367,21 @@ func TestErrors(t *testing.T) {
t.Fatalf("error setting up testing env: %v", err)
}
deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&solc_errors.CMetaData}, nil, nil)
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
deploymentParams := bind1.NewDeploymentParams([]*MetaData{&solc_errors.CMetaData}, nil, nil)
res, err := bind1.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
if err != nil {
t.Fatalf("error deploying contract for testing: %v", err)
}
backend.Commit()
if _, err := bind.WaitDeployed(context.Background(), backend, res.Txs[solc_errors.CMetaData.Pattern]); err != nil {
if _, err := bind1.WaitDeployed(context.Background(), backend, res.Txs[solc_errors.CMetaData.Pattern]); err != nil {
t.Fatalf("WaitDeployed failed %v", err)
}
ctrct, _ := solc_errors.NewC()
var packedInput []byte
opts := &bind.CallOpts{
opts := &CallOpts{
From: res.Addrs[solc_errors.CMetaData.Pattern],
}
packedInput, _ = ctrct.PackFoo()
@ -467,7 +467,7 @@ func TestBindingGeneration(t *testing.T) {
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x
libs[libPattern] = typeName
}
code, err := bind.BindV2(types, abis, bins, dir, libs, make(map[string]string))
code, err := bind1.BindV2(types, abis, bins, dir, libs, make(map[string]string))
if err != nil {
t.Fatalf("error creating bindings for package %s: %v", dir, err)
}

View file

@ -0,0 +1,31 @@
// 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 bind
import bind1 "github.com/ethereum/go-ethereum/accounts/abi/bind"
type ContractBackend = bind1.ContractBackend
type MetaData = bind1.MetaData
type FilterOpts = bind1.FilterOpts
type WatchOpts = bind1.WatchOpts
type TransactOpts = bind1.TransactOpts
type CallOpts = bind1.CallOpts