github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/accounts/abi/bind/util.go (about)

     1  // Copyright 2022 Commercium
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bind
    19  
    20  import (
    21  	"context"
    22  	"errors"
    23  	"time"
    24  
    25  	"github.com/CommerciumBlockchain/go-commercium/common"
    26  	"github.com/CommerciumBlockchain/go-commercium/core/types"
    27  	"github.com/CommerciumBlockchain/go-commercium/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 receipt != nil {
    40  			return receipt, nil
    41  		}
    42  		if err != nil {
    43  			logger.Trace("Receipt retrieval failed", "err", err)
    44  		} else {
    45  			logger.Trace("Transaction not yet mined")
    46  		}
    47  		// Wait for the next round.
    48  		select {
    49  		case <-ctx.Done():
    50  			return nil, ctx.Err()
    51  		case <-queryTicker.C:
    52  		}
    53  	}
    54  }
    55  
    56  // WaitDeployed waits for a contract deployment transaction and returns the on-chain
    57  // contract address when it is mined. It stops waiting when ctx is canceled.
    58  func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
    59  	if tx.To() != nil {
    60  		return common.Address{}, errors.New("tx is not contract creation")
    61  	}
    62  	receipt, err := WaitMined(ctx, b, tx)
    63  	if err != nil {
    64  		return common.Address{}, err
    65  	}
    66  	if receipt.ContractAddress == (common.Address{}) {
    67  		return common.Address{}, errors.New("zero address")
    68  	}
    69  	// Check that code has indeed been deployed at the address.
    70  	// This matters on pre-Homestead chains: OOG in the constructor
    71  	// could leave an empty account behind.
    72  	code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
    73  	if err == nil && len(code) == 0 {
    74  		err = ErrNoCodeAfterDeploy
    75  	}
    76  	return receipt.ContractAddress, err
    77  }