Skip to content

account/abi/bind/v2: fix TestDeploymentWithOverrides #32212

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions accounts/abi/bind/v2/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package bind

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -241,3 +242,27 @@ func DefaultDeployer(opts *TransactOpts, backend ContractBackend) DeployFn {
return addr, tx, nil
}
}

// DeployerWithNonceAssignment is basically identical to DefaultDeployer,
// but it additionally tracks the nonce to enable automatic assignment.
//
// This is especially useful when deploying multiple contracts
// from the same address — whether they are independent contracts
// or part of a dependency chain that must be deployed in order.
func DeployerWithNonceAssignment(opts *TransactOpts, backend ContractBackend) DeployFn {
var pendingNonce int64
if opts.Nonce != nil {
pendingNonce = opts.Nonce.Int64()
}
return func(input []byte, deployer []byte) (common.Address, *types.Transaction, error) {
if pendingNonce != 0 {
opts.Nonce = big.NewInt(pendingNonce)
}
addr, tx, err := DeployContract(opts, deployer, backend, input)
if err != nil {
return common.Address{}, nil, err
}
pendingNonce = int64(tx.Nonce() + 1)
return addr, tx, nil
}
}
12 changes: 10 additions & 2 deletions accounts/abi/bind/v2/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func makeTestDeployer(backend simulated.Client) func(input, deployer []byte) (co
return bind.DefaultDeployer(bind.NewKeyedTransactor(testKey, chainId), backend)
}

// makeTestDeployerWithNonceAssignment is similar to makeTestDeployer,
// but it returns a deployer that automatically tracks nonce,
// enabling the deployment of multiple contracts from the same account.
func makeTestDeployerWithNonceAssignment(backend simulated.Client) func(input, deployer []byte) (common.Address, *types.Transaction, error) {
chainId, _ := backend.ChainID(context.Background())
return bind.DeployerWithNonceAssignment(bind.NewKeyedTransactor(testKey, chainId), backend)
}

// test that deploying a contract with library dependencies works,
// verifying by calling method on the deployed contract.
func TestDeploymentLibraries(t *testing.T) {
Expand All @@ -80,7 +88,7 @@ func TestDeploymentLibraries(t *testing.T) {
Contracts: []*bind.MetaData{&nested_libraries.C1MetaData},
Inputs: map[string][]byte{nested_libraries.C1MetaData.ID: constructorInput},
}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(bindBackend.Client))
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployerWithNonceAssignment(bindBackend.Client))
if err != nil {
t.Fatalf("err: %+v\n", err)
}
Expand Down Expand Up @@ -122,7 +130,7 @@ func TestDeploymentWithOverrides(t *testing.T) {
deploymentParams := &bind.DeploymentParams{
Contracts: nested_libraries.C1MetaData.Deps,
}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(bindBackend))
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployerWithNonceAssignment(bindBackend))
if err != nil {
t.Fatalf("err: %+v\n", err)
}
Expand Down
Loading