Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Supply Offsets to bank module #119

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@
- [QueryParamsResponse](#cosmos.bank.v1beta1.QueryParamsResponse)
- [QuerySupplyOfRequest](#cosmos.bank.v1beta1.QuerySupplyOfRequest)
- [QuerySupplyOfResponse](#cosmos.bank.v1beta1.QuerySupplyOfResponse)
- [QuerySupplyOfWithoutOffsetRequest](#cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetRequest)
- [QuerySupplyOfWithoutOffsetResponse](#cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetResponse)
- [QueryTotalSupplyRequest](#cosmos.bank.v1beta1.QueryTotalSupplyRequest)
- [QueryTotalSupplyResponse](#cosmos.bank.v1beta1.QueryTotalSupplyResponse)
- [QueryTotalSupplyWithoutOffsetRequest](#cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetRequest)
- [QueryTotalSupplyWithoutOffsetResponse](#cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetResponse)

- [Query](#cosmos.bank.v1beta1.Query)

Expand Down Expand Up @@ -1268,6 +1272,9 @@ tags are stringified and the log is JSON decoded.
| `gas_used` | [int64](#int64) | | Amount of gas consumed by transaction. |
| `tx` | [google.protobuf.Any](#google.protobuf.Any) | | The request transaction bytes. |
| `timestamp` | [string](#string) | | Time of the previous block. For heights > 1, it's the weighted median of the timestamps of the valid votes in the block.LastCommit. For height == 1, it's genesis time. |
| `events` | [tendermint.abci.Event](#tendermint.abci.Event) | repeated | Events defines all the events emitted by processing a transaction. Note, these events include those emitted by processing all the messages and those emitted from the ante handler. Whereas Logs contains the events, with additional metadata, emitted only by processing the messages.

Since: cosmos-sdk 0.42.11, 0.44.5, 0.45 |



Expand Down Expand Up @@ -1899,6 +1906,36 @@ QuerySupplyOfResponse is the response type for the Query/SupplyOf RPC method.



<a name="cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetRequest"></a>

### QuerySupplyOfWithoutOffsetRequest
QuerySupplyOfWithoutOffsetRequest is the request type for the Query/SupplyOfWithoutOffset RPC method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `denom` | [string](#string) | | denom is the coin denom to query balances for. |






<a name="cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetResponse"></a>

### QuerySupplyOfWithoutOffsetResponse
QuerySupplyOfWithoutOffsetResponse is the response type for the Query/SupplyOfWithoutOffset RPC method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | amount is the supply of the coin. |






<a name="cosmos.bank.v1beta1.QueryTotalSupplyRequest"></a>

### QueryTotalSupplyRequest
Expand Down Expand Up @@ -1935,6 +1972,43 @@ Since: cosmos-sdk 0.43 |




<a name="cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetRequest"></a>

### QueryTotalSupplyWithoutOffsetRequest
QueryTotalSupplyWithoutOffsetRequest is the request type for the Query/TotalSupplyWithoutOffset RPC
method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `pagination` | [cosmos.base.query.v1beta1.PageRequest](#cosmos.base.query.v1beta1.PageRequest) | | pagination defines an optional pagination for the request.

Since: cosmos-sdk 0.43 |






<a name="cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetResponse"></a>

### QueryTotalSupplyWithoutOffsetResponse
QueryTotalSupplyWithoutOffsetResponse is the response type for the Query/TotalSupplyWithoutOffset RPC
method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `supply` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | supply is the supply of the coins |
| `pagination` | [cosmos.base.query.v1beta1.PageResponse](#cosmos.base.query.v1beta1.PageResponse) | | pagination defines the pagination in the response.

Since: cosmos-sdk 0.43 |





<!-- end messages -->

<!-- end enums -->
Expand All @@ -1953,6 +2027,8 @@ Query defines the gRPC querier service.
| `AllBalances` | [QueryAllBalancesRequest](#cosmos.bank.v1beta1.QueryAllBalancesRequest) | [QueryAllBalancesResponse](#cosmos.bank.v1beta1.QueryAllBalancesResponse) | AllBalances queries the balance of all coins for a single account. | GET|/cosmos/bank/v1beta1/balances/{address}|
| `TotalSupply` | [QueryTotalSupplyRequest](#cosmos.bank.v1beta1.QueryTotalSupplyRequest) | [QueryTotalSupplyResponse](#cosmos.bank.v1beta1.QueryTotalSupplyResponse) | TotalSupply queries the total supply of all coins. | GET|/cosmos/bank/v1beta1/supply|
| `SupplyOf` | [QuerySupplyOfRequest](#cosmos.bank.v1beta1.QuerySupplyOfRequest) | [QuerySupplyOfResponse](#cosmos.bank.v1beta1.QuerySupplyOfResponse) | SupplyOf queries the supply of a single coin. | GET|/cosmos/bank/v1beta1/supply/{denom}|
| `TotalSupplyWithoutOffset` | [QueryTotalSupplyWithoutOffsetRequest](#cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetRequest) | [QueryTotalSupplyWithoutOffsetResponse](#cosmos.bank.v1beta1.QueryTotalSupplyWithoutOffsetResponse) | TotalSupplyWithoutOffset queries the total supply of all coins. | GET|/cosmos/bank/v1beta1/supply_without_offset|
| `SupplyOfWithoutOffset` | [QuerySupplyOfWithoutOffsetRequest](#cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetRequest) | [QuerySupplyOfWithoutOffsetResponse](#cosmos.bank.v1beta1.QuerySupplyOfWithoutOffsetResponse) | SupplyOf queries the supply of a single coin. | GET|/cosmos/bank/v1beta1/supply_without_offset/{denom}|
| `Params` | [QueryParamsRequest](#cosmos.bank.v1beta1.QueryParamsRequest) | [QueryParamsResponse](#cosmos.bank.v1beta1.QueryParamsResponse) | Params queries the parameters of x/bank module. | GET|/cosmos/bank/v1beta1/params|
| `DenomMetadata` | [QueryDenomMetadataRequest](#cosmos.bank.v1beta1.QueryDenomMetadataRequest) | [QueryDenomMetadataResponse](#cosmos.bank.v1beta1.QueryDenomMetadataResponse) | DenomsMetadata queries the client metadata of a given coin denomination. | GET|/cosmos/bank/v1beta1/denoms_metadata/{denom}|
| `DenomsMetadata` | [QueryDenomsMetadataRequest](#cosmos.bank.v1beta1.QueryDenomsMetadataRequest) | [QueryDenomsMetadataResponse](#cosmos.bank.v1beta1.QueryDenomsMetadataResponse) | DenomsMetadata queries the client metadata for all registered coin denominations. | GET|/cosmos/bank/v1beta1/denoms_metadata|
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,6 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA=
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/osmosis-labs/iavl v0.17.3-fast.1.0.20220207021926-5f889f6b44d5 h1:Yxj5CB5vnE2BSWbfWlDFzmyccDQmaFVGTms5RlD2Rgo=
github.com/osmosis-labs/iavl v0.17.3-fast.1.0.20220207021926-5f889f6b44d5/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/iavl v0.17.3-fast.4 h1:P6872Aq9Q+X2nCQFMpUb+VCP0rNYZPW5OKa4tTnzR+A=
github.com/osmosis-labs/iavl v0.17.3-fast.4/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/iavl v0.17.3-osmo-v1 h1:orHUut98Miu2+bsFiNZJ29B3ogrbiBbQpti94L2w3Z4=
github.com/osmosis-labs/iavl v0.17.3-osmo-v1/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/iavl v0.17.3-osmo-v3 h1:q2Qv3+DK52w5b68I96VApHI05jBu7HeQK/YDttKh/jY=
github.com/osmosis-labs/iavl v0.17.3-osmo-v3/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/tm-db v0.6.5-0.20210911033928-ba9154613417 h1:otchJDd2SjFWfs7Tse3ULblGcVWqMJ50BE02XCaqXOo=
Expand Down
57 changes: 52 additions & 5 deletions proto/cosmos/bank/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ service Query {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply/{denom}";
}

// TotalSupplyWithoutOffset queries the total supply of all coins.
rpc TotalSupplyWithoutOffset(QueryTotalSupplyWithoutOffsetRequest) returns (QueryTotalSupplyWithoutOffsetResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply_without_offset";
}

// SupplyOf queries the supply of a single coin.
rpc SupplyOfWithoutOffset(QuerySupplyOfWithoutOffsetRequest) returns (QuerySupplyOfWithoutOffsetResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/supply_without_offset/{denom}";
}

// Params queries the parameters of x/bank module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/params";
Expand All @@ -49,7 +59,7 @@ service Query {

// QueryBalanceRequest is the request type for the Query/Balance RPC method.
message QueryBalanceRequest {
option (gogoproto.equal) = false;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// address is the address to query balances for.
Expand All @@ -67,7 +77,7 @@ message QueryBalanceResponse {

// QueryBalanceRequest is the request type for the Query/AllBalances RPC method.
message QueryAllBalancesRequest {
option (gogoproto.equal) = false;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// address is the address to query balances for.
Expand All @@ -82,7 +92,7 @@ message QueryAllBalancesRequest {
message QueryAllBalancesResponse {
// balances is the balances of all the coins.
repeated cosmos.base.v1beta1.Coin balances = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
Expand All @@ -91,7 +101,7 @@ message QueryAllBalancesResponse {
// QueryTotalSupplyRequest is the request type for the Query/TotalSupply RPC
// method.
message QueryTotalSupplyRequest {
option (gogoproto.equal) = false;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// pagination defines an optional pagination for the request.
Expand All @@ -105,7 +115,7 @@ message QueryTotalSupplyRequest {
message QueryTotalSupplyResponse {
// supply is the supply of the coins
repeated cosmos.base.v1beta1.Coin supply = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];

// pagination defines the pagination in the response.
//
Expand All @@ -125,6 +135,43 @@ message QuerySupplyOfResponse {
cosmos.base.v1beta1.Coin amount = 1 [(gogoproto.nullable) = false];
}

// QueryTotalSupplyWithoutOffsetRequest is the request type for the Query/TotalSupplyWithoutOffset RPC
// method.
message QueryTotalSupplyWithoutOffsetRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// pagination defines an optional pagination for the request.
//
// Since: cosmos-sdk 0.43
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryTotalSupplyWithoutOffsetResponse is the response type for the Query/TotalSupplyWithoutOffset RPC
// method
message QueryTotalSupplyWithoutOffsetResponse {
// supply is the supply of the coins
repeated cosmos.base.v1beta1.Coin supply = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];

// pagination defines the pagination in the response.
//
// Since: cosmos-sdk 0.43
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySupplyOfWithoutOffsetRequest is the request type for the Query/SupplyOfWithoutOffset RPC method.
message QuerySupplyOfWithoutOffsetRequest {
// denom is the coin denom to query balances for.
string denom = 1;
}

// QuerySupplyOfWithoutOffsetResponse is the response type for the Query/SupplyOfWithoutOffset RPC method.
message QuerySupplyOfWithoutOffsetResponse {
// amount is the supply of the coin.
cosmos.base.v1beta1.Coin amount = 1 [(gogoproto.nullable) = false];
}

// QueryParamsRequest defines the request type for querying x/bank parameters.
message QueryParamsRequest {}

Expand Down
16 changes: 8 additions & 8 deletions proto/cosmos/base/tendermint/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ message GetNodeInfoResponse {

// VersionInfo is the type for the GetNodeInfoResponse message.
message VersionInfo {
string name = 1;
string app_name = 2;
string version = 3;
string git_commit = 4;
string build_tags = 5;
string go_version = 6;
repeated Module build_deps = 7;
string name = 1;
string app_name = 2;
string version = 3;
string git_commit = 4;
string build_tags = 5;
string go_version = 6;
repeated Module build_deps = 7;
// Since: cosmos-sdk 0.43
string cosmos_sdk_version = 8;
string cosmos_sdk_version = 8;
}

// Module is the type for VersionInfo
Expand Down
2 changes: 1 addition & 1 deletion proto/cosmos/gov/v1beta1/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ message Vote {
// Deprecated: Prefer to use `options` instead. This field is set in queries
// if and only if `len(options) == 1` and that option has weight 1. In all
// other cases, this field will default to VOTE_OPTION_UNSPECIFIED.
VoteOption option = 3 [deprecated = true];
VoteOption option = 3 [deprecated = true];
// Since: cosmos-sdk 0.43
repeated WeightedVoteOption options = 4 [(gogoproto.nullable) = false];
}
Expand Down
31 changes: 29 additions & 2 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (k BaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalances
// TotalSupply implements the Query/TotalSupply gRPC method
func (k BaseKeeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
totalSupply, pageRes, err := k.GetPaginatedTotalSupplyWithOffsets(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -98,11 +98,38 @@ func (k BaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest)
}

ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)
supply := k.GetSupplyWithOffset(ctx, req.Denom)

return &types.QuerySupplyOfResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

// TotalSupply implements the Query/TotalSupplyWithoutOffset gRPC method
func (k BaseKeeper) TotalSupplyWithoutOffset(ctx context.Context, req *types.QueryTotalSupplyWithoutOffsetRequest) (*types.QueryTotalSupplyWithoutOffsetResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryTotalSupplyWithoutOffsetResponse{Supply: totalSupply, Pagination: pageRes}, nil
}

// SupplyOf implements the Query/SupplyOf gRPC method
func (k BaseKeeper) SupplyOfWithoutOffset(c context.Context, req *types.QuerySupplyOfWithoutOffsetRequest) (*types.QuerySupplyOfWithoutOffsetResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.Denom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid denom")
}

ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)

return &types.QuerySupplyOfWithoutOffsetResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

// Params implements the gRPC service handler for querying x/bank parameters.
func (k BaseKeeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
Expand Down
31 changes: 31 additions & 0 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupply() {
suite.Require().NotNil(res)

suite.Require().Equal(expectedTotalSupply, res.Supply)

// test total supply query with supply offset
app.BankKeeper.AddSupplyOffset(ctx, "test", sdk.NewInt(-100000000))
res, err = queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
suite.Require().NotNil(res)

suite.Require().Equal(expectedTotalSupply.Sub(sdk.NewCoins(sdk.NewInt64Coin("test", 100000000))), res.Supply)

// make sure query without offsets hasn't changed
res2, err := queryClient.TotalSupplyWithoutOffset(gocontext.Background(), &types.QueryTotalSupplyWithoutOffsetRequest{})
suite.Require().NoError(err)
suite.Require().NotNil(res2)

suite.Require().Equal(expectedTotalSupply, res2.Supply)

}

func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
Expand All @@ -118,6 +134,21 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
suite.Require().NotNil(res)

suite.Require().Equal(test1Supply, res.Amount)

// test total supply of query with supply offset
app.BankKeeper.AddSupplyOffset(ctx, "test1", sdk.NewInt(-1000000))
res, err = queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{Denom: test1Supply.Denom})
suite.Require().NoError(err)
suite.Require().NotNil(res)

suite.Require().Equal(test1Supply.Sub(sdk.NewInt64Coin("test1", 1000000)), res.Amount)

// make sure query without offsets hasn't changed
res2, err := queryClient.SupplyOfWithoutOffset(gocontext.Background(), &types.QuerySupplyOfWithoutOffsetRequest{Denom: test1Supply.Denom})
suite.Require().NoError(err)
suite.Require().NotNil(res2)

suite.Require().Equal(test1Supply, res2.Amount)
}

func (suite *IntegrationTestSuite) TestQueryParams() {
Expand Down
5 changes: 5 additions & 0 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ type Keeper interface {
HasSupply(ctx sdk.Context, denom string) bool
GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool)
GetSupplyOffset(ctx sdk.Context, denom string) sdk.Int
AddSupplyOffset(ctx sdk.Context, denom string, offsetAmount sdk.Int)
GetSupplyWithOffset(ctx sdk.Context, denom string) sdk.Coin
GetPaginatedTotalSupplyWithOffsets(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupplyWithOffsets(ctx sdk.Context, cb func(sdk.Coin) bool)
GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool)
SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata)
IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metadata) bool)
Expand Down
Loading