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

fix(2.2.7): improve commission when creating staker #25

Merged
merged 8 commits into from
Mar 27, 2023
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
12 changes: 9 additions & 3 deletions proto/kyve/stakers/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgStakePool defines a SDK message for staking in a pool.
// MsgCreateStaker defines a SDK message for creating a staker.
message MsgCreateStaker {
// creator ...
// creator is the address of the staker.
string creator = 1;
// amount ...
// amount is the initial self-stake of the staker.
uint64 amount = 2;
// commission is the percentage that is deducted from rewards before
// distributing the staker's delegators.
string commission = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// MsgStakePoolResponse defines the Msg/StakePool response type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,36 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)

func CmdCreateStaker() *cobra.Command {
cmd := &cobra.Command{
Use: "create-staker [amount]",
Use: "create-staker [amount] [commission]",
Short: "Broadcast message create-staker",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argAmount, err := cast.ToUint64E(args[0])
if err != nil {
return err
}

argCommission, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.MsgCreateStaker{
Creator: clientCtx.GetFromAddress().String(),
Amount: argAmount,
Creator: clientCtx.GetFromAddress().String(),
Amount: argAmount,
Commission: argCommission,
}

if err := msg.ValidateBasic(); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions x/stakers/keeper/msg_server_create_staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
// Every user can create a staker object with some stake. However,
// only if self_delegation + delegation is large enough to join a pool the staker
// is able to participate in the protocol
func (k msgServer) CreateStaker(goCtx context.Context, msg *types.MsgCreateStaker) (*types.MsgCreateStakerResponse, error) {
func (k msgServer) CreateStaker(
goCtx context.Context,
msg *types.MsgCreateStaker,
) (*types.MsgCreateStakerResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// Only create new stakers
Expand All @@ -26,7 +29,7 @@ func (k msgServer) CreateStaker(goCtx context.Context, msg *types.MsgCreateStake
// Create and append new staker to store
k.AppendStaker(ctx, types.Staker{
Address: msg.Creator,
Commission: types.DefaultCommission,
Commission: msg.Commission,
})

// Perform initial self delegation
Expand Down
13 changes: 8 additions & 5 deletions x/stakers/keeper/msg_server_create_staker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
delegationtypes "github.com/KYVENetwork/chain/x/delegation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -39,8 +40,9 @@ var _ = Describe("msg_server_create_staker.go", Ordered, func() {
It("Create a first new staker and delegate 100 $KYVE", func() {
// ACT
s.RunTxStakersSuccess(&stakerstypes.MsgCreateStaker{
Creator: i.STAKER_0,
Amount: 100 * i.KYVE,
Creator: i.STAKER_0,
Amount: 100 * i.KYVE,
Commission: sdk.MustNewDecFromStr("0.2"),
})

// ASSERT
Expand All @@ -56,7 +58,7 @@ var _ = Describe("msg_server_create_staker.go", Ordered, func() {
Expect(staker.Address).To(Equal(i.STAKER_0))
Expect(s.App().DelegationKeeper.GetDelegationAmount(s.Ctx(), i.STAKER_0)).To(Equal(100 * i.KYVE))
Expect(s.App().DelegationKeeper.GetDelegationAmountOfDelegator(s.Ctx(), i.STAKER_0, i.STAKER_0)).To(Equal(100 * i.KYVE))
Expect(staker.Commission).To(Equal(types.DefaultCommission))
Expect(staker.Commission).To(Equal(sdk.MustNewDecFromStr("0.2")))

Expect(staker.Moniker).To(BeEmpty())
Expect(staker.Logo).To(BeEmpty())
Expand All @@ -68,8 +70,9 @@ var _ = Describe("msg_server_create_staker.go", Ordered, func() {
It("Do an additional 50 $KYVE self delegation after staker has already delegated 100 $KYVE", func() {
// ARRANGE
s.RunTxStakersSuccess(&stakerstypes.MsgCreateStaker{
Creator: i.STAKER_0,
Amount: 100 * i.KYVE,
Creator: i.STAKER_0,
Amount: 100 * i.KYVE,
Commission: types.DefaultCommission,
})

// ACT
Expand Down
2 changes: 1 addition & 1 deletion x/stakers/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (

const MaxStakers = 50

var DefaultCommission = sdk.MustNewDecFromStr("0.9")
var DefaultCommission = sdk.MustNewDecFromStr("0.1")

// StakerKey returns the store Key to retrieve a Staker from the index fields
func StakerKey(staker string) []byte {
Expand Down
8 changes: 8 additions & 0 deletions x/stakers/types/message_create_staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/KYVENetwork/chain/util"
sdk "github.com/cosmos/cosmos-sdk/types"
errorsTypes "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -27,5 +28,12 @@ func (msg *MsgCreateStaker) ValidateBasic() error {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, "invalid amount")
}

if msg.Commission.IsNil() {
msg.Commission = DefaultCommission
}
if util.ValidatePercentage(msg.Commission) != nil {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, "invalid commission")
}

return nil
}
135 changes: 92 additions & 43 deletions x/stakers/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.