accounts/abi/bind/v2: export DeploymentParams fields

The constructor function doesn't make sense. The struct has one required and two optional
fields, and we may want to add more configuration later. With the constructor function,
all parameters always need to be given, even the optional ones. And we can't extend the
function without breaking the API. So it's better to use require users to write the
literal.
This commit is contained in:
Felix Lange 2025-01-28 00:16:42 +01:00
parent 8200bbf4a0
commit d3dfbf9bfa
3 changed files with 29 additions and 24 deletions

View file

@ -14,22 +14,15 @@ import (
// set of contracts. It takes an optional override // set of contracts. It takes an optional override
// list to specify contracts/libraries that have already been deployed on-chain. // list to specify contracts/libraries that have already been deployed on-chain.
type DeploymentParams struct { type DeploymentParams struct {
contracts []*MetaData Contracts []*MetaData
// map of solidity library pattern to constructor input.
inputs map[string][]byte // Map of solidity library pattern to constructor input.
Inputs map[string][]byte
// Overrides is an optional map of pattern to deployment address. // Overrides is an optional map of pattern to deployment address.
// Contracts/libraries that refer to dependencies in the override // Contracts/libraries that refer to dependencies in the override
// set are linked to the provided address (an already-deployed contract). // set are linked to the provided address (an already-deployed contract).
overrides map[string]common.Address Overrides map[string]common.Address
}
// NewDeploymentParams instantiates a DeploymentParams instance.
func NewDeploymentParams(contracts []*MetaData, inputs map[string][]byte, overrides map[string]common.Address) *DeploymentParams {
return &DeploymentParams{
contracts,
inputs,
overrides,
}
} }
// DeploymentResult encapsulates information about the result of the deployment // DeploymentResult encapsulates information about the result of the deployment
@ -38,6 +31,7 @@ func NewDeploymentParams(contracts []*MetaData, inputs map[string][]byte, overri
type DeploymentResult struct { type DeploymentResult struct {
// map of contract library pattern -> deploy transaction // map of contract library pattern -> deploy transaction
Txs map[string]*types.Transaction Txs map[string]*types.Transaction
// map of contract library pattern -> deployed address // map of contract library pattern -> deployed address
Addrs map[string]common.Address Addrs map[string]common.Address
} }
@ -56,11 +50,11 @@ type depTreeDeployer struct {
} }
func newDepTreeDeployer(deployParams *DeploymentParams, deployFn DeployFn) *depTreeDeployer { func newDepTreeDeployer(deployParams *DeploymentParams, deployFn DeployFn) *depTreeDeployer {
deployedAddrs := maps.Clone(deployParams.overrides) deployedAddrs := maps.Clone(deployParams.Overrides)
if deployedAddrs == nil { if deployedAddrs == nil {
deployedAddrs = make(map[string]common.Address) deployedAddrs = make(map[string]common.Address)
} }
inputs := deployParams.inputs inputs := deployParams.Inputs
if inputs == nil { if inputs == nil {
inputs = make(map[string][]byte) inputs = make(map[string][]byte)
} }
@ -123,7 +117,7 @@ func (d *depTreeDeployer) result() *DeploymentResult {
// deployed are returned in the result. // deployed are returned in the result.
func LinkAndDeploy(deployParams *DeploymentParams, deploy DeployFn) (res *DeploymentResult, err error) { func LinkAndDeploy(deployParams *DeploymentParams, deploy DeployFn) (res *DeploymentResult, err error) {
deployer := newDepTreeDeployer(deployParams, deploy) deployer := newDepTreeDeployer(deployParams, deploy)
for _, contract := range deployParams.contracts { for _, contract := range deployParams.Contracts {
if _, err := deployer.linkAndDeploy(contract); err != nil { if _, err := deployer.linkAndDeploy(contract); err != nil {
return deployer.result(), err return deployer.result(), err
} }

View file

@ -193,7 +193,10 @@ func testLinkCase(tcInput linkTestCaseInput) error {
overrides[pattern] = override overrides[pattern] = override
} }
deployParams := NewDeploymentParams(contractsList, nil, overrides) deployParams := &DeploymentParams{
Contracts: contractsList,
Overrides: overrides,
}
res, err := LinkAndDeploy(deployParams, mockDeploy) res, err := LinkAndDeploy(deployParams, mockDeploy)
if err != nil { if err != nil {
return err return err

View file

@ -108,8 +108,10 @@ func TestDeploymentLibraries(t *testing.T) {
c := nested_libraries.NewC1() c := nested_libraries.NewC1()
constructorInput := c.PackConstructor(big.NewInt(42), big.NewInt(1)) constructorInput := c.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 := &bind.DeploymentParams{
Contracts: []*bind.MetaData{&nested_libraries.C1MetaData},
Inputs: map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput},
}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil { if err != nil {
t.Fatalf("err: %+v\n", err) t.Fatalf("err: %+v\n", err)
@ -149,8 +151,9 @@ func TestDeploymentWithOverrides(t *testing.T) {
defer bindBackend.Backend.Close() defer bindBackend.Backend.Close()
// deploy all the library dependencies of our target contract, but not the target contract itself. // 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 := &bind.DeploymentParams{
Contracts: nested_libraries.C1MetaData.Deps,
}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil { if err != nil {
t.Fatalf("err: %+v\n", err) t.Fatalf("err: %+v\n", err)
@ -170,8 +173,13 @@ func TestDeploymentWithOverrides(t *testing.T) {
c := nested_libraries.NewC1() c := nested_libraries.NewC1()
constructorInput := c.PackConstructor(big.NewInt(42), big.NewInt(1)) constructorInput := c.PackConstructor(big.NewInt(42), big.NewInt(1))
overrides := res.Addrs overrides := res.Addrs
// deploy the contract // deploy the contract
deploymentParams = bind.NewDeploymentParams([]*bind.MetaData{&nested_libraries.C1MetaData}, map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput}, overrides) deploymentParams = &bind.DeploymentParams{
Contracts: []*bind.MetaData{&nested_libraries.C1MetaData},
Inputs: map[string][]byte{nested_libraries.C1MetaData.Pattern: constructorInput},
Overrides: overrides,
}
res, err = bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend)) res, err = bind.LinkAndDeploy(deploymentParams, makeTestDeployer(opts, bindBackend))
if err != nil { if err != nil {
t.Fatalf("err: %+v\n", err) t.Fatalf("err: %+v\n", err)
@ -208,7 +216,7 @@ func TestEvents(t *testing.T) {
t.Fatalf("error setting up testing env: %v", err) t.Fatalf("error setting up testing env: %v", err)
} }
deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&events.CMetaData}, nil, nil) deploymentParams := &bind.DeploymentParams{Contracts: []*bind.MetaData{&events.CMetaData}}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend)) res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
if err != nil { if err != nil {
t.Fatalf("error deploying contract for testing: %v", err) t.Fatalf("error deploying contract for testing: %v", err)
@ -307,7 +315,7 @@ func TestErrors(t *testing.T) {
t.Fatalf("error setting up testing env: %v", err) t.Fatalf("error setting up testing env: %v", err)
} }
deploymentParams := bind.NewDeploymentParams([]*bind.MetaData{&solc_errors.CMetaData}, nil, nil) deploymentParams := &bind.DeploymentParams{Contracts: []*bind.MetaData{&solc_errors.CMetaData}}
res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend)) res, err := bind.LinkAndDeploy(deploymentParams, makeTestDeployer(txAuth, backend))
if err != nil { if err != nil {
t.Fatalf("error deploying contract for testing: %v", err) t.Fatalf("error deploying contract for testing: %v", err)