Skip to content

Commit

Permalink
fix rewards, vesting issues... (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi authored Aug 6, 2024
1 parent 5ccd2bd commit 06a6e5b
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 47 deletions.
4 changes: 2 additions & 2 deletions proto/cosmos/accumulator/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion x/accumulator/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions x/accumulator/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions x/accumulator/client/cli/tx_admin.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 27 additions & 28 deletions x/accumulator/types/query.pb.go

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

4 changes: 2 additions & 2 deletions x/accumulator/types/query.pb.gw.go

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

7 changes: 3 additions & 4 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions x/distribution/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions x/nft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion x/nft/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion x/nft/types/message_undelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
TypeMsgUndelegate = "delegate"
TypeMsgUndelegate = "undelegate"
)

var _ sdk.Msg = &MsgUndelegate{}
Expand Down
2 changes: 1 addition & 1 deletion x/nft/types/message_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
TypeMsgWithdrawal = "delegate"
TypeMsgWithdrawal = "withdarawal"
)

var _ sdk.Msg = &MsgWithdrawal{}
Expand Down

0 comments on commit 06a6e5b

Please sign in to comment.