util.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind
  17. import (
  18. "context"
  19. "errors"
  20. "time"
  21. "github.com/ethereum/go-ethereum"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. // WaitMined waits for tx to be mined on the blockchain.
  27. // It stops waiting when the context is canceled.
  28. func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
  29. queryTicker := time.NewTicker(time.Second)
  30. defer queryTicker.Stop()
  31. logger := log.New("hash", tx.Hash())
  32. for {
  33. receipt, err := b.TransactionReceipt(ctx, tx.Hash())
  34. if err == nil {
  35. return receipt, nil
  36. }
  37. if errors.Is(err, ethereum.NotFound) {
  38. logger.Trace("Transaction not yet mined")
  39. } else {
  40. logger.Trace("Receipt retrieval failed", "err", err)
  41. }
  42. // Wait for the next round.
  43. select {
  44. case <-ctx.Done():
  45. return nil, ctx.Err()
  46. case <-queryTicker.C:
  47. }
  48. }
  49. }
  50. // WaitDeployed waits for a contract deployment transaction and returns the on-chain
  51. // contract address when it is mined. It stops waiting when ctx is canceled.
  52. func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
  53. if tx.To() != nil {
  54. return common.Address{}, errors.New("tx is not contract creation")
  55. }
  56. receipt, err := WaitMined(ctx, b, tx)
  57. if err != nil {
  58. return common.Address{}, err
  59. }
  60. if receipt.ContractAddress == (common.Address{}) {
  61. return common.Address{}, errors.New("zero address")
  62. }
  63. // Check that code has indeed been deployed at the address.
  64. // This matters on pre-Homestead chains: OOG in the constructor
  65. // could leave an empty account behind.
  66. code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
  67. if err == nil && len(code) == 0 {
  68. err = ErrNoCodeAfterDeploy
  69. }
  70. return receipt.ContractAddress, err
  71. }