github.com/amazechain/amc@v0.1.3/accounts/abi/bind/util.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bind
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"github.com/amazechain/amc/common/block"
    23  	"github.com/amazechain/amc/common/transaction"
    24  	"github.com/amazechain/amc/common/types"
    25  	"github.com/amazechain/amc/log"
    26  	"time"
    27  )
    28  
    29  // NotFound is returned by API methods if the requested item does not exist.
    30  var NotFound = errors.New("not found")
    31  
    32  // WaitMined waits for tx to be mined on the blockchain.
    33  // It stops waiting when the context is canceled.
    34  func WaitMined(ctx context.Context, b DeployBackend, tx *transaction.Transaction) (*block.Receipt, error) {
    35  	queryTicker := time.NewTicker(time.Second)
    36  	defer queryTicker.Stop()
    37  
    38  	logger := log.New("hash", tx.Hash())
    39  	for {
    40  		receipt, err := b.TransactionReceipt(ctx, tx.Hash())
    41  		if err == nil {
    42  			return receipt, nil
    43  		}
    44  
    45  		if errors.Is(err, NotFound) {
    46  			logger.Trace("Transaction not yet mined")
    47  		} else {
    48  			logger.Trace("Receipt retrieval failed", "err", err)
    49  		}
    50  
    51  		// Wait for the next round.
    52  		select {
    53  		case <-ctx.Done():
    54  			return nil, ctx.Err()
    55  		case <-queryTicker.C:
    56  		}
    57  	}
    58  }
    59  
    60  // WaitDeployed waits for a contract deployment transaction and returns the on-chain
    61  // contract address when it is mined. It stops waiting when ctx is canceled.
    62  func WaitDeployed(ctx context.Context, b DeployBackend, tx *transaction.Transaction) (types.Address, error) {
    63  	if tx.To() != nil {
    64  		return types.Address{}, errors.New("tx is not contract creation")
    65  	}
    66  	receipt, err := WaitMined(ctx, b, tx)
    67  	if err != nil {
    68  		return types.Address{}, err
    69  	}
    70  	if receipt.ContractAddress == (types.Address{}) {
    71  		return types.Address{}, errors.New("zero address")
    72  	}
    73  	// Check that code has indeed been deployed at the address.
    74  	// This matters on pre-Homestead chains: OOG in the constructor
    75  	// could leave an empty account behind.
    76  	code, err := b.CodeAt(ctx, receipt.ContractAddress, nil)
    77  	if err == nil && len(code) == 0 {
    78  		err = ErrNoCodeAfterDeploy
    79  	}
    80  	return receipt.ContractAddress, err
    81  }