Skip to content

Commit

Permalink
Feat/block 401 (#201)
Browse files Browse the repository at this point in the history
* chore: set max cap in creating vestings per user

* chore: add more unit tests in vesting

* chore: update proto buf to do error handling in CLI side

* move CLI flag constants declaration in CLI folder

* cfix: remove hardcoded gov module name

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
kenta-elys and cosmic-vagabond authored Sep 28, 2023
1 parent be505a9 commit be3bd0d
Show file tree
Hide file tree
Showing 34 changed files with 1,342 additions and 82 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ func NewElysApp(
app.BankKeeper,
app.StakingKeeper,
app.AssetprofileKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.AccountedPoolKeeper = *accountedpoolmodulekeeper.NewKeeper(
Expand Down
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ genesis:
epoch_identifier: tenseconds
num_epochs: "10"
vest_now_factor: "90"
num_max_vestings: "10"
incentive:
params:
lp_incentives:
Expand Down
14 changes: 14 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38623,6 +38623,9 @@ paths:
format: int64
vest_now_factor:
type: string
num_max_vestings:
type: string
format: int64
description: >-
QueryParamsResponse is response type for the Query/Params RPC
method.
Expand Down Expand Up @@ -78579,6 +78582,8 @@ definitions:
type: object
elys.commitment.MsgUncommitTokensResponse:
type: object
elys.commitment.MsgUpdateVestingInfoResponse:
type: object
elys.commitment.MsgVestNowResponse:
type: object
elys.commitment.MsgVestResponse:
Expand All @@ -78604,6 +78609,9 @@ definitions:
format: int64
vest_now_factor:
type: string
num_max_vestings:
type: string
format: int64
description: Params defines the parameters for the module.
elys.commitment.QueryParamsResponse:
type: object
Expand All @@ -78628,6 +78636,9 @@ definitions:
format: int64
vest_now_factor:
type: string
num_max_vestings:
type: string
format: int64
description: QueryParamsResponse is response type for the Query/Params RPC method.
elys.commitment.QueryShowCommitmentsResponse:
type: object
Expand Down Expand Up @@ -78696,6 +78707,9 @@ definitions:
format: int64
vest_now_factor:
type: string
num_max_vestings:
type: string
format: int64
elys.commitment.VestingTokens:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions proto/elys/commitment/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ message VestingInfo {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
int64 num_max_vestings = 6;
}
27 changes: 20 additions & 7 deletions proto/elys/commitment/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ option go_package = "github.com/elys-network/elys/x/commitment/types";

// Msg defines the Msg service.
service Msg {
rpc CommitTokens (MsgCommitTokens ) returns (MsgCommitTokensResponse );
rpc UncommitTokens (MsgUncommitTokens) returns (MsgUncommitTokensResponse);
rpc WithdrawTokens (MsgWithdrawTokens) returns (MsgWithdrawTokensResponse);
rpc DepositTokens (MsgDepositTokens ) returns (MsgDepositTokensResponse );
rpc Vest (MsgVest ) returns (MsgVestResponse );
rpc CancelVest (MsgCancelVest ) returns (MsgCancelVestResponse );
rpc VestNow (MsgVestNow ) returns (MsgVestNowResponse );
rpc CommitTokens (MsgCommitTokens ) returns (MsgCommitTokensResponse );
rpc UncommitTokens (MsgUncommitTokens ) returns (MsgUncommitTokensResponse );
rpc WithdrawTokens (MsgWithdrawTokens ) returns (MsgWithdrawTokensResponse );
rpc DepositTokens (MsgDepositTokens ) returns (MsgDepositTokensResponse );
rpc Vest (MsgVest ) returns (MsgVestResponse );
rpc CancelVest (MsgCancelVest ) returns (MsgCancelVestResponse );
rpc VestNow (MsgVestNow ) returns (MsgVestNowResponse );
rpc UpdateVestingInfo (MsgUpdateVestingInfo) returns (MsgUpdateVestingInfoResponse);
}
message MsgCommitTokens {
string creator = 1;
Expand Down Expand Up @@ -72,3 +73,15 @@ message MsgVestNow {

message MsgVestNowResponse {}

message MsgUpdateVestingInfo {
string authority = 1;
string baseDenom = 2;
string vestingDenom = 3;
string epochIdentifier = 4;
int64 numEpochs = 5;
int64 vestNowFactor = 6;
int64 numMaxVestings = 7;
}

message MsgUpdateVestingInfoResponse {}

5 changes: 5 additions & 0 deletions testutil/keeper/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/elys-network/elys/x/commitment/keeper"
"github.com/elys-network/elys/x/commitment/types"
Expand All @@ -36,6 +38,8 @@ func CommitmentKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
"CommitmentParams",
)

govAddress := sdk.AccAddress(address.Module(govtypes.ModuleName))
k := keeper.NewKeeper(
cdc,
storeKey,
Expand All @@ -44,6 +48,7 @@ func CommitmentKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
govAddress.String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
10 changes: 5 additions & 5 deletions x/amm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func NewKeeper(
transientStoreKey: transientStoreKey,
paramstore: ps,

bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
oracleKeeper: oracleKeeper,
commitmentKeeper: commitmentKeeper,
apKeeper: apKeeper,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
oracleKeeper: oracleKeeper,
commitmentKeeper: commitmentKeeper,
apKeeper: apKeeper,
accountedPoolKeeper: accountedPoolKeeper,
}
}
Expand Down
10 changes: 10 additions & 0 deletions x/commitment/client/cli/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cli

const (
BaseDenom = "base-denom"
VestingDenom = "vesting-denom"
EpochIdentifier = "epoch-identifier"
NumEpochs = "num-epochs"
VestNowFactor = "vest-now-factor"
NumMaxVestings = "num-max-vestings"
)
1 change: 1 addition & 0 deletions x/commitment/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdVest())
cmd.AddCommand(CmdCancelVest())
cmd.AddCommand(CmdVestNow())
cmd.AddCommand(CmdUpdateVestingInfo())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
154 changes: 154 additions & 0 deletions x/commitment/client/cli/tx_update_vesting_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package cli

import (
"errors"
"strconv"

"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/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/elys-network/elys/x/commitment/types"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdUpdateVestingInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "update-vesting-info",
Short: "Broadcast message update-vesting-info",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

title, err := cmd.Flags().GetString(cli.FlagTitle)
if err != nil {
return err
}

summary, err := cmd.Flags().GetString(cli.FlagSummary)
if err != nil {
return err
}

metadata, err := cmd.Flags().GetString(cli.FlagMetadata)
if err != nil {
return err
}

argBaseDenom, err := cmd.Flags().GetString(BaseDenom)
if err != nil {
return err
}

argVestingDenom, err := cmd.Flags().GetString(VestingDenom)
if err != nil {
return err
}

argEpochIdentifier, err := cmd.Flags().GetString(EpochIdentifier)
if err != nil {
return err
}

argNumEpochs, err := cmd.Flags().GetString(NumEpochs)
if err != nil {
return err
}

argVestNowFactor, err := cmd.Flags().GetString(VestNowFactor)
if err != nil {
return err
}

argNumMaxVestings, err := cmd.Flags().GetString(NumMaxVestings)
if err != nil {
return err
}

signer := clientCtx.GetFromAddress()
if signer == nil {
return errors.New("signer address is missing")
}

numEpochs, err := strconv.ParseInt(argNumEpochs, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argNumEpochs)
}

vestNowFactor, err := strconv.ParseInt(argVestNowFactor, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argVestNowFactor)
}

maxVestings, err := strconv.ParseInt(argNumMaxVestings, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argNumMaxVestings)
}

govAddress := sdk.AccAddress(address.Module(govtypes.ModuleName))
msg := types.NewMsgUpdateVestingInfo(
govAddress.String(),
argBaseDenom,
argVestingDenom,
argEpochIdentifier,
numEpochs,
vestNowFactor,
maxVestings,
)
if err := msg.ValidateBasic(); err != nil {
return err
}

depositStr, err := cmd.Flags().GetString(cli.FlagDeposit)
if err != nil {
return err
}

deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}

govMsg, err := v1.NewMsgSubmitProposal([]sdk.Msg{msg}, deposit, signer.String(), metadata, title, summary)
if err != nil {
return err
}

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

cmd.Flags().String(BaseDenom, "", "base denom")
cmd.Flags().String(VestingDenom, "", "vesting-denom")
cmd.Flags().String(EpochIdentifier, "", "epoch-identifier")
cmd.Flags().String(NumEpochs, "", "num-epochs")
cmd.Flags().String(VestNowFactor, "", "vest-now-factor")
cmd.Flags().String(NumMaxVestings, "", "num-max-vestings")
cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagSummary, "", "summary of proposal")
cmd.Flags().String(cli.FlagMetadata, "", "metadata of proposal")
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
_ = cmd.MarkFlagRequired(BaseDenom)
_ = cmd.MarkFlagRequired(VestingDenom)
_ = cmd.MarkFlagRequired(EpochIdentifier)
_ = cmd.MarkFlagRequired(NumEpochs)
_ = cmd.MarkFlagRequired(VestNowFactor)
_ = cmd.MarkFlagRequired(NumMaxVestings)
_ = cmd.MarkFlagRequired(cli.FlagTitle)
_ = cmd.MarkFlagRequired(cli.FlagSummary)
_ = cmd.MarkFlagRequired(cli.FlagMetadata)
_ = cmd.MarkFlagRequired(cli.FlagDeposit)
flags.AddTxFlagsToCmd(cmd)

return cmd
}
50 changes: 50 additions & 0 deletions x/commitment/client/cli/tx_update_vesting_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli_test

import (
"strconv"
"testing"

clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"

"github.com/elys-network/elys/testutil/network"
"github.com/elys-network/elys/x/commitment/client/cli"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func setupNetwork(t *testing.T) *network.Network {
t.Helper()

cfg := network.DefaultConfig()
return network.New(t, cfg)
}

func TestGovUpdateVestingInfo(t *testing.T) {
net := setupNetwork(t)
ctx := net.Validators[0].ClientCtx
val := net.Validators[0]

// Use baseURL to make API HTTP requests or use val.RPCClient to make direct
// Tendermint RPC calls.
// ...

args := []string{
"--title=test",
"--summary=test",
"--metadata=test",
"--deposit=1000000uelys",
"--base-denom=ueden",
"--vesting-denom=uelys",
"--epoch-identifier=day",
"--num-epochs=100",
"--vest-now-factor=1",
"--num-max-vestings=10",
"--from=" + val.Address.String(),
"-y",
}

_, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdUpdateVestingInfo(), args)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion x/commitment/keeper/epoch_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64)

// Future Improvement: check all VestingInfos and get all VestingTokens by denom
// so we can iterate different denoms in different EpochIdentifiers
vestingInfo := k.GetVestingInfo(ctx, ptypes.Eden)
vestingInfo, _ := k.GetVestingInfo(ctx, ptypes.Eden)
if vestingInfo != nil {
if epochIdentifier == vestingInfo.EpochIdentifier {
k.Logger(ctx).Info("Vesting tokens for vestingInfo", vestingInfo)
Expand Down
Loading

0 comments on commit be3bd0d

Please sign in to comment.