Skip to content

Commit

Permalink
added token-balances api and cli query
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Apr 24, 2024
1 parent 97af6be commit 2f29bc8
Show file tree
Hide file tree
Showing 7 changed files with 993 additions and 62 deletions.
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";

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";
}
}

// 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;
// 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 DenomOwner denom_owners = 1;
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// DenomOwner 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 DenomOwner {
// 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];
}

// 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 @@ func GetQueryCmd() *cobra.Command {
QueryInflationParams(),
QueryInflationCyleEnd(),
QueryEmergencyGroup(),
QueryTokenBalances(),
)

return cmd
Expand Down Expand Up @@ -121,3 +122,33 @@ func QueryInflationCyleEnd() *cobra.Command {

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
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := ugov.NewQueryClient(clientCtx)
resp, err := queryClient.TokenBalances(cmd.Context(), &ugov.QueryTokenBalances{
Denom: args[0],
Pagination: pageReq,
})
return cli.PrintOrErr(resp, err, clientCtx)
},
}

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

return cmd
}
23 changes: 14 additions & 9 deletions x/ugov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@ 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 +42,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 @@ import (
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 @@ func (q Querier) InflationCycleEnd(ctx context.Context, _ *ugov.QueryInflationCy
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 {
sdkCtx = sdkCtx.WithBlockHeight(req.Height)
}
resp, err := q.BankKeeper.DenomOwners(sdk.WrapSDKContext(sdkCtx), &banktypes.QueryDenomOwnersRequest{
Denom: req.Denom,
Pagination: req.Pagination,
})
if err != nil {
return nil, err
}

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

return &ugov.QueryTokenBalancesResponse{Pagination: resp.Pagination, DenomOwners: denomsOwners}, nil
}
Loading

0 comments on commit 2f29bc8

Please sign in to comment.