github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/accounts/abi/bind/util.go (about) 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 17 package bind 18 19 import ( 20 "context" 21 "errors" 22 "time" 23 24 "github.com/ethxdao/go-ethereum/common" 25 "github.com/ethxdao/go-ethereum/core/types" 26 "github.com/ethxdao/go-ethereum/log" 27 ) 28 29 // WaitMined waits for tx to be mined on the blockchain. 30 // It stops waiting when the context is canceled. 31 func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { 32 queryTicker := time.NewTicker(time.Second) 33 defer queryTicker.Stop() 34 35 logger := log.New("hash", tx.Hash()) 36 for { 37 receipt, err := b.TransactionReceipt(ctx, tx.Hash()) 38 if err == nil { 39 return receipt, nil 40 } 41 42 if errors.Is(err, ethereum.NotFound) { 43 logger.Trace("Transaction not yet mined") 44 } else { 45 logger.Trace("Receipt retrieval failed", "err", err) 46 } 47 48 // Wait for the next round. 49 select { 50 case <-ctx.Done(): 51 return nil, ctx.Err() 52 case <-queryTicker.C: 53 } 54 } 55 } 56 57 // WaitDeployed waits for a contract deployment transaction and returns the on-chain 58 // contract address when it is mined. It stops waiting when ctx is canceled. 59 func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { 60 if tx.To() != nil { 61 return common.Address{}, errors.New("tx is not contract creation") 62 } 63 receipt, err := WaitMined(ctx, b, tx) 64 if err != nil { 65 return common.Address{}, err 66 } 67 if receipt.ContractAddress == (common.Address{}) { 68 return common.Address{}, errors.New("zero address") 69 } 70 // Check that code has indeed been deployed at the address. 71 // This matters on pre-Homestead chains: OOG in the constructor 72 // could leave an empty account behind. 73 code, err := b.CodeAt(ctx, receipt.ContractAddress, nil) 74 if err == nil && len(code) == 0 { 75 err = ErrNoCodeAfterDeploy 76 } 77 return receipt.ContractAddress, err 78 }