github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/abi/bind/util.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package bind
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"time"
    18  
    19  	"github.com/Sberex/go-sberex/common"
    20  	"github.com/Sberex/go-sberex/core/types"
    21  	"github.com/Sberex/go-sberex/log"
    22  )
    23  
    24  // WaitMined waits for tx to be mined on the blockchain.
    25  // It stops waiting when the context is canceled.
    26  func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
    27  	queryTicker := time.NewTicker(time.Second)
    28  	defer queryTicker.Stop()
    29  
    30  	logger := log.New("hash", tx.Hash())
    31  	for {
    32  		receipt, err := b.TransactionReceipt(ctx, tx.Hash())
    33  		if receipt != nil {
    34  			return receipt, nil
    35  		}
    36  		if err != nil {
    37  			logger.Trace("Receipt retrieval failed", "err", err)
    38  		} else {
    39  			logger.Trace("Transaction not yet mined")
    40  		}
    41  		// Wait for the next round.
    42  		select {
    43  		case <-ctx.Done():
    44  			return nil, ctx.Err()
    45  		case <-queryTicker.C:
    46  		}
    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{}, fmt.Errorf("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{}, fmt.Errorf("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  }