diff --git a/accounts/abi/abigen/source.go.tpl b/accounts/abi/abigen/source.go.tpl index c84862d03b..56ff1e66f0 100644 --- a/accounts/abi/abigen/source.go.tpl +++ b/accounts/abi/abigen/source.go.tpl @@ -77,7 +77,10 @@ var ( return common.Address{}, nil, nil, errors.New("GetABI returned nil") } {{range $pattern, $name := .Libraries}} - {{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend) + {{decapitalise $name}}Addr, tx, _, _ := Deploy{{capitalise $name}}(auth, backend) + if err := bind.WaitAccepted(context.Background(), backend, tx.Hash()); err != nil { + return common.Address{}, nil, nil err + } {{$contract.Type}}Bin = strings.ReplaceAll({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:]) {{end}} address, tx, contract, err := bind.DeployContract(auth, *parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) diff --git a/accounts/abi/bind/v2/backend.go b/accounts/abi/bind/v2/backend.go index 2f5f17b31e..6ddd6b8b2c 100644 --- a/accounts/abi/bind/v2/backend.go +++ b/accounts/abi/bind/v2/backend.go @@ -109,6 +109,7 @@ type ContractTransactor interface { type DeployBackend interface { TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) + TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) } // ContractFilterer defines the methods needed to access log events using one-off diff --git a/accounts/abi/bind/v2/base.go b/accounts/abi/bind/v2/base.go index 535c0ed4fd..0a34523575 100644 --- a/accounts/abi/bind/v2/base.go +++ b/accounts/abi/bind/v2/base.go @@ -313,7 +313,7 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add } } // create the transaction - nonce, err := c.getNonce(opts) + nonce, err := c.GetNonce(opts) if err != nil { return nil, err } @@ -358,7 +358,7 @@ func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Addr } } // create the transaction - nonce, err := c.getNonce(opts) + nonce, err := c.GetNonce(opts) if err != nil { return nil, err } @@ -395,7 +395,7 @@ func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Ad return c.transactor.EstimateGas(ensureContext(opts.Context), msg) } -func (c *BoundContract) getNonce(opts *TransactOpts) (uint64, error) { +func (c *BoundContract) GetNonce(opts *TransactOpts) (uint64, error) { if opts.Nonce == nil { return c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From) } else { diff --git a/accounts/abi/bind/v2/util.go b/accounts/abi/bind/v2/util.go index 438848a753..0a0e86441a 100644 --- a/accounts/abi/bind/v2/util.go +++ b/accounts/abi/bind/v2/util.go @@ -75,3 +75,28 @@ func WaitDeployed(ctx context.Context, b DeployBackend, hash common.Hash) (commo } return receipt.ContractAddress, err } + +func WaitAccepted(ctx context.Context, d DeployBackend, txHash common.Hash) error { + queryTicker := time.NewTicker(time.Second) + defer queryTicker.Stop() + logger := log.New("hash", txHash) + for { + _, _, err := d.TransactionByHash(ctx, txHash) + if err == nil { + return nil + } + + if errors.Is(err, ethereum.NotFound) { // TODO: check this is emitted + logger.Trace("Transaction not yet accepted") + } else { + logger.Trace("Transaction submission failed", "err", err) + } + + // Wait for the next round. + select { + case <-ctx.Done(): + return ctx.Err() + case <-queryTicker.C: + } + } +}