From 06a6e5b84117c006ee29eb700a3d1d839edb9237 Mon Sep 17 00:00:00 2001 From: Mark Cherepovskyi <88227619+MarkCherepovskyi@users.noreply.github.com> Date: Tue, 6 Aug 2024 18:08:16 +0000 Subject: [PATCH] fix rewards, vesting issues... (#7) --- proto/cosmos/accumulator/query.proto | 4 +- x/accumulator/abci.go | 5 ++- x/accumulator/client/cli/tx.go | 5 ++- x/accumulator/client/cli/tx_admin.go | 58 ++++++++++++++++++++++++++++ x/accumulator/types/query.pb.go | 55 +++++++++++++------------- x/accumulator/types/query.pb.gw.go | 4 +- x/distribution/keeper/delegation.go | 7 ++-- x/distribution/keeper/validator.go | 4 +- x/nft/abci.go | 6 +-- x/nft/client/cli/tx.go | 2 +- x/nft/types/message_undelegate.go | 2 +- x/nft/types/message_withdraw.go | 2 +- 12 files changed, 107 insertions(+), 47 deletions(-) create mode 100644 x/accumulator/client/cli/tx_admin.go diff --git a/proto/cosmos/accumulator/query.proto b/proto/cosmos/accumulator/query.proto index c2f6f0962..50bc60697 100644 --- a/proto/cosmos/accumulator/query.proto +++ b/proto/cosmos/accumulator/query.proto @@ -15,11 +15,11 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/accumulator/types"; service Query { // Parameters queries the parameters of the module. rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { - option (google.api.http).get = "/cosmos/cosmos-sdk/accumulator/params"; + option (google.api.http).get = "/cosmos/accumulator/params"; } rpc GetAdmins(QueryAdmins) returns (QueryAdminsResponse) { - option (google.api.http).get = "/cosmos/cosmos-sdk/accumulator/admins"; + option (google.api.http).get = "/cosmos/accumulator/admins"; } // this line is used by starport scaffolding # 2 } diff --git a/x/accumulator/abci.go b/x/accumulator/abci.go index 715d1cce6..cce1d91fd 100644 --- a/x/accumulator/abci.go +++ b/x/accumulator/abci.go @@ -21,10 +21,13 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) { } address, err := sdk.AccAddressFromBech32(admin.Address) if err != nil { - panic(err) + k.Logger(ctx).Error("failed to parse account", err.Error()) + return } + err = k.DistributeToAccount(ctx, types.AdminPoolName, sdk.NewCoins(sdk.NewCoin(admin.Denom, admin.RewardPerPeriod.Amount)), address) if err != nil { + k.Logger(ctx).Error("failed to distribute token to account", err.Error()) return } diff --git a/x/accumulator/client/cli/tx.go b/x/accumulator/client/cli/tx.go index af12285e1..37cffcc17 100644 --- a/x/accumulator/client/cli/tx.go +++ b/x/accumulator/client/cli/tx.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" - // "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/x/accumulator/types" ) @@ -25,7 +24,9 @@ func GetTxCmd() *cobra.Command { RunE: client.ValidateCmd, } - cmd.AddCommand() + cmd.AddCommand( + CmdNewAdmin(), + ) // this line is used by starport scaffolding # 1 return cmd diff --git a/x/accumulator/client/cli/tx_admin.go b/x/accumulator/client/cli/tx_admin.go new file mode 100644 index 000000000..7d1541ae0 --- /dev/null +++ b/x/accumulator/client/cli/tx_admin.go @@ -0,0 +1,58 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/spf13/cobra" + "strconv" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/x/accumulator/types" +) + +func CmdNewAdmin() *cobra.Command { + cmd := &cobra.Command{ + Use: "newadmin [from_key_or_address] [denom] [address] [rewards_per_period] [vesting_periods] [vesting_period]", + Short: "Init a new admin with vesting params", + Args: cobra.ExactArgs(6), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.Flags().Set(flags.FlagFrom, args[0]) + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + rewardPerPeriod, err := sdk.ParseCoinNormalized(args[3]) + if err != nil { + return err + } + + vestingPeriodsCount, err := strconv.ParseInt(args[4], 10, 64) + if err != nil { + return err + } + + vestingPeriod, err := strconv.ParseInt(args[5], 10, 64) + if err != nil { + return err + } + + msg := types.NewMsgAddAdmin( + clientCtx.GetFromAddress().String(), + args[1], + args[2], + rewardPerPeriod, + vestingPeriodsCount, + vestingPeriod, + ) + if err = msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + return cmd +} diff --git a/x/accumulator/types/query.pb.go b/x/accumulator/types/query.pb.go index c9bd978cd..87e5417d5 100644 --- a/x/accumulator/types/query.pb.go +++ b/x/accumulator/types/query.pb.go @@ -219,34 +219,33 @@ func init() { func init() { proto.RegisterFile("cosmos/accumulator/query.proto", fileDescriptor_c7a2f9753e81ed3a) } var fileDescriptor_c7a2f9753e81ed3a = []byte{ - // 419 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x92, 0xb1, 0x4f, 0xdb, 0x40, - 0x14, 0xc6, 0x7d, 0x69, 0x6b, 0xa9, 0x97, 0xed, 0x9a, 0xa1, 0xb5, 0x2a, 0xa7, 0xb2, 0xd4, 0x24, - 0xaa, 0x14, 0x5f, 0x93, 0x0e, 0xed, 0xda, 0x0c, 0x44, 0x62, 0x21, 0x44, 0x62, 0x61, 0x3b, 0x3b, - 0x27, 0x63, 0x11, 0xfb, 0x1c, 0xdf, 0x19, 0x91, 0x15, 0x31, 0x30, 0x22, 0x31, 0x20, 0xfe, 0xa3, - 0x8c, 0x91, 0x58, 0x98, 0x10, 0x4a, 0xf8, 0x43, 0x90, 0xef, 0x2e, 0x10, 0x2b, 0x86, 0x30, 0xd9, - 0xba, 0xf7, 0xbd, 0xef, 0xfb, 0xbd, 0x77, 0x07, 0x6d, 0x9f, 0xf1, 0x88, 0x71, 0x4c, 0x7c, 0x3f, - 0x8b, 0xb2, 0x31, 0x11, 0x2c, 0xc5, 0x93, 0x8c, 0xa6, 0x53, 0x37, 0x49, 0x99, 0x60, 0x08, 0xa9, - 0xba, 0xbb, 0x56, 0xb7, 0x6a, 0x01, 0x0b, 0x98, 0x2c, 0xe3, 0xfc, 0x4f, 0x29, 0xad, 0xef, 0x01, - 0x63, 0xc1, 0x98, 0x62, 0x92, 0x84, 0x98, 0xc4, 0x31, 0x13, 0x44, 0x84, 0x2c, 0xe6, 0xba, 0xfa, - 0x4b, 0xe7, 0x78, 0x84, 0x53, 0x15, 0x80, 0x4f, 0x3a, 0x1e, 0x15, 0xa4, 0x83, 0x13, 0x12, 0x84, - 0xb1, 0x14, 0x6b, 0x6d, 0xbd, 0x84, 0x29, 0x21, 0x29, 0x89, 0x56, 0x66, 0x65, 0xd0, 0x64, 0x14, - 0x85, 0xda, 0xc0, 0xa9, 0x41, 0xb4, 0x9f, 0x47, 0x0c, 0x64, 0xd3, 0x90, 0x4e, 0x32, 0xca, 0x85, - 0xb3, 0x07, 0xbf, 0x14, 0x4e, 0x79, 0xc2, 0x62, 0x4e, 0xd1, 0x3f, 0x68, 0x2a, 0xf3, 0xaf, 0xe0, - 0x07, 0x68, 0x55, 0xbb, 0x96, 0xbb, 0x39, 0xb2, 0xab, 0x7a, 0x7a, 0x1f, 0x67, 0xf7, 0x75, 0x63, - 0xa8, 0xf5, 0xce, 0x01, 0xac, 0x4a, 0xc3, 0xff, 0x79, 0x34, 0x47, 0x3b, 0x10, 0xbe, 0x8c, 0xa2, - 0xcd, 0x1a, 0x2b, 0xb3, 0x7c, 0x6e, 0x57, 0x2d, 0x56, 0xcf, 0xed, 0x0e, 0x48, 0x40, 0x35, 0xdb, - 0x70, 0xad, 0xd3, 0xb9, 0x06, 0x1a, 0x54, 0xf9, 0x3e, 0x83, 0xfe, 0x85, 0xa6, 0x1c, 0x32, 0x07, - 0xfd, 0xd0, 0xaa, 0x76, 0xbf, 0x95, 0x81, 0xca, 0x9e, 0x15, 0xa7, 0x92, 0xa3, 0x7e, 0x01, 0xac, - 0x22, 0xc1, 0x9a, 0x5b, 0xc1, 0x54, 0xea, 0x3a, 0x59, 0xf7, 0xa6, 0x02, 0x3f, 0x49, 0x32, 0x74, - 0x01, 0xa0, 0xa9, 0x76, 0x82, 0x1a, 0x65, 0x18, 0x9b, 0xeb, 0xb7, 0x9a, 0x5b, 0x75, 0x2a, 0xd1, - 0x69, 0x9f, 0xdd, 0x3e, 0x5e, 0x55, 0x9a, 0xe8, 0x27, 0xd6, 0xd7, 0xac, 0x3e, 0x6d, 0x3e, 0x3a, - 0x2e, 0x79, 0x12, 0xe8, 0x1c, 0xc0, 0xcf, 0x7d, 0x2a, 0xf4, 0x25, 0xd4, 0x5f, 0x4d, 0x51, 0x82, - 0x37, 0x30, 0x8a, 0xeb, 0x7e, 0x37, 0x86, 0x5a, 0x72, 0x6f, 0x77, 0xb6, 0xb0, 0xc1, 0x7c, 0x61, - 0x83, 0x87, 0x85, 0x0d, 0x2e, 0x97, 0xb6, 0x31, 0x5f, 0xda, 0xc6, 0xdd, 0xd2, 0x36, 0x0e, 0x7f, - 0x07, 0xa1, 0x38, 0xca, 0x3c, 0xd7, 0x67, 0x51, 0x89, 0xd5, 0x69, 0xc1, 0x4c, 0x4c, 0x13, 0xca, - 0x3d, 0x53, 0x3e, 0xe3, 0x3f, 0x4f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x27, 0x5a, 0xe2, 0x05, 0x9d, - 0x03, 0x00, 0x00, + // 412 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0x41, 0x4f, 0xe2, 0x40, + 0x1c, 0xc5, 0x5b, 0x76, 0xb7, 0xc9, 0x0e, 0xb7, 0x59, 0x0e, 0xbb, 0x0d, 0x29, 0x9b, 0x1e, 0x80, + 0x6c, 0xb2, 0x1d, 0xc1, 0x83, 0x5e, 0xe5, 0x20, 0x89, 0x17, 0x91, 0xc4, 0x8b, 0xb7, 0x69, 0x99, + 0xd4, 0x46, 0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 0x8f, 0x01, 0xf8, 0x3a, 0x45, 0x99, 0x35, + 0x73, 0xb3, 0xec, 0x6e, 0x47, 0x16, 0xab, 0xee, 0x76, 0x06, 0xd8, 0x27, 0x8a, 0x6d, 0xb8, 0xb2, + 0x69, 0x3f, 0xe9, 0x0a, 0x54, 0xfa, 0x7e, 0x82, 0x1e, 0x00, 0x43, 0x1c, 0x99, 0x81, 0x7e, 0x6b, + 0x57, 0xbb, 0x7f, 0xca, 0x40, 0xc5, 0x4e, 0xce, 0x29, 0xe5, 0xb0, 0x5f, 0x00, 0xab, 0x08, 0xb0, + 0xd6, 0x4e, 0x30, 0x99, 0xba, 0x4a, 0xd6, 0xbd, 0xab, 0x80, 0x1f, 0x82, 0x0c, 0xde, 0x02, 0x43, + 0x56, 0x02, 0x9b, 0x65, 0x14, 0xeb, 0xed, 0x9b, 0xad, 0x9d, 0x3a, 0x19, 0x68, 0xdb, 0xf7, 0xcf, + 0xef, 0x8f, 0x95, 0x3a, 0x34, 0xd1, 0xc6, 0x67, 0x00, 0xa7, 0xe0, 0x67, 0x9f, 0x70, 0xd5, 0x7b, + 0x63, 0xa3, 0xb3, 0x14, 0x6c, 0x89, 0x2e, 0x36, 0xbc, 0x3d, 0x5a, 0x96, 0xd9, 0x3b, 0x99, 0x2d, + 0x2c, 0x7d, 0xbe, 0xb0, 0xf4, 0xb7, 0x85, 0xa5, 0x3f, 0x2c, 0x2d, 0x6d, 0xbe, 0xb4, 0xb4, 0x97, + 0xa5, 0xa5, 0x5d, 0xec, 0xf9, 0x01, 0xbf, 0x4c, 0x5d, 0xc7, 0xa3, 0x61, 0xbe, 0x2f, 0x7f, 0xfe, + 0xb3, 0xd1, 0x15, 0xba, 0x29, 0x98, 0xf1, 0x69, 0x4c, 0x98, 0x6b, 0x88, 0xe7, 0xba, 0xff, 0x11, + 0x00, 0x00, 0xff, 0xff, 0x19, 0x55, 0xc1, 0xda, 0x85, 0x03, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/accumulator/types/query.pb.gw.go b/x/accumulator/types/query.pb.gw.go index 82abc6882..d75db606b 100644 --- a/x/accumulator/types/query.pb.gw.go +++ b/x/accumulator/types/query.pb.gw.go @@ -224,9 +224,9 @@ func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, clie } var ( - pattern_Query_Params_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"cosmos", "cosmos-sdk", "accumulator", "params"}, "", runtime.AssumeColonVerbOpt(false))) + pattern_Query_Params_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"cosmos", "accumulator", "params"}, "", runtime.AssumeColonVerbOpt(false))) - pattern_Query_GetAdmins_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"cosmos", "cosmos-sdk", "accumulator", "admins"}, "", runtime.AssumeColonVerbOpt(false))) + pattern_Query_GetAdmins_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"cosmos", "accumulator", "admins"}, "", runtime.AssumeColonVerbOpt(false))) ) var ( diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 2809c3d00..cd6463316 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -87,14 +87,13 @@ func (k Keeper) CalculateDelegationRewards(ctx sdk.Context, val stakingtypes.Val if endingHeight > startingHeight { k.IterateValidatorSlashEventsBetween(ctx, del.GetValidatorAddr(), startingHeight, endingHeight, func(height uint64, event types.ValidatorSlashEvent) (stop bool) { - endingPeriod := event.ValidatorPeriod - if endingPeriod > startingPeriod { + if event.ValidatorPeriod > startingPeriod { rewards = rewards.Add(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, wrapStake(stake))...) // Note: It is necessary to truncate so we don't allow withdrawing // more rewards than owed. stake = stake.MulTruncate(sdk.OneDec().Sub(event.Fraction)) - startingPeriod = endingPeriod + startingPeriod = event.ValidatorPeriod } return false }, @@ -140,7 +139,7 @@ func (k Keeper) CalculateDelegationRewards(ctx sdk.Context, val stakingtypes.Val } // calculate rewards for final period - rewards = rewards.Add(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, stake)...) + rewards = rewards.Add(k.calculateDelegationRewardsBetween(ctx, val, startingPeriod, endingPeriod, wrapStake(stake))...) return rewards } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index 40380dc22..c18360aee 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -36,11 +36,11 @@ 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.Add(del.Amount.Mul(params.NftProposerReward))) + tokens = tokens.Add(del.Amount.Add(del.Amount.Mul(params.NftProposerReward))) continue } - tokens.Add(del.Amount) + tokens = tokens.Add(del.Amount) } if tokens.IsZero() { diff --git a/x/nft/abci.go b/x/nft/abci.go index 13c1d5558..1aafec17a 100644 --- a/x/nft/abci.go +++ b/x/nft/abci.go @@ -13,14 +13,14 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) for _, nft := range k.GetNFTs(ctx) { if ctx.BlockTime().Unix()-nft.LastVestingTime < nft.VestingPeriod { - return + continue } if nft.VestingCounter >= nft.VestingPeriodsCount { - return + continue } - nft.AvailableToWithdraw.Add(sdk.NewCoin(nft.Denom, sdk.NewInt(nft.VestingPeriod).Mul(nft.RewardPerPeriod.Amount))) + nft.AvailableToWithdraw = nft.AvailableToWithdraw.Add(sdk.NewCoin(nft.Denom, nft.RewardPerPeriod.Amount)) nft.VestingCounter++ nft.LastVestingTime = ctx.BlockTime().Unix() diff --git a/x/nft/client/cli/tx.go b/x/nft/client/cli/tx.go index 7f6fe5dd1..95445ac6b 100644 --- a/x/nft/client/cli/tx.go +++ b/x/nft/client/cli/tx.go @@ -134,7 +134,7 @@ func CmdSend() *cobra.Command { cmd := &cobra.Command{ Use: "send [from_key_or_address] [receiver] [nft_address]", Short: "send nft to another account", - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { cmd.Flags().Set(flags.FlagFrom, args[0]) diff --git a/x/nft/types/message_undelegate.go b/x/nft/types/message_undelegate.go index 3b9c0fd2b..f4c4366ae 100644 --- a/x/nft/types/message_undelegate.go +++ b/x/nft/types/message_undelegate.go @@ -6,7 +6,7 @@ import ( ) const ( - TypeMsgUndelegate = "delegate" + TypeMsgUndelegate = "undelegate" ) var _ sdk.Msg = &MsgUndelegate{} diff --git a/x/nft/types/message_withdraw.go b/x/nft/types/message_withdraw.go index 263db9600..59a6916ad 100644 --- a/x/nft/types/message_withdraw.go +++ b/x/nft/types/message_withdraw.go @@ -6,7 +6,7 @@ import ( ) const ( - TypeMsgWithdrawal = "delegate" + TypeMsgWithdrawal = "withdarawal" ) var _ sdk.Msg = &MsgWithdrawal{}