github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/abi/bind/util.go (about) 1 package bind 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/quickchainproject/quickchain/common" 9 "github.com/quickchainproject/quickchain/core/types" 10 "github.com/quickchainproject/quickchain/log" 11 ) 12 13 // WaitMined waits for tx to be mined on the blockchain. 14 // It stops waiting when the context is canceled. 15 func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { 16 queryTicker := time.NewTicker(time.Second) 17 defer queryTicker.Stop() 18 19 logger := log.New("hash", tx.Hash()) 20 for { 21 receipt, err := b.TransactionReceipt(ctx, tx.Hash()) 22 if receipt != nil { 23 return receipt, nil 24 } 25 if err != nil { 26 logger.Trace("Receipt retrieval failed", "err", err) 27 } else { 28 logger.Trace("Transaction not yet mined") 29 } 30 // Wait for the next round. 31 select { 32 case <-ctx.Done(): 33 return nil, ctx.Err() 34 case <-queryTicker.C: 35 } 36 } 37 } 38 39 // WaitDeployed waits for a contract deployment transaction and returns the on-chain 40 // contract address when it is mined. It stops waiting when ctx is canceled. 41 func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { 42 if tx.To() != nil { 43 return common.Address{}, fmt.Errorf("tx is not contract creation") 44 } 45 receipt, err := WaitMined(ctx, b, tx) 46 if err != nil { 47 return common.Address{}, err 48 } 49 if receipt.ContractAddress == (common.Address{}) { 50 return common.Address{}, fmt.Errorf("zero address") 51 } 52 // Check that code has indeed been deployed at the address. 53 // This matters on pre-Homestead chains: OOG in the constructor 54 // could leave an empty account behind. 55 code, err := b.CodeAt(ctx, receipt.ContractAddress, nil) 56 if err == nil && len(code) == 0 { 57 err = ErrNoCodeAfterDeploy 58 } 59 return receipt.ContractAddress, err 60 }