github.com/0xsequence/ethkit@v1.25.0/ethtest/contracts.go (about)

     1  package ethtest
     2  
     3  import (
     4  	"context"
     5  	_ "embed"
     6  
     7  	"github.com/0xsequence/ethkit/ethartifact"
     8  	"github.com/0xsequence/ethkit/ethrpc"
     9  	"github.com/0xsequence/ethkit/ethtxn"
    10  	"github.com/0xsequence/ethkit/ethwallet"
    11  	"github.com/0xsequence/ethkit/go-ethereum"
    12  	"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
    13  	"github.com/0xsequence/ethkit/go-ethereum/common"
    14  	"github.com/0xsequence/ethkit/go-ethereum/core/types"
    15  )
    16  
    17  var (
    18  	//go:embed contracts/CallReceiverMock.json
    19  	artifact_callReceiverMock string
    20  
    21  	//go:embed contracts/ERC20Mock.json
    22  	artifact_erc20mock string
    23  
    24  	// Contracts registry to have some contracts on hand during testing
    25  	Contracts = ethartifact.NewContractRegistry()
    26  )
    27  
    28  func init() {
    29  	Contracts.MustAdd(ethartifact.MustParseArtifactJSON(artifact_callReceiverMock))
    30  	Contracts.MustAdd(ethartifact.MustParseArtifactJSON(artifact_erc20mock))
    31  }
    32  
    33  func ContractCall(provider *ethrpc.Provider, contractAddress common.Address, contractABI abi.ABI, result interface{}, method string, args ...interface{}) ([]byte, error) {
    34  	calldata, err := contractABI.Pack(method, args...)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	msg := ethereum.CallMsg{
    40  		To:   &contractAddress,
    41  		Data: calldata,
    42  	}
    43  
    44  	output, err := provider.CallContract(context.Background(), msg, nil)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	if result == nil {
    49  		return output, nil
    50  	}
    51  
    52  	err = contractABI.UnpackIntoInterface(result, method, output)
    53  	if err != nil {
    54  		return output, err
    55  	}
    56  	return output, nil
    57  }
    58  
    59  func ContractQuery(provider *ethrpc.Provider, contractAddress common.Address, inputExpr, outputExpr string, args []string) ([]string, error) {
    60  	return provider.ContractQuery(context.Background(), contractAddress.Hex(), inputExpr, outputExpr, args)
    61  }
    62  
    63  func ContractTransact(wallet *ethwallet.Wallet, contractAddress common.Address, contractABI abi.ABI, method string, args ...interface{}) (*types.Receipt, error) {
    64  	calldata, err := contractABI.Pack(method, args...)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	signedTxn, err := wallet.NewTransaction(context.Background(), &ethtxn.TransactionRequest{
    70  		To:   &contractAddress,
    71  		Data: calldata,
    72  	})
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	_, waitReceipt, err := wallet.SendTransaction(context.Background(), signedTxn)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	receipt, err := waitReceipt(context.Background())
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return receipt, nil
    88  }