github.com/Finschia/finschia-sdk@v0.48.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  		tc := tc
    92  		s.Run(tc.name, func() {
    93  			resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
    94  			s.Require().NoError(err)
    95  
    96  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
    97  			s.Require().Equal(tc.expected.String(), tc.respType.String())
    98  		})
    99  	}
   100  }
   101  
   102  func (s *IntegrationTestSuite) TestDenomMetadataGRPCHandler() {
   103  	val := s.network.Validators[0]
   104  	baseURL := val.APIAddress
   105  
   106  	testCases := []struct {
   107  		name     string
   108  		url      string
   109  		headers  map[string]string
   110  		expErr   bool
   111  		respType proto.Message
   112  		expected proto.Message
   113  	}{
   114  		{
   115  			"test GRPC client metadata",
   116  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata", baseURL),
   117  			map[string]string{
   118  				grpctypes.GRPCBlockHeightHeader: "1",
   119  			},
   120  			false,
   121  			&types.QueryDenomsMetadataResponse{},
   122  			&types.QueryDenomsMetadataResponse{
   123  				Metadatas: []types.Metadata{
   124  					{
   125  						Name:        "Cosmos Hub Atom",
   126  						Symbol:      "ATOM",
   127  						Description: "The native staking token of the Cosmos Hub.",
   128  						DenomUnits: []*types.DenomUnit{
   129  							{
   130  								Denom:    "uatom",
   131  								Exponent: 0,
   132  								Aliases:  []string{"microatom"},
   133  							},
   134  							{
   135  								Denom:    "atom",
   136  								Exponent: 6,
   137  								Aliases:  []string{"ATOM"},
   138  							},
   139  						},
   140  						Base:    "uatom",
   141  						Display: "atom",
   142  					},
   143  					{
   144  						Name:        "Ethereum",
   145  						Symbol:      "ETH",
   146  						Description: "Ethereum mainnet token",
   147  						DenomUnits: []*types.DenomUnit{
   148  							{
   149  								Denom:    "wei",
   150  								Exponent: 0,
   151  							},
   152  							{
   153  								Denom:    "eth",
   154  								Exponent: 6,
   155  								Aliases:  []string{"ETH"},
   156  							},
   157  						},
   158  						Base:    "wei",
   159  						Display: "eth",
   160  					},
   161  				},
   162  				Pagination: &query.PageResponse{Total: 2},
   163  			},
   164  		},
   165  		{
   166  			"GRPC client metadata of a specific denom",
   167  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata/uatom", baseURL),
   168  			map[string]string{
   169  				grpctypes.GRPCBlockHeightHeader: "1",
   170  			},
   171  			false,
   172  			&types.QueryDenomMetadataResponse{},
   173  			&types.QueryDenomMetadataResponse{
   174  				Metadata: types.Metadata{
   175  					Name:        "Cosmos Hub Atom",
   176  					Symbol:      "ATOM",
   177  					Description: "The native staking token of the Cosmos Hub.",
   178  					DenomUnits: []*types.DenomUnit{
   179  						{
   180  							Denom:    "uatom",
   181  							Exponent: 0,
   182  							Aliases:  []string{"microatom"},
   183  						},
   184  						{
   185  							Denom:    "atom",
   186  							Exponent: 6,
   187  							Aliases:  []string{"ATOM"},
   188  						},
   189  					},
   190  					Base:    "uatom",
   191  					Display: "atom",
   192  				},
   193  			},
   194  		},
   195  		{
   196  			"GRPC client metadata of a bogus denom",
   197  			fmt.Sprintf("%s/cosmos/bank/v1beta1/denoms_metadata/foobar", baseURL),
   198  			map[string]string{
   199  				grpctypes.GRPCBlockHeightHeader: "1",
   200  			},
   201  			true,
   202  			&types.QueryDenomMetadataResponse{},
   203  			&types.QueryDenomMetadataResponse{
   204  				Metadata: types.Metadata{
   205  					DenomUnits: []*types.DenomUnit{},
   206  				},
   207  			},
   208  		},
   209  	}
   210  
   211  	for _, tc := range testCases {
   212  		tc := tc
   213  		s.Run(tc.name, func() {
   214  			resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
   215  			s.Require().NoError(err)
   216  
   217  			if tc.expErr {
   218  				s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   219  			} else {
   220  				s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   221  				s.Require().Equal(tc.expected.String(), tc.respType.String())
   222  			}
   223  		})
   224  	}
   225  }
   226  
   227  func (s *IntegrationTestSuite) TestBalancesGRPCHandler() {
   228  	val := s.network.Validators[0]
   229  	baseURL := val.APIAddress
   230  
   231  	testCases := []struct {
   232  		name     string
   233  		url      string
   234  		respType proto.Message
   235  		expected proto.Message
   236  	}{
   237  		{
   238  			"gRPC total account balance",
   239  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", baseURL, val.Address.String()),
   240  			&types.QueryAllBalancesResponse{},
   241  			&types.QueryAllBalancesResponse{
   242  				Balances: sdk.NewCoins(
   243  					sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
   244  					sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
   245  				),
   246  				Pagination: &query.PageResponse{
   247  					Total: 2,
   248  				},
   249  			},
   250  		},
   251  		{
   252  			"gPRC account balance of a denom",
   253  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=%s", baseURL, val.Address.String(), s.cfg.BondDenom),
   254  			&types.QueryBalanceResponse{},
   255  			&types.QueryBalanceResponse{
   256  				Balance: &sdk.Coin{
   257  					Denom:  s.cfg.BondDenom,
   258  					Amount: s.cfg.StakingTokens.Sub(s.cfg.BondedTokens),
   259  				},
   260  			},
   261  		},
   262  		{
   263  			"gPRC account balance of a bogus denom",
   264  			fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s/by_denom?denom=foobar", baseURL, val.Address.String()),
   265  			&types.QueryBalanceResponse{},
   266  			&types.QueryBalanceResponse{
   267  				Balance: &sdk.Coin{
   268  					Denom:  "foobar",
   269  					Amount: sdk.NewInt(0),
   270  				},
   271  			},
   272  		},
   273  	}
   274  
   275  	for _, tc := range testCases {
   276  		tc := tc
   277  		s.Run(tc.name, func() {
   278  			resp, err := rest.GetRequest(tc.url)
   279  			s.Require().NoError(err)
   280  
   281  			s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType))
   282  			s.Require().Equal(tc.expected.String(), tc.respType.String())
   283  		})
   284  	}
   285  }