Skip to content

Commit

Permalink
fix: rename x/supply gRPC queries (#835)
Browse files Browse the repository at this point in the history
## Description

This PR renames the `TotalSupply` and `CirculatingSupply` gRPC endpoints to simply be `Total` and `Circulating`, removing the verbose `Supply` suffix.

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [x] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [x] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [x] reviewed tests and test coverage
- [x] manually tested (if applicable)
  • Loading branch information
RiccardoM authored Apr 26, 2022
1 parent 3a559c0 commit 4aab7fe
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 303 deletions.
37 changes: 17 additions & 20 deletions proto/desmos/supply/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ option go_package = "github.com/desmos-labs/desmos/v3/x/supply/types";
// Query defines the gRPC querier service.
service Query {

// TotalSupply queries the total supply of the given denom
rpc TotalSupply(QueryTotalSupplyRequest) returns (QueryTotalSupplyResponse) {
option (google.api.http).get = "/desmos/supply/v1/total-supply/{denom}";
// Total queries the total supply of the given denom
rpc Total(QueryTotalRequest) returns (QueryTotalResponse) {
option (google.api.http).get = "/desmos/supply/v1/total/{denom}";
}

// CirculatingSupply queries the amount of tokens circulating in the market of
// the given denom
rpc CirculatingSupply(QueryCirculatingSupplyRequest)
returns (QueryCirculatingSupplyResponse) {
option (google.api.http).get =
"/desmos/supply/v1/circulating-supply/{denom}";
// Circulating queries the amount of tokens circulating in the market of the
// given denom
rpc Circulating(QueryCirculatingRequest) returns (QueryCirculatingResponse) {
option (google.api.http).get = "/desmos/supply/v1/circulating/{denom}";
}
}

// QueryTotalSupplyRequest is the request type for Query/TotalSupply RPC method
message QueryTotalSupplyRequest {
// QueryTotalRequest is the request type for Query/Total RPC method
message QueryTotalRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand All @@ -40,9 +38,8 @@ message QueryTotalSupplyRequest {
uint64 divider_exponent = 2;
}

// QueryTotalSupplyResponse is the response type for the Query/TotalSupply RPC
// method
message QueryTotalSupplyResponse {
// QueryTotalResponse is the response type for the Query/Total RPC method
message QueryTotalResponse {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand All @@ -52,9 +49,9 @@ message QueryTotalSupplyResponse {
];
}

// QueryCirculatingSupplyRequest is the request type for the
// Query/CirculatingSupply RPC method
message QueryCirculatingSupplyRequest {
// QueryCirculatingRequest is the request type for the Query/Circulating RPC
// method
message QueryCirculatingRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand All @@ -66,9 +63,9 @@ message QueryCirculatingSupplyRequest {
uint64 divider_exponent = 2;
}

// QueryCirculatingSupplyRequest is the response type for the
// Query/CirculatingSupply RPC method
message QueryCirculatingSupplyResponse {
// QueryCirculatingResponse is the response type for the Query/Circulating RPC
// method
message QueryCirculatingResponse {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand Down
16 changes: 8 additions & 8 deletions x/supply/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *IntegrationTestSuite) TestCmdQueryTotalSupply() {
name string
args []string
shouldErr bool
expectedOutput types.QueryTotalSupplyResponse
expectedOutput types.QueryTotalResponse
}{
{
name: "invalid denom returns error",
Expand All @@ -80,7 +80,7 @@ func (s *IntegrationTestSuite) TestCmdQueryTotalSupply() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
shouldErr: false,
expectedOutput: types.QueryTotalSupplyResponse{TotalSupply: sdk.NewInt(1000000020)},
expectedOutput: types.QueryTotalResponse{TotalSupply: sdk.NewInt(1000000020)},
},
{
name: "total supply returned correctly with divider_exponent conversion applied",
Expand All @@ -90,7 +90,7 @@ func (s *IntegrationTestSuite) TestCmdQueryTotalSupply() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
shouldErr: false,
expectedOutput: types.QueryTotalSupplyResponse{TotalSupply: sdk.NewInt(10000000)},
expectedOutput: types.QueryTotalResponse{TotalSupply: sdk.NewInt(10000000)},
},
}

Expand All @@ -107,7 +107,7 @@ func (s *IntegrationTestSuite) TestCmdQueryTotalSupply() {
} else {
s.Require().NoError(err)

var response types.QueryTotalSupplyResponse
var response types.QueryTotalResponse
s.Require().NoError(clientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &response), out.String())
s.Require().Equal(tc.expectedOutput.TotalSupply, response.TotalSupply)
}
Expand All @@ -122,7 +122,7 @@ func (s *IntegrationTestSuite) TestCmdQueryCirculatingSupply() {
name string
args []string
shouldErr bool
expectedOutput types.QueryCirculatingSupplyResponse
expectedOutput types.QueryCirculatingResponse
}{
{
name: "invalid denom returns error",
Expand All @@ -139,7 +139,7 @@ func (s *IntegrationTestSuite) TestCmdQueryCirculatingSupply() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
shouldErr: false,
expectedOutput: types.QueryCirculatingSupplyResponse{CirculatingSupply: sdk.NewInt(1000000020)},
expectedOutput: types.QueryCirculatingResponse{CirculatingSupply: sdk.NewInt(1000000020)},
},
{
name: "circulating supply returned correctly with divider_exponent conversion applied",
Expand All @@ -149,7 +149,7 @@ func (s *IntegrationTestSuite) TestCmdQueryCirculatingSupply() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
shouldErr: false,
expectedOutput: types.QueryCirculatingSupplyResponse{CirculatingSupply: sdk.NewInt(10000000)},
expectedOutput: types.QueryCirculatingResponse{CirculatingSupply: sdk.NewInt(10000000)},
},
}

Expand All @@ -166,7 +166,7 @@ func (s *IntegrationTestSuite) TestCmdQueryCirculatingSupply() {
} else {
s.Require().NoError(err)

var response types.QueryCirculatingSupplyResponse
var response types.QueryCirculatingResponse
s.Require().NoError(clientCtx.JSONCodec.UnmarshalJSON(out.Bytes(), &response), out.String())
s.Require().Equal(tc.expectedOutput.CirculatingSupply, response.CirculatingSupply)
}
Expand Down
37 changes: 16 additions & 21 deletions x/supply/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func GetQueryCmd() *cobra.Command {
// converted with a divider powered with the given (optional) divider_exponent
func GetCmdQueryTotalSupply() *cobra.Command {
cmd := &cobra.Command{
Use: "total-supply [denom] [[divider_exponent]]",
Use: "total [denom] [[divider_exponent]]",
Short: "Query the total supply of the given denom. It can be converted with an optional 10 divider powered with the given divider_exponent",
Example: fmt.Sprintf(`%s query supply total-supply "stake"
%s query supply total-supply "stake" 5`, version.AppName, version.AppName),
Long: `Get the total supply of a token with the given denom. If a divider exponent is given,the returned result
will be divided by 10^(divider_exponent).`,
Long: `Get the total supply of a token with the given denom.
If a divider exponent is given,the returned result will be divided by 10^(divider_exponent).`,
Example: fmt.Sprintf(`
%s query supply total "stake"
%s query supply total "stake" 5`,
version.AppName, version.AppName),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -62,7 +64,7 @@ will be divided by 10^(divider_exponent).`,
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.TotalSupply(context.Background(), types.NewQueryTotalSupplyRequest(denom, divider))
res, err := queryClient.Total(context.Background(), types.NewQueryTotalRequest(denom, divider))
if err != nil {
return err
}
Expand All @@ -78,21 +80,14 @@ will be divided by 10^(divider_exponent).`,
// converted with the given (optional) divider
func GetCmdQueryCirculatingSupply() *cobra.Command {
cmd := &cobra.Command{
Use: "circulating-supply [denom] [[divider_exponent]]",
Use: "circulating [denom] [[divider_exponent]]",
Short: "Query the circulating supply of the given denom. It can be converted with an optional 10 divider powered with the given divider_exponent",
Example: fmt.Sprintf(`%s query supply circulating-supply "stake"
%s query supply circulating-supply "stake" 5`, version.AppName, version.AppName),
Long: fmt.Sprintf(`Get the circulating supply of a token with the given denom. The result can be converted with an optional 10 divider powered by the given divider_exponent. If the default value is kept, the result will be displayed in millionth (the common way with which tokens' amount are displayed on cosmo-SDK chains. Otherwise it will be converted according to the divider_exponent).
1. Without divider
%s desmos query supply circulating-supply udsm
2. With divider
%s desmos query supply circulating-supply udsm 6
6 means 10^6 = 1_000_000 divider
`, version.AppName, version.AppName,
),
Long: `Get the circulating supply of a token with the given denom.
If a divider exponent is given,the returned result will be divided by 10^(divider_exponent).`,
Example: fmt.Sprintf(`
%s query supply circulating "stake"
%s query supply circulating "stake" 5`,
version.AppName, version.AppName),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -114,7 +109,7 @@ func GetCmdQueryCirculatingSupply() *cobra.Command {
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.CirculatingSupply(context.Background(), types.NewQueryCirculatingSupplyRequest(denom, divider))
res, err := queryClient.Circulating(context.Background(), types.NewQueryCirculatingRequest(denom, divider))
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions x/supply/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/supply/circulating/{%s}", DenomParam), queryCirculatingSupplyFn(clientCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/supply/total/{%s}", DenomParam), queryTotalSupplyFn(clientCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/supply/circulating/{%s}", DenomParam), queryCirculatingSupplyFn(clientCtx)).Methods("GET")
}

func queryCirculatingSupplyFn(clientCtx client.Context) http.HandlerFunc {
func queryTotalSupplyFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
clientCtx, ok := resttypes.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
Expand All @@ -34,13 +34,13 @@ func queryCirculatingSupplyFn(clientCtx client.Context) http.HandlerFunc {
}

vars := mux.Vars(r)
params := types.NewQueryCirculatingSupplyRequest(vars[DenomParam], divider)
params := types.NewQueryTotalRequest(vars[DenomParam], divider)
bz, err := clientCtx.Codec.Marshal(params)
if resttypes.CheckBadRequestError(w, err) {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryCirculatingSupply)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply)
res, height, err := clientCtx.QueryWithData(route, bz)
if resttypes.CheckInternalServerError(w, err) {
return
Expand All @@ -51,7 +51,7 @@ func queryCirculatingSupplyFn(clientCtx client.Context) http.HandlerFunc {
}
}

func queryTotalSupplyFn(clientCtx client.Context) http.HandlerFunc {
func queryCirculatingSupplyFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
clientCtx, ok := resttypes.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
Expand All @@ -69,13 +69,13 @@ func queryTotalSupplyFn(clientCtx client.Context) http.HandlerFunc {
}

vars := mux.Vars(r)
params := types.NewQueryTotalSupplyRequest(vars[DenomParam], divider)
params := types.NewQueryCirculatingRequest(vars[DenomParam], divider)
bz, err := clientCtx.Codec.Marshal(params)
if resttypes.CheckBadRequestError(w, err) {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryCirculatingSupply)
res, height, err := clientCtx.QueryWithData(route, bz)
if resttypes.CheckInternalServerError(w, err) {
return
Expand Down
12 changes: 6 additions & 6 deletions x/supply/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (

var _ types.QueryServer = Keeper{}

// TotalSupply implements the Query/TotalSupply gRPC method
func (k Keeper) TotalSupply(ctx context.Context, request *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
// Total implements the Query/Total gRPC method
func (k Keeper) Total(ctx context.Context, request *types.QueryTotalRequest) (*types.QueryTotalResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply := k.GetTotalSupply(sdkCtx, request.Denom, types.NewDividerPoweredByExponent(request.DividerExponent))
return &types.QueryTotalSupplyResponse{TotalSupply: totalSupply}, nil
return &types.QueryTotalResponse{TotalSupply: totalSupply}, nil
}

// CirculatingSupply implements the Query/CirculatingSupply gRPC method
func (k Keeper) CirculatingSupply(ctx context.Context, request *types.QueryCirculatingSupplyRequest) (*types.QueryCirculatingSupplyResponse, error) {
// Circulating implements the Query/Circulating gRPC method
func (k Keeper) Circulating(ctx context.Context, request *types.QueryCirculatingRequest) (*types.QueryCirculatingResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
circulatingSupply := k.GetCirculatingSupply(sdkCtx, request.Denom, types.NewDividerPoweredByExponent(request.DividerExponent))
return &types.QueryCirculatingSupplyResponse{CirculatingSupply: circulatingSupply}, nil
return &types.QueryCirculatingResponse{CirculatingSupply: circulatingSupply}, nil
}
20 changes: 10 additions & 10 deletions x/supply/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/desmos-labs/desmos/v3/x/supply/types"
)

func (suite *KeeperTestSuite) TestQueryServer_TotalSupply() {
func (suite *KeeperTestSuite) TestQueryServer_Total() {
testCases := []struct {
name string
store func(ctx sdk.Context)
req *types.QueryTotalSupplyRequest
req *types.QueryTotalRequest
expSupply sdk.Int
}{
{
Expand All @@ -22,7 +22,7 @@ func (suite *KeeperTestSuite) TestQueryServer_TotalSupply() {
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(300_000))),
)
},
req: types.NewQueryTotalSupplyRequest(sdk.DefaultBondDenom, 6),
req: types.NewQueryTotalRequest(sdk.DefaultBondDenom, 6),
expSupply: sdk.NewInt(1_000_000),
},
{
Expand All @@ -34,7 +34,7 @@ func (suite *KeeperTestSuite) TestQueryServer_TotalSupply() {
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(300_000))),
)
},
req: types.NewQueryTotalSupplyRequest(sdk.DefaultBondDenom, 0),
req: types.NewQueryTotalRequest(sdk.DefaultBondDenom, 0),
expSupply: sdk.NewInt(1_000_000_000_000),
},
}
Expand All @@ -47,18 +47,18 @@ func (suite *KeeperTestSuite) TestQueryServer_TotalSupply() {
tc.store(ctx)
}

res, err := suite.k.TotalSupply(sdk.WrapSDKContext(ctx), tc.req)
res, err := suite.k.Total(sdk.WrapSDKContext(ctx), tc.req)
suite.Require().NoError(err)
suite.Require().Equal(tc.expSupply, res.TotalSupply)
})
}
}

func (suite *KeeperTestSuite) TestQueryServer_CirculatingSupply() {
func (suite *KeeperTestSuite) TestQueryServer_Circulating() {
testCases := []struct {
name string
store func(ctx sdk.Context)
req *types.QueryCirculatingSupplyRequest
req *types.QueryCirculatingRequest
expSupply sdk.Int
}{
{
Expand All @@ -70,7 +70,7 @@ func (suite *KeeperTestSuite) TestQueryServer_CirculatingSupply() {
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(300_000))),
)
},
req: types.NewQueryCirculatingSupplyRequest(sdk.DefaultBondDenom, 3),
req: types.NewQueryCirculatingRequest(sdk.DefaultBondDenom, 3),
expSupply: sdk.NewInt(500),
},
{
Expand All @@ -82,7 +82,7 @@ func (suite *KeeperTestSuite) TestQueryServer_CirculatingSupply() {
sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(300_000))),
)
},
req: types.NewQueryCirculatingSupplyRequest(sdk.DefaultBondDenom, 0),
req: types.NewQueryCirculatingRequest(sdk.DefaultBondDenom, 0),
expSupply: sdk.NewInt(500_000),
},
}
Expand All @@ -95,7 +95,7 @@ func (suite *KeeperTestSuite) TestQueryServer_CirculatingSupply() {
tc.store(ctx)
}

res, err := suite.k.CirculatingSupply(sdk.WrapSDKContext(ctx), tc.req)
res, err := suite.k.Circulating(sdk.WrapSDKContext(ctx), tc.req)
suite.Require().NoError(err)
suite.Require().Equal(tc.expSupply, res.CirculatingSupply)
})
Expand Down
Loading

0 comments on commit 4aab7fe

Please sign in to comment.