github.com/InjectiveLabs/sdk-go@v1.53.0/client/chain/chain_test.go (about)

     1  package chain_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	rpchttp "github.com/cometbft/cometbft/rpc/client/http"
     8  	"github.com/cosmos/cosmos-sdk/crypto/keyring"
     9  	cosmtypes "github.com/cosmos/cosmos-sdk/types"
    10  	eth "github.com/ethereum/go-ethereum/common"
    11  
    12  	"github.com/InjectiveLabs/sdk-go/client"
    13  	"github.com/InjectiveLabs/sdk-go/client/chain"
    14  	"github.com/InjectiveLabs/sdk-go/client/common"
    15  )
    16  
    17  func accountForTests() (cosmtypes.AccAddress, keyring.Keyring, error) {
    18  	senderAddress, cosmosKeyring, err := chain.InitCosmosKeyring(
    19  		os.Getenv("HOME")+"/.injectived",
    20  		"injectived",
    21  		"file",
    22  		"inj-user",
    23  		"12345678",
    24  		"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
    25  		false,
    26  	)
    27  
    28  	return senderAddress, cosmosKeyring, err
    29  }
    30  
    31  func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyring, network common.Network) (chain.ChainClient, error) {
    32  	tmClient, _ := rpchttp.New(network.TmEndpoint, "/websocket")
    33  	clientCtx, err := chain.NewClientContext(
    34  		network.ChainId,
    35  		senderAddress.String(),
    36  		cosmosKeyring,
    37  	)
    38  
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
    44  	// configure Keyring as nil to avoid the account initialization request when running unit tests
    45  	clientCtx.Keyring = nil
    46  
    47  	chainClient, err := chain.NewChainClient(
    48  		clientCtx,
    49  		network,
    50  		common.OptionGasPrices(client.DefaultGasPriceWithDenom),
    51  	)
    52  
    53  	return chainClient, err
    54  }
    55  
    56  func TestDefaultSubaccount(t *testing.T) {
    57  	network := common.LoadNetwork("devnet", "lb")
    58  	senderAddress, cosmosKeyring, err := accountForTests()
    59  
    60  	if err != nil {
    61  		t.Errorf("Error creating the address %v", err)
    62  	}
    63  
    64  	chainClient, err := createClient(senderAddress, cosmosKeyring, network)
    65  
    66  	if err != nil {
    67  		t.Errorf("Error creating the client %v", err)
    68  	}
    69  
    70  	defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress)
    71  
    72  	expectedSubaccountId := "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000"
    73  	expectedSubaccountIdHash := eth.HexToHash(expectedSubaccountId)
    74  	if defaultSubaccountID != expectedSubaccountIdHash {
    75  		t.Error("The default subaccount is calculated incorrectly")
    76  	}
    77  }
    78  
    79  func TestGetSubaccountWithIndex(t *testing.T) {
    80  	network := common.LoadNetwork("devnet", "lb")
    81  	senderAddress, cosmosKeyring, err := accountForTests()
    82  
    83  	if err != nil {
    84  		t.Errorf("Error creating the address %v", err)
    85  	}
    86  
    87  	chainClient, err := createClient(senderAddress, cosmosKeyring, network)
    88  
    89  	if err != nil {
    90  		t.Errorf("Error creating the client %v", err)
    91  	}
    92  
    93  	subaccountOne := chainClient.Subaccount(senderAddress, 1)
    94  	subaccountThirty := chainClient.Subaccount(senderAddress, 30)
    95  
    96  	expectedSubaccounOnetId := "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000001"
    97  	expectedSubaccountOneIdHash := eth.HexToHash(expectedSubaccounOnetId)
    98  
    99  	expectedSubaccounThirtytId := "0xaf79152ac5df276d9a8e1e2e22822f971347490200000000000000000000001e"
   100  	expectedSubaccountThirtyIdHash := eth.HexToHash(expectedSubaccounThirtytId)
   101  
   102  	if subaccountOne != expectedSubaccountOneIdHash {
   103  		t.Error("The subaccount with index 1 was calculated incorrectly")
   104  	}
   105  	if subaccountThirty != expectedSubaccountThirtyIdHash {
   106  		t.Error("The subaccount with index 30 was calculated incorrectly")
   107  	}
   108  }