github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/bind/old.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 is the runtime for abigen v1 generated contract bindings.
    18  // Deprecated: please use github.com/ethereum/go-ethereum/bind/v2
    19  package bind
    20  
    21  import (
    22  	"context"
    23  	"crypto/ecdsa"
    24  	"errors"
    25  	"io"
    26  	"math/big"
    27  	"strings"
    28  	"sync"
    29  
    30  	"github.com/ethereum/go-ethereum/accounts"
    31  	"github.com/ethereum/go-ethereum/accounts/abi"
    32  	"github.com/ethereum/go-ethereum/accounts/abi/abigen"
    33  	bind2 "github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
    34  	"github.com/ethereum/go-ethereum/accounts/external"
    35  	"github.com/ethereum/go-ethereum/accounts/keystore"
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/core/types"
    38  	"github.com/ethereum/go-ethereum/crypto"
    39  	"github.com/ethereum/go-ethereum/log"
    40  )
    41  
    42  // Bind generates a v1 contract binding.
    43  // Deprecated: binding generation has moved to github.com/ethereum/go-ethereum/accounts/abi/abigen
    44  func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, libs map[string]string, aliases map[string]string) (string, error) {
    45  	return abigen.Bind(types, abis, bytecodes, fsigs, pkg, libs, aliases)
    46  }
    47  
    48  // auth.go
    49  
    50  // ErrNoChainID is returned whenever the user failed to specify a chain id.
    51  var ErrNoChainID = errors.New("no chain id specified")
    52  
    53  // ErrNotAuthorized is returned when an account is not properly unlocked.
    54  var ErrNotAuthorized = bind2.ErrNotAuthorized
    55  
    56  // NewTransactor is a utility method to easily create a transaction signer from
    57  // an encrypted json key stream and the associated passphrase.
    58  //
    59  // Deprecated: Use NewTransactorWithChainID instead.
    60  func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
    61  	log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID")
    62  	json, err := io.ReadAll(keyin)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	key, err := keystore.DecryptKey(json, passphrase)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return NewKeyedTransactor(key.PrivateKey), nil
    71  }
    72  
    73  // NewKeyStoreTransactor is a utility method to easily create a transaction signer from
    74  // a decrypted key from a keystore.
    75  //
    76  // Deprecated: Use NewKeyStoreTransactorWithChainID instead.
    77  func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) {
    78  	log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID")
    79  	signer := types.HomesteadSigner{}
    80  	return &TransactOpts{
    81  		From: account.Address,
    82  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    83  			if address != account.Address {
    84  				return nil, ErrNotAuthorized
    85  			}
    86  			signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes())
    87  			if err != nil {
    88  				return nil, err
    89  			}
    90  			return tx.WithSignature(signer, signature)
    91  		},
    92  		Context: context.Background(),
    93  	}, nil
    94  }
    95  
    96  // NewKeyedTransactor is a utility method to easily create a transaction signer
    97  // from a single private key.
    98  //
    99  // Deprecated: Use NewKeyedTransactorWithChainID instead.
   100  func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
   101  	log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
   102  	keyAddr := crypto.PubkeyToAddress(key.PublicKey)
   103  	signer := types.HomesteadSigner{}
   104  	return &TransactOpts{
   105  		From: keyAddr,
   106  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
   107  			if address != keyAddr {
   108  				return nil, ErrNotAuthorized
   109  			}
   110  			signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
   111  			if err != nil {
   112  				return nil, err
   113  			}
   114  			return tx.WithSignature(signer, signature)
   115  		},
   116  		Context: context.Background(),
   117  	}
   118  }
   119  
   120  // NewTransactorWithChainID is a utility method to easily create a transaction signer from
   121  // an encrypted json key stream and the associated passphrase.
   122  func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) {
   123  	json, err := io.ReadAll(keyin)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	key, err := keystore.DecryptKey(json, passphrase)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
   132  }
   133  
   134  // NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
   135  // a decrypted key from a keystore.
   136  func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) {
   137  	// New version panics for chainID == nil, catch it here.
   138  	if chainID == nil {
   139  		return nil, ErrNoChainID
   140  	}
   141  	return bind2.NewKeyStoreTransactor(keystore, account, chainID), nil
   142  }
   143  
   144  // NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
   145  // from a single private key.
   146  func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
   147  	// New version panics for chainID == nil, catch it here.
   148  	if chainID == nil {
   149  		return nil, ErrNoChainID
   150  	}
   151  	return bind2.NewKeyedTransactor(key, chainID), nil
   152  }
   153  
   154  // NewClefTransactor is a utility method to easily create a transaction signer
   155  // with a clef backend.
   156  func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts {
   157  	return bind2.NewClefTransactor(clef, account)
   158  }
   159  
   160  // backend.go
   161  
   162  var (
   163  	// ErrNoCode is returned by call and transact operations for which the requested
   164  	// recipient contract to operate on does not exist in the state db or does not
   165  	// have any code associated with it (i.e. self-destructed).
   166  	ErrNoCode = bind2.ErrNoCode
   167  
   168  	// ErrNoPendingState is raised when attempting to perform a pending state action
   169  	// on a backend that doesn't implement PendingContractCaller.
   170  	ErrNoPendingState = bind2.ErrNoPendingState
   171  
   172  	// ErrNoBlockHashState is raised when attempting to perform a block hash action
   173  	// on a backend that doesn't implement BlockHashContractCaller.
   174  	ErrNoBlockHashState = bind2.ErrNoBlockHashState
   175  
   176  	// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
   177  	// an empty contract behind.
   178  	ErrNoCodeAfterDeploy = bind2.ErrNoCodeAfterDeploy
   179  )
   180  
   181  // ContractCaller defines the methods needed to allow operating with a contract on a read
   182  // only basis.
   183  type ContractCaller = bind2.ContractCaller
   184  
   185  // PendingContractCaller defines methods to perform contract calls on the pending state.
   186  // Call will try to discover this interface when access to the pending state is requested.
   187  // If the backend does not support the pending state, Call returns ErrNoPendingState.
   188  type PendingContractCaller = bind2.PendingContractCaller
   189  
   190  // BlockHashContractCaller defines methods to perform contract calls on a specific block hash.
   191  // Call will try to discover this interface when access to a block by hash is requested.
   192  // If the backend does not support the block hash state, Call returns ErrNoBlockHashState.
   193  type BlockHashContractCaller = bind2.BlockHashContractCaller
   194  
   195  // ContractTransactor defines the methods needed to allow operating with a contract
   196  // on a write only basis. Besides the transacting method, the remainder are helpers
   197  // used when the user does not provide some needed values, but rather leaves it up
   198  // to the transactor to decide.
   199  type ContractTransactor = bind2.ContractTransactor
   200  
   201  // DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
   202  type DeployBackend = bind2.DeployBackend
   203  
   204  // ContractFilterer defines the methods needed to access log events using one-off
   205  // queries or continuous event subscriptions.
   206  type ContractFilterer = bind2.ContractFilterer
   207  
   208  // ContractBackend defines the methods needed to work with contracts on a read-write basis.
   209  type ContractBackend = bind2.ContractBackend
   210  
   211  // base.go
   212  
   213  type SignerFn = bind2.SignerFn
   214  
   215  type CallOpts = bind2.CallOpts
   216  
   217  type TransactOpts = bind2.TransactOpts
   218  
   219  type FilterOpts = bind2.FilterOpts
   220  
   221  type WatchOpts = bind2.WatchOpts
   222  
   223  type BoundContract = bind2.BoundContract
   224  
   225  func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor, filterer ContractFilterer) *BoundContract {
   226  	return bind2.NewBoundContract(address, abi, caller, transactor, filterer)
   227  }
   228  
   229  func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) {
   230  	packed, err := abi.Pack("", params...)
   231  	if err != nil {
   232  		return common.Address{}, nil, nil, err
   233  	}
   234  	addr, tx, err := bind2.DeployContract(opts, bytecode, backend, packed)
   235  	if err != nil {
   236  		return common.Address{}, nil, nil, err
   237  	}
   238  	contract := NewBoundContract(addr, abi, backend, backend, backend)
   239  	return addr, tx, contract, nil
   240  }
   241  
   242  // MetaData collects all metadata for a bound contract.
   243  type MetaData struct {
   244  	Bin       string            // runtime bytecode (as a hex string)
   245  	ABI       string            // the raw ABI definition (JSON)
   246  	Sigs      map[string]string // 4byte identifier -> function signature
   247  	mu        sync.Mutex
   248  	parsedABI *abi.ABI
   249  }
   250  
   251  // GetAbi returns the parsed ABI definition.
   252  func (m *MetaData) GetAbi() (*abi.ABI, error) {
   253  	m.mu.Lock()
   254  	defer m.mu.Unlock()
   255  
   256  	if m.parsedABI != nil {
   257  		return m.parsedABI, nil
   258  	}
   259  	if parsed, err := abi.JSON(strings.NewReader(m.ABI)); err != nil {
   260  		return nil, err
   261  	} else {
   262  		m.parsedABI = &parsed
   263  	}
   264  	return m.parsedABI, nil
   265  }
   266  
   267  // util.go
   268  
   269  // WaitMined waits for tx to be mined on the blockchain.
   270  // It stops waiting when the context is canceled.
   271  func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) {
   272  	return bind2.WaitMined(ctx, b, tx.Hash())
   273  }
   274  
   275  // WaitMinedHash waits for a transaction with the provided hash to be mined on the blockchain.
   276  // It stops waiting when the context is canceled.
   277  func WaitMinedHash(ctx context.Context, b DeployBackend, hash common.Hash) (*types.Receipt, error) {
   278  	return bind2.WaitMined(ctx, b, hash)
   279  }
   280  
   281  // WaitDeployed waits for a contract deployment transaction and returns the on-chain
   282  // contract address when it is mined. It stops waiting when ctx is canceled.
   283  func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) {
   284  	if tx.To() != nil {
   285  		return common.Address{}, errors.New("tx is not contract creation")
   286  	}
   287  	return bind2.WaitDeployed(ctx, b, tx.Hash())
   288  }
   289  
   290  // WaitDeployedHash waits for a contract deployment transaction with the provided hash and returns the on-chain
   291  // contract address when it is mined. It stops waiting when ctx is canceled.
   292  func WaitDeployedHash(ctx context.Context, b DeployBackend, hash common.Hash) (common.Address, error) {
   293  	return bind2.WaitDeployed(ctx, b, hash)
   294  }