github.com/Finschia/finschia-sdk@v0.49.1/x/bank/client/testutil/grpc.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  
     8  	"github.com/Finschia/finschia-sdk/testutil"
     9  	"github.com/Finschia/finschia-sdk/testutil/rest"
    10  	sdk "github.com/Finschia/finschia-sdk/types"
    11  	grpctypes "github.com/Finschia/finschia-sdk/types/grpc"
    12  	"github.com/Finschia/finschia-sdk/types/query"
    13  	"github.com/Finschia/finschia-sdk/x/bank/types"
    14  )
    15  
    16  func (s *IntegrationTestSuite) TestTotalSupplyGRPCHandler() {
    17  	val := s.network.Validators[0]
    18  	baseURL := val.APIAddress
    19  
    20  	testCases := []struct {
    21  		name     string
    22  		url      string
    23  		headers  map[string]string
    24  		respType proto.Message
    25  		expected proto.Message
    26  	}{
    27  		{
    28  			"test GRPC total supply",
    29  			fmt.Sprintf("%s/cosmos/bank/v1beta1/supply", baseURL),
    30  			map[string]string{
    31  				grpctypes.GRPCBlockHeightHeader: "1",
    32  			},
    33  			&types.QueryTotalSupplyResponse{},
    34  			&types.QueryTotalSupplyResponse{
    35  				Supply: sdk.NewCoins(
    36  					sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
    37  					sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
    38  				),
    39  				Pagination: &query.PageResponse{
    40  					Total: 2,
    41  				},
    42  			},
    43  		},
    44  		{
    45  			"GRPC total supply of a specific denom",
    46  			fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/%s", baseURL, s.cfg.BondDenom),
    47  			map[string]string{
    48  				grpctypes.GRPCBlockHeightHeader: "1",
    49  			},
    50  			&types.QuerySupplyOfResponse{},
    51  			&types.QuerySupplyOfResponse{
    52  				Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
    53  			},
    54  		},
    55  		{
    56  			"Query for `height` > 1",
    57  			fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/%s", baseURL, s.cfg.BondDenom),
    58  			map[string]string{
    59  				grpctypes.GRPCBlockHeightHeader: "2",
    60  			},
    61  			&types.QuerySupplyOfResponse{},
    62  			&types.QuerySupplyOfResponse{
    63  				Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(20))),
    64  			},
    65  		},
    66  		{
    67  			"Query params shouldn't be considered as height",
    68  			fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/%s?height=2", baseURL, s.cfg.BondDenom),
    69  			map[string]string{
    70  				grpctypes.GRPCBlockHeightHeader: "1",
    71  			},
    72  			&types.QuerySupplyOfResponse{},
    73  			&types.QuerySupplyOfResponse{
    74  				Amount: sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
    75  			},
    76  		},
    77  		{
    78  			"GRPC total supply of a bogus denom",
    79  			fmt.Sprintf("%s/cosmos/bank/v1beta1/supply/foobar", baseURL),
    80  			map[string]string{
    81  				grpctypes.GRPCBlockHeightHeader: "1",
    82  			},
    83  			&types.QuerySupplyOfResponse{},
    84  			&types.QuerySupplyOfResponse{
    85  				Amount: sdk.NewCoin("foobar", sdk.ZeroInt()),
    86  			},
    87  		},
    88  	}
    89  
    90  	for _, tc := range testCases {
    91  		s.Run(tc.name, func() {
    92  			resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
    93  			s.Require().NoError(err)
    94  
    95  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
    96  			s.Require().Equal(tc.expected.String(), tc.respType.String())
    97  		})
    98  	}
    99  }
   100  
   101  func (s *IntegrationTestSuite) TestDenomMetadataGRPCHandler() {
   102  	val := s.network.Validators[0]
   103  	baseURL := val.APIAddress
   104  
   105  	testCases := []struct {
   106  		name     string
   107  		url      string
   108  		headers  map[string]string
   109  		expErr   bool
   110  		respType proto.Message
   111  		expected proto.Message
   112  	}{
   113  		{
   114  			"test GRPC client metadata",
   115  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata", baseURL),
   116  			map[string]string{
   117  				grpctypes.GRPCBlockHeightHeader: "1",
   118  			},
   119  			false,
   120  			&types.QueryDenomsMetadataResponse{},
   121  			&types.QueryDenomsMetadataResponse{
   122  				Metadatas: []types.Metadata{
   123  					{
   124  						Name:        "Cosmos Hub Atom",
   125  						Symbol:      "ATOM",
   126  						Description: "The native staking token of the Cosmos Hub.",
   127  						DenomUnits: []*types.DenomUnit{
   128  							{
   129  								Denom:    "uatom",
   130  								Exponent: 0,
   131  								Aliases:  []string{"microatom"},
   132  							},
   133  							{
   134  								Denom:    "atom",
   135  								Exponent: 6,
   136  								Aliases:  []string{"ATOM"},
   137  							},
   138  						},
   139  						Base:    "uatom",
   140  						Display: "atom",
   141  					},
   142  					{
   143  						Name:        "Ethereum",
   144  						Symbol:      "ETH",
   145  						Description: "Ethereum mainnet token",
   146  						DenomUnits: []*types.DenomUnit{
   147  							{
   148  								Denom:    "wei",
   149  								Exponent: 0,
   150  							},
   151  							{
   152  								Denom:    "eth",
   153  								Exponent: 6,
   154  								Aliases:  []string{"ETH"},
   155  							},
   156  						},
   157  						Base:    "wei",
   158  						Display: "eth",
   159  					},
   160  				},
   161  				Pagination: &query.PageResponse{Total: 2},
   162  			},
   163  		},
   164  		{
   165  			"GRPC client metadata of a specific denom",
   166  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata/uatom", baseURL),
   167  			map[string]string{
   168  				grpctypes.GRPCBlockHeightHeader: "1",
   169  			},
   170  			false,
   171  			&types.QueryDenomMetadataResponse{},
   172  			&types.QueryDenomMetadataResponse{
   173  				Metadata: types.Metadata{
   174  					Name:        "Cosmos Hub Atom",
   175  					Symbol:      "ATOM",
   176  					Description: "The native staking token of the Cosmos Hub.",
   177  					DenomUnits: []*types.DenomUnit{
   178  						{
   179  							Denom:    "uatom",
   180  							Exponent: 0,
   181  							Aliases:  []string{"microatom"},
   182  						},
   183  						{
   184  							Denom:    "atom",
   185  							Exponent: 6,
   186  							Aliases:  []string{"ATOM"},
   187  						},
   188  					},
   189  					Base:    "uatom",
   190  					Display: "atom",
   191  				},
   192  			},
   193  		},
   194  		{
   195  			"GRPC client metadata of a bogus denom",
   196  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata/foobar", baseURL),
   197  			map[string]string{
   198  				grpctypes.GRPCBlockHeightHeader: "1",
   199  			},
   200  			true,
   201  			&types.QueryDenomMetadataResponse{},
   202  			&types.QueryDenomMetadataResponse{
   203  				Metadata: types.Metadata{
   204  					DenomUnits: []*types.DenomUnit{},
   205  				},
   206  			},
   207  		},
   208  	}
   209  
   210  	for _, tc := range testCases {
   211  		s.Run(tc.name, func() {
   212  			resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   213  			s.Require().NoError(err)
   214  
   215  			if tc.expErr {
   216  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   217  			} else {
   218  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   219  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   220  			}
   221  		})
   222  	}
   223  }
   224  
   225  func (s *IntegrationTestSuite) TestBalancesGRPCHandler() {
   226  	val := s.network.Validators[0]
   227  	baseURL := val.APIAddress
   228  
   229  	testCases := []struct {
   230  		name     string
   231  		url      string
   232  		respType proto.Message
   233  		expected proto.Message
   234  	}{
   235  		{
   236  			"gRPC total account balance",
   237  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", baseURL, val.Address.String()),
   238  			&types.QueryAllBalancesResponse{},
   239  			&types.QueryAllBalancesResponse{
   240  				Balances: sdk.NewCoins(
   241  					sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
   242  					sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
   243  				),
   244  				Pagination: &query.PageResponse{
   245  					Total: 2,
   246  				},
   247  			},
   248  		},
   249  		{
   250  			"gPRC account balance of a denom",
   251  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=%s", baseURL, val.Address.String(), s.cfg.BondDenom),
   252  			&types.QueryBalanceResponse{},
   253  			&types.QueryBalanceResponse{
   254  				Balance: &sdk.Coin{
   255  					Denom:  s.cfg.BondDenom,
   256  					Amount: s.cfg.StakingTokens.Sub(s.cfg.BondedTokens),
   257  				},
   258  			},
   259  		},
   260  		{
   261  			"gPRC account balance of a bogus denom",
   262  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=foobar", baseURL, val.Address.String()),
   263  			&types.QueryBalanceResponse{},
   264  			&types.QueryBalanceResponse{
   265  				Balance: &sdk.Coin{
   266  					Denom:  "foobar",
   267  					Amount: sdk.NewInt(0),
   268  				},
   269  			},
   270  		},
   271  	}
   272  
   273  	for _, tc := range testCases {
   274  		s.Run(tc.name, func() {
   275  			resp, err := rest.GetRequest(tc.url)
   276  			s.Require().NoError(err)
   277  
   278  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   279  			s.Require().Equal(tc.expected.String(), tc.respType.String())
   280  		})
   281  	}
   282  }