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: added denom-owners grpc_web api and cli query #2508

Merged
merged 10 commits into from
Apr 29, 2024
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func New(

app.NFTKeeper = nftkeeper.NewKeeper(keys[nftkeeper.StoreKey], appCodec, app.AccountKeeper, app.BankKeeper)

app.UGovKeeperB = ugovkeeper.NewBuilder(appCodec, keys[ugov.ModuleName])
app.UGovKeeperB = ugovkeeper.NewBuilder(appCodec, keys[ugov.ModuleName], app.BankKeeper)

app.OracleKeeper = oraclekeeper.NewKeeper(
appCodec,
Expand Down
37 changes: 37 additions & 0 deletions proto/umee/ugov/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "amino/amino.proto";
gsk967 marked this conversation as resolved.
Show resolved Hide resolved

import "umee/ugov/v1/ugov.proto";

Expand Down Expand Up @@ -34,6 +36,41 @@ service Query {
rpc InflationCycleEnd(QueryInflationCycleEnd) returns (QueryInflationCycleEndResponse) {
option (google.api.http).get = "/umee/ugov/v1/inflation_cycle_end";
}

// Token Balances queries for all account addresses that own a particular token
// denomination.
rpc TokenBalances(QueryTokenBalances) returns (QueryTokenBalancesResponse){
option (google.api.http).get = "/umee/ugov/v1/token_balances";
}
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
}

// which queries for a paginated set of all account holders of a particular
// denomination.
message QueryTokenBalances{
// denom defines the coin denomination to query all account holders for.
string denom = 1;
int64 height = 2;
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 3;
}

// QueryDenomOwnersResponse defines the RPC response of a TokenBalances RPC query.
message QueryTokenBalancesResponse {
repeated TokenBalance token_balances = 1;
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// TokenBalance defines structure representing an account that owns or holds a
// particular denominated token. It contains the account address and account
// balance of the denominated token.
//
message TokenBalance {
// address defines the address that owns a particular denomination.
string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// balance is the balance of the denominated coin for an account.
cosmos.base.v1beta1.Coin balance = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
}

// QueryMinGasPrice is a request type.
Expand Down
31 changes: 31 additions & 0 deletions x/ugov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
QueryInflationParams(),
QueryInflationCyleEnd(),
QueryEmergencyGroup(),
QueryTokenBalances(),

Check warning on line 28 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L28

Added line #L28 was not covered by tests
)

return cmd
Expand Down Expand Up @@ -121,3 +122,33 @@

return cmd
}

// QueryTokenBalances creates the Query/TokenBalances CLI.
func QueryTokenBalances() *cobra.Command {
cmd := &cobra.Command{
Use: "token-balances [denom]",
Args: cobra.ExactArgs(1),
Short: "Queries for all account addresses that own a particular token denomination.",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err

Check warning on line 135 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L127-L135

Added lines #L127 - L135 were not covered by tests
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err

Check warning on line 139 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L137-L139

Added lines #L137 - L139 were not covered by tests
}
queryClient := ugov.NewQueryClient(clientCtx)
resp, err := queryClient.TokenBalances(cmd.Context(), &ugov.QueryTokenBalances{
Denom: args[0],
Pagination: pageReq,
})
return cli.PrintOrErr(resp, err, clientCtx)

Check warning on line 146 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L141-L146

Added lines #L141 - L146 were not covered by tests
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "token-balances")

Check warning on line 151 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L150-L151

Added lines #L150 - L151 were not covered by tests

return cmd

Check warning on line 153 in x/ugov/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/client/cli/query.go#L153

Added line #L153 was not covered by tests
}
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion x/ugov/keeper/intest/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

"github.com/umee-network/umee/v6/tests/tsdk"
"github.com/umee-network/umee/v6/x/ugov"
Expand All @@ -20,7 +21,7 @@ func MkKeeper(t *testing.T) (*sdk.Context, ugov.Keeper) {
ugov.RegisterInterfaces(ir)
cdc := codec.NewProtoCodec(ir)
storeKey := storetypes.NewMemoryStoreKey(ugov.StoreKey)
kb := keeper.NewBuilder(cdc, storeKey)
kb := keeper.NewBuilder(cdc, storeKey, bkeeper.BaseKeeper{})
ctx, _ := tsdk.NewCtxOneStore(t, storeKey)
return &ctx, kb.Keeper(&ctx)
}
24 changes: 15 additions & 9 deletions x/ugov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,35 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/keeper"

"github.com/umee-network/umee/v6/x/ugov"
)

var _ ugov.Keeper = Keeper{}

// Builder constructs Keeper by perparing all related dependencies (notably the store).
type Builder struct {
storeKey storetypes.StoreKey
Cdc codec.BinaryCodec
storeKey storetypes.StoreKey
Cdc codec.BinaryCodec
BankKeeper keeper.BaseKeeper
}

func NewBuilder(
cdc codec.BinaryCodec, key storetypes.StoreKey,
cdc codec.BinaryCodec, key storetypes.StoreKey, bk keeper.BaseKeeper,
) Builder {
return Builder{
Cdc: cdc,
storeKey: key,
Cdc: cdc,
storeKey: key,
BankKeeper: bk,
}
}

func (kb Builder) Keeper(ctx *sdk.Context) ugov.Keeper {
return Keeper{
store: ctx.KVStore(kb.storeKey),
cdc: kb.Cdc,
store: ctx.KVStore(kb.storeKey),
cdc: kb.Cdc,
BankKeeper: kb.BankKeeper,
}
}

Expand All @@ -38,6 +43,7 @@ func (kb Builder) EmergencyGroup(ctx *sdk.Context) ugov.WithEmergencyGroup { ret

// Keeper provides a light interface for module data access and transformation
type Keeper struct {
store sdk.KVStore
cdc codec.BinaryCodec
store sdk.KVStore
cdc codec.BinaryCodec
BankKeeper keeper.BaseKeeper
}
28 changes: 28 additions & 0 deletions x/ugov/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
context "context"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/umee-network/umee/v6/x/ugov"
)

Expand Down Expand Up @@ -49,3 +51,29 @@
cycleEndTime := q.Keeper(&sdkCtx).InflationCycleEnd()
return &ugov.QueryInflationCycleEndResponse{End: &cycleEndTime}, nil
}

// TokenBalances implements ugov.QueryServer.
func (q Querier) TokenBalances(ctx context.Context, req *ugov.QueryTokenBalances) (*ugov.QueryTokenBalancesResponse,
error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
if req.Height != 0 {
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
sdkCtx = sdkCtx.WithBlockHeight(req.Height)

Check warning on line 60 in x/ugov/keeper/query_server.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/keeper/query_server.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}
resp, err := q.BankKeeper.DenomOwners(sdk.WrapSDKContext(sdkCtx), &banktypes.QueryDenomOwnersRequest{
Denom: req.Denom,
Pagination: req.Pagination,
})
if err != nil {
return nil, err

Check warning on line 67 in x/ugov/keeper/query_server.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/keeper/query_server.go#L62-L67

Added lines #L62 - L67 were not covered by tests
}

tb := make([]*ugov.TokenBalance, 0)
for _, v := range resp.DenomOwners {
tb = append(tb, &ugov.TokenBalance{
Address: v.Address,
Balance: v.Balance,
})

Check warning on line 75 in x/ugov/keeper/query_server.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/keeper/query_server.go#L70-L75

Added lines #L70 - L75 were not covered by tests
}

return &ugov.QueryTokenBalancesResponse{Pagination: resp.Pagination, TokenBalances: tb}, nil

Check warning on line 78 in x/ugov/keeper/query_server.go

View check run for this annotation

Codecov / codecov/patch

x/ugov/keeper/query_server.go#L78

Added line #L78 was not covered by tests
}
Loading
Loading