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