Skip to content

Commit

Permalink
Implement cli (#4)
Browse files Browse the repository at this point in the history
* implement cli for nft module and add ability to get nfts by holder address

* separate store logic with query
  • Loading branch information
MarkCherepovskyi authored Jul 29, 2024
1 parent 6694f7a commit bd5dfe9
Show file tree
Hide file tree
Showing 23 changed files with 2,537 additions and 213 deletions.
3 changes: 2 additions & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions proto/cosmos/nft/nft.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ message NFT {
string denom = 10;
}

message Owner {
string address = 1;
string nft_address = 2;
}

44 changes: 42 additions & 2 deletions proto/cosmos/nft/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/nft/types";
// Query defines the gRPC querier service.
service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/nft/params";
option (google.api.http).get = "/cosmos/nfts/params";
}

rpc GetAllNFTs(QueryAllNFTs) returns (QueryAllNFTsResponse) {
option (google.api.http).get = "/cosmos/nfts";
}

rpc GetNFTByAddress(QueryNFTByAddress) returns (QueryNFTByAddressResponse) {
option (google.api.http).get = "/cosmos/nft/{address}";
option (google.api.http).get = "/cosmos/nfts/{address}";
}

rpc GetAllOwners(QueryAllOwners) returns (QueryAllOwnersResponse) {
option (google.api.http).get = "/cosmos/owners";
}

rpc GetAllNFTsByOwner(QueryAllNFTsByOwner) returns (QueryAllNFTsByOwnerResponse) {
option (google.api.http).get = "/cosmos/owners/{owner}";
}
}

Expand All @@ -39,4 +51,32 @@ message QueryNFTByAddress {

message QueryNFTByAddressResponse {
cosmos.nft.NFT nft = 1;
}

message QueryAllNFTs {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllNFTsResponse {
repeated cosmos.nft.NFT nft = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllNFTsByOwner {
string owner = 1; // address of owner
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllNFTsByOwnerResponse {
repeated cosmos.nft.NFT nft = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllOwners {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllOwnersResponse {
repeated string owner = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
16 changes: 8 additions & 8 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ type SimApp struct {
BankKeeper bankkeeper.Keeper
AccumulatorKeeper accumulatorkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper stakingkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
Expand Down Expand Up @@ -287,16 +287,16 @@ func NewSimApp(
appCodec, keys[accumulatortypes.StoreKey], keys[accumulatortypes.MemStoreKey], app.AccountKeeper, app.BankKeeper)

app.MintKeeper = mintkeeper.NewKeeper(
appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), &app.StakingKeeper,
appCodec, keys[minttypes.StoreKey], app.GetSubspace(minttypes.ModuleName), app.StakingKeeper,
app.AccountKeeper, app.BankKeeper, app.AccumulatorKeeper, authtypes.FeeCollectorName,
)

app.DistrKeeper = distrkeeper.NewKeeper(
appCodec, keys[distrtypes.StoreKey], app.GetSubspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&app.StakingKeeper, app.NFTKeeper, authtypes.FeeCollectorName,
app.StakingKeeper, app.NFTKeeper, authtypes.FeeCollectorName,
)
app.SlashingKeeper = slashingkeeper.NewKeeper(
appCodec, keys[slashingtypes.StoreKey], &app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
appCodec, keys[slashingtypes.StoreKey], app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName),
)
app.CrisisKeeper = crisiskeeper.NewKeeper(
app.GetSubspace(crisistypes.ModuleName), invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName,
Expand All @@ -306,7 +306,7 @@ func NewSimApp(

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *app.StakingKeeper.SetHooks(
app.StakingKeeper = app.StakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

Expand Down Expand Up @@ -338,7 +338,7 @@ func NewSimApp(
*/
govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
app.StakingKeeper, govRouter, app.MsgServiceRouter(), govConfig,
)

app.GovKeeper = *govKeeper.SetHooks(
Expand All @@ -349,7 +349,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
appCodec, keys[evidencetypes.StoreKey], app.StakingKeeper, app.SlashingKeeper,
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand Down Expand Up @@ -379,7 +379,7 @@ func NewSimApp(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
staking.NewAppModule(appCodec, *app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
params.NewAppModule(app.ParamsKeeper),
Expand Down
2 changes: 1 addition & 1 deletion simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (app *SimApp) ExportAppStateAndValidators(
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
validators, err := staking.WriteValidators(ctx, *app.StakingKeeper)
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Expand Down
3 changes: 0 additions & 3 deletions x/accumulator/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand()
cmd.AddCommand()
cmd.AddCommand()
cmd.AddCommand()
// this line is used by starport scaffolding # 1

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (k Keeper) CalculateDelegationRewards(ctx sdk.Context, val stakingtypes.Val

wrapStake := func(stake sdk.Dec) sdk.Dec {
if isNFTstake {
return stake.Mul(params.NftProposerReward)
return stake.Add(stake.Mul(params.NftProposerReward))
}
return stake
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (k Keeper) IncrementValidatorPeriod(ctx sdk.Context, val stakingtypes.Valid
params := k.GetParams(ctx)
for _, del := range k.stakingKeeper.GetValidatorDelegations(ctx, val.GetOperator()) {
if _, found := k.nftKeeper.GetNFT(ctx, del.GetDelegatorAddr().String()); found {
tokens.Add(del.Amount.Mul(params.NftProposerReward))
tokens.Add(del.Amount.Add(del.Amount.Mul(params.NftProposerReward)))
continue
}

Expand Down
2 changes: 1 addition & 1 deletion x/nft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// update vesting state for each nft
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
for _, nft := range k.GetAllNFT(ctx) {
for _, nft := range k.GetNFTs(ctx) {
if ctx.BlockTime().Unix()-nft.LastVestingTime < nft.VestingPeriod {
return
}
Expand Down
4 changes: 3 additions & 1 deletion x/nft/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(
CmdQueryParams(),
CmdQueryNFTByAddress(),
CmdDerivePoolAddress(),
CmdQueryAllNft(),
CmdQueryAllOwners(),
CmdQueryNFTsByOwner(),
)
return cmd
}
26 changes: 0 additions & 26 deletions x/nft/client/cli/query_derive_address.go

This file was deleted.

30 changes: 29 additions & 1 deletion x/nft/client/cli/query_nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func CmdQueryNFTByAddress() *cobra.Command {
cmd := &cobra.Command{
Use: "grt-nft",
Use: "nft",
Short: "Query the nft by address",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
Expand All @@ -35,3 +35,31 @@ func CmdQueryNFTByAddress() *cobra.Command {

return cmd
}

func CmdQueryAllNft() *cobra.Command {
cmd := &cobra.Command{
Use: "nfts",
Short: "Query the all nft",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryAllNFTs{}

res, err := queryClient.GetAllNFTs(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
65 changes: 65 additions & 0 deletions x/nft/client/cli/query_owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cli

import (
"context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/nft/types"
"github.com/spf13/cobra"
)

func CmdQueryNFTsByOwner() *cobra.Command {
cmd := &cobra.Command{
Use: "owner",
Short: "Query the all nfts by owner address",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryAllNFTsByOwner{}

res, err := queryClient.GetAllNFTsByOwner(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdQueryAllOwners() *cobra.Command {
cmd := &cobra.Command{
Use: "owners",
Short: "Query the all NFT holders",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryAllOwners{}

res, err := queryClient.GetAllOwners(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit bd5dfe9

Please sign in to comment.