github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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  	"fmt"
    21  	"time"
    22  
    23  	"github.com/atheioschain/go-atheios/common"
    24  	"github.com/atheioschain/go-atheios/core/types"
    25  	"github.com/atheioschain/go-atheios/logger"
    26  	"github.com/atheioschain/go-atheios/logger/glog"
    27  	"golang.org/x/net/context"
    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(1 * time.Second)
    34  	defer queryTicker.Stop()
    35  	loghash := tx.Hash().Hex()[:8]
    36  	for {
    37  		receipt, err := b.TransactionReceipt(ctx, tx.Hash())
    38  		if receipt != nil {
    39  			return receipt, nil
    40  		}
    41  		if err != nil {
    42  			glog.V(logger.Detail).Infof("tx %x error: %v", loghash, err)
    43  		} else {
    44  			glog.V(logger.Detail).Infof("tx %x not yet mined...", loghash)
    45  		}
    46  		// Wait for the next round.
    47  		select {
    48  		case <-ctx.Done():
    49  			return nil, ctx.Err()
    50  		case <-queryTicker.C:
    51  		}
    52  	}
    53  }
    54  
    55  // WaitDeployed waits for a contract deployment transaction and returns the on-chain
    56  // contract address when it is mined. It stops waiting when ctx is canceled.
    57  func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
    58  	if tx.To() != nil {
    59  		return common.Address{}, fmt.Errorf("tx is not contract creation")
    60  	}
    61  	receipt, err := WaitMined(ctx, b, tx)
    62  	if err != nil {
    63  		return common.Address{}, err
    64  	}
    65  	if receipt.ContractAddress == (common.Address{}) {
    66  		return common.Address{}, fmt.Errorf("zero address")
    67  	}
    68  	// Check that code has indeed been deployed at the address.
    69  	// This matters on pre-Homestead chains: OOG in the constructor
    70  	// could leave an empty account behind.
    71  	code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
    72  	if err == nil && len(code) == 0 {
    73  		err = ErrNoCodeAfterDeploy
    74  	}
    75  	return receipt.ContractAddress, err
    76  }