github.com/cosmos/cosmos-sdk@v0.50.10/testutil/cli/tx.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cosmos/cosmos-sdk/client"
     7  	"github.com/cosmos/cosmos-sdk/client/flags"
     8  	"github.com/cosmos/cosmos-sdk/testutil/network"
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  	authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
    11  )
    12  
    13  // CheckTxCode verifies that the transaction result returns a specific code
    14  // Takes a network, wait for two blocks and fetch the transaction from its hash
    15  func CheckTxCode(network *network.Network, clientCtx client.Context, txHash string, expectedCode uint32) error {
    16  	// wait for 2 blocks
    17  	for i := 0; i < 2; i++ {
    18  		if err := network.WaitForNextBlock(); err != nil {
    19  			return fmt.Errorf("failed to wait for next block: %w", err)
    20  		}
    21  	}
    22  
    23  	cmd := authcli.QueryTxCmd()
    24  	out, err := ExecTestCLICmd(clientCtx, cmd, []string{txHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	var response sdk.TxResponse
    30  	if err := clientCtx.Codec.UnmarshalJSON(out.Bytes(), &response); err != nil {
    31  		return err
    32  	}
    33  
    34  	if response.Code != expectedCode {
    35  		return fmt.Errorf("expected code %d, got %d", expectedCode, response.Code)
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  // GetTxResponse returns queries the transaction response of a transaction from its hash
    42  // Takes a network, wait for two blocks and fetch the transaction from its hash
    43  func GetTxResponse(network *network.Network, clientCtx client.Context, txHash string) (sdk.TxResponse, error) {
    44  	// wait for 2 blocks
    45  	for i := 0; i < 2; i++ {
    46  		if err := network.WaitForNextBlock(); err != nil {
    47  			return sdk.TxResponse{}, fmt.Errorf("failed to wait for next block: %w", err)
    48  		}
    49  	}
    50  
    51  	cmd := authcli.QueryTxCmd()
    52  	out, err := ExecTestCLICmd(clientCtx, cmd, []string{txHash, fmt.Sprintf("--%s=json", flags.FlagOutput)})
    53  	if err != nil {
    54  		return sdk.TxResponse{}, err
    55  	}
    56  
    57  	var response sdk.TxResponse
    58  	if err := clientCtx.Codec.UnmarshalJSON(out.Bytes(), &response); err != nil {
    59  		return sdk.TxResponse{}, err
    60  	}
    61  
    62  	return response, nil
    63  }