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: Implement CLI query endpoints and add AdvanceEpoch msg #115

Merged
merged 7 commits into from
Sep 16, 2021
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ endif
ldflags += $(LDFLAGS)
ldflags := $(strip $(ldflags))

testing_ldflags = -X github.com/tendermint/farming/x/farming/keeper.enableAdvanceEpoch=true

BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)'
TESTING_BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags) $(testing_ldflags)'

all: tools install lint

Expand All @@ -87,6 +90,9 @@ build-linux: go.sum
install: go.sum
go install $(BUILD_FLAGS) ./cmd/farmingd

install-testing: go.sum
go install $(TESTING_BUILD_FLAGS) ./cmd/farmingd

build-reproducible: go.sum
$(DOCKER) rm latest-build || true
$(DOCKER) run --volume=$(CURDIR):/sources:ro \
Expand Down
2 changes: 1 addition & 1 deletion proto/tendermint/farming/v1beta1/farming.proto
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ message QueuedStaking {
string amount = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}

message TotalStaking {
message TotalStakings {
option (gogoproto.goproto_getters) = false;

string amount = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
Expand Down
43 changes: 43 additions & 0 deletions proto/tendermint/farming/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "google/api/annotations.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/tendermint/farming/x/farming/types";

Expand All @@ -27,6 +28,18 @@ service Query {
rpc Plan(QueryPlanRequest) returns (QueryPlanResponse) {
option (google.api.http).get = "/cosmos/farming/v1beta1/plans/{plan_id}";
}

rpc Stakings(QueryStakingsRequest) returns (QueryStakingsResponse) {
option (google.api.http).get = "/cosmos/farming/v1beta1/stakings/{farmer}";
}

rpc TotalStakings(QueryTotalStakingsRequest) returns (QueryTotalStakingsResponse) {
option (google.api.http).get = "/cosmos/farming/v1beta1/total_stakings/{staking_coin_denom}";
}

rpc Rewards(QueryRewardsRequest) returns (QueryRewardsResponse) {
option (google.api.http).get = "/cosmos/farming/v1beta1/rewards/{farmer}";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
Expand Down Expand Up @@ -62,3 +75,33 @@ message QueryPlanRequest {
message QueryPlanResponse {
google.protobuf.Any plan = 1 [(cosmos_proto.accepts_interface) = "PlanI"];
}

message QueryStakingsRequest {
string farmer = 1;
string staking_coin_denom = 2;
}

message QueryStakingsResponse {
repeated cosmos.base.v1beta1.Coin staked_coins = 1
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
repeated cosmos.base.v1beta1.Coin queued_coins = 2
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
}

message QueryTotalStakingsRequest {
string staking_coin_denom = 1;
}

message QueryTotalStakingsResponse {
string amount = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}

message QueryRewardsRequest {
string farmer = 1;
string staking_coin_denom = 2;
}

message QueryRewardsResponse {
repeated cosmos.base.v1beta1.Coin rewards = 1
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false];
}
17 changes: 16 additions & 1 deletion proto/tendermint/farming/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ service Msg {
// Unstake defines a method for unstaking coins from the farming plan
rpc Unstake(MsgUnstake) returns (MsgUnstakeResponse);

// harvest defines a method for claiming farming rewards
// Harvest defines a method for claiming farming rewards
rpc Harvest(MsgHarvest) returns (MsgHarvestResponse);

// AdvanceEpoch defines a method for advancing epoch by one, just for testing purpose
// and shouldn't be used in real world
rpc AdvanceEpoch(MsgAdvanceEpoch) returns (MsgAdvanceEpochResponse);
}

// MsgCreateFixedAmountPlan defines a SDK message for creating a new fixed
Expand Down Expand Up @@ -156,3 +160,14 @@ message MsgHarvest {

// MsgHarvestResponse defines the Msg/MsgHarvestResponse response type.
message MsgHarvestResponse {}

// MsgAdvanceEpoch defines a message to advance epoch by one.
message MsgAdvanceEpoch {
option (gogoproto.goproto_getters) = false;

// requester defines the bech32-encoded address of the requester
string requester = 1;
}

// MsgAdvanceEpochResponse defines the Msg/AdvanceEpoch response type.
message MsgAdvanceEpochResponse {}
16 changes: 16 additions & 0 deletions x/farming/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ func flagSetPlans() *flag.FlagSet {
return fs
}

func flagSetStakings() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

fs.String(FlagStakingCoinDenom, "", "The staking coin denom")

return fs
}

func flagSetRewards() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

fs.String(FlagStakingCoinDenom, "", "The staking coin denom")

return fs
}

func flagSetHarvest() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)

Expand Down
149 changes: 149 additions & 0 deletions x/farming/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryParams(),
GetCmdQueryPlans(),
GetCmdQueryPlan(),
GetCmdQueryStakings(),
GetCmdQueryTotalStakings(),
GetCmdQueryRewards(),
)

return farmingQueryCmd
Expand Down Expand Up @@ -188,3 +191,149 @@ $ %s query %s plan

return cmd
}

func GetCmdQueryStakings() *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()

cmd := &cobra.Command{
Use: "stakings [farmer]",
Args: cobra.ExactArgs(1),
Short: "Query stakings by a farmer",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all stakings by a farmer.

Optionally restrict coins for a staking coin denom.

Example:
$ %s query %s stakings %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
$ %s query %s stakings %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --staking-coin-denom poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
`,
version.AppName, types.ModuleName, bech32PrefixAccAddr,
version.AppName, types.ModuleName, bech32PrefixAccAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

farmerAcc, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

stakingCoinDenom, _ := cmd.Flags().GetString(FlagStakingCoinDenom)

resp, err := queryClient.Stakings(cmd.Context(), &types.QueryStakingsRequest{
Farmer: farmerAcc.String(),
StakingCoinDenom: stakingCoinDenom,
})
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

cmd.Flags().AddFlagSet(flagSetStakings())
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func GetCmdQueryTotalStakings() *cobra.Command {
cmd := &cobra.Command{
Use: "total-stakings [staking-coin-denom]",
Args: cobra.ExactArgs(1),
Short: "Query total staking amount for a staking coin denom",
Long: strings.TrimSpace(
fmt.Sprintf(`Query total staking amount for a staking coin denom.

Example:
$ %s query %s total-stakings poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
`,
version.AppName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

stakingCoinDenom := args[0]
if err := sdk.ValidateDenom(stakingCoinDenom); err != nil {
return err
}

resp, err := queryClient.TotalStakings(cmd.Context(), &types.QueryTotalStakingsRequest{
StakingCoinDenom: stakingCoinDenom,
})
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func GetCmdQueryRewards() *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()

cmd := &cobra.Command{
Use: "rewards [farmer]",
Args: cobra.ExactArgs(1),
Short: "Query rewards for a farmer",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all rewards for a farmer.

Optionally restrict rewards for a staking coin denom.

Example:
$ %s query %s rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
$ %s query %s rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --staking-coin-denom poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
`,
version.AppName, types.ModuleName, bech32PrefixAccAddr,
version.AppName, types.ModuleName, bech32PrefixAccAddr,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

farmerAcc, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

stakingCoinDenom, _ := cmd.Flags().GetString(FlagStakingCoinDenom)

resp, err := queryClient.Rewards(cmd.Context(), &types.QueryRewardsRequest{
Farmer: farmerAcc.String(),
StakingCoinDenom: stakingCoinDenom,
})
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

cmd.Flags().AddFlagSet(flagSetRewards())
flags.AddQueryFlagsToCmd(cmd)

return cmd
}
28 changes: 28 additions & 0 deletions x/farming/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/tendermint/farming/x/farming/keeper"
"github.com/tendermint/farming/x/farming/types"
)

Expand All @@ -38,6 +39,9 @@ func GetTxCmd() *cobra.Command {
NewUnstakeCmd(),
NewHarvestCmd(),
)
if keeper.EnableAdvanceEpoch {
farmingTxCmd.AddCommand(NewAdvanceEpochCmd())
}

return farmingTxCmd
}
Expand Down Expand Up @@ -302,6 +306,30 @@ $ %s tx %s harvest --staking-coin-denoms="poolD35A0CC16EE598F90B044CE296A405BA9C
return cmd
}

func NewAdvanceEpochCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "advance-epoch",
Args: cobra.NoArgs,
Short: "advance epoch by one to simulate reward distribution",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

requesterAcc := clientCtx.GetFromAddress()

msg := types.NewMsgAdvanceEpoch(requesterAcc)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// GetCmdSubmitPublicPlanProposal implements a command handler for submitting a public farming plan transaction to create, update, and delete plan.
func GetCmdSubmitPublicPlanProposal() *cobra.Command {
cmd := &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion x/farming/client/testutil/cli_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testutil
import (
"fmt"

dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -17,7 +19,6 @@ import (

farmingapp "github.com/tendermint/farming/app"
farmingcli "github.com/tendermint/farming/x/farming/client/cli"
dbm "github.com/tendermint/tm-db"
)

// NewConfig returns config that defines the necessary testing requirements
Expand Down
Loading