Skip to content

Commit

Permalink
chore: add more unit tests in vesting
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Sep 26, 2023
1 parent b1e0d25 commit f39bfed
Show file tree
Hide file tree
Showing 28 changed files with 1,289 additions and 57 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
2 changes: 2 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78582,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 Down
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;
string numEpochs = 5;
string vestNowFactor = 6;
string 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,7 @@ 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"
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 +37,9 @@ func CommitmentKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
memStoreKey,
"CommitmentParams",
)

govAddress := sdk.AccAddress(address.Module("gov"))

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
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
137 changes: 137 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,137 @@
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"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
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("base-denom")
if err != nil {
return err
}

argVestingDenom, err := cmd.Flags().GetString("vesting-denom")
if err != nil {
return err
}

argEpochIdentifier, err := cmd.Flags().GetString("epoch-identifier")
if err != nil {
return err
}

argNumEpochs, err := cmd.Flags().GetString("num-epochs")
if err != nil {
return err
}

argVestNowFactor, err := cmd.Flags().GetString("vest-now-factor")
if err != nil {
return err
}

argNumMaxVestings, err := cmd.Flags().GetString("num-max-vestings")
if err != nil {
return err
}

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

govAddress := sdk.AccAddress(address.Module("gov"))
msg := types.NewMsgUpdateVestingInfo(
govAddress.String(),
argBaseDenom,
argVestingDenom,
argEpochIdentifier,
argNumEpochs,
argVestNowFactor,
argNumMaxVestings,
)
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("base-denom", "", "base denom (string)")
cmd.Flags().String("vesting-denom", "", "vesting-denom (string)")
cmd.Flags().String("epoch-identifier", "", "epoch-identifier (string)")
cmd.Flags().String("num-epochs", "", "num-epochs (int64)")
cmd.Flags().String("vest-now-factor", "", "vest-now-factor (decimal)")
cmd.Flags().String("num-max-vestings", "", "num-max-vestings (int64)")
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("base-denom")
_ = cmd.MarkFlagRequired("vesting-denom")
_ = cmd.MarkFlagRequired("epoch-identifier")
_ = cmd.MarkFlagRequired("num-epochs")
_ = cmd.MarkFlagRequired("vest-now-factor")
_ = cmd.MarkFlagRequired("num-max-vestings")
_ = 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
3 changes: 3 additions & 0 deletions x/commitment/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type (
bankKeeper types.BankKeeper
stakingKeeper types.StakingKeeper
apKeeper types.AssetProfileKeeper
authority string
}
)

Expand All @@ -68,6 +69,7 @@ func NewKeeper(
bankKeeper types.BankKeeper,
stakingKeeper types.StakingKeeper,
apKeeper types.AssetProfileKeeper,
authority string,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
Expand All @@ -83,6 +85,7 @@ func NewKeeper(
bankKeeper: bankKeeper,
stakingKeeper: stakingKeeper,
apKeeper: apKeeper,
authority: authority,
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/commitment/keeper/msg_server_cancel_vest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (k msgServer) CancelVest(goCtx context.Context, msg *types.MsgCancelVest) (*types.MsgCancelVestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

vestingInfo := k.GetVestingInfo(ctx, msg.Denom)
vestingInfo, _ := k.GetVestingInfo(ctx, msg.Denom)

if vestingInfo == nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidDenom, "denom: %s", msg.Denom)
Expand Down
1 change: 1 addition & 0 deletions x/commitment/keeper/msg_server_cancel_vest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestCancelVest(t *testing.T) {
EpochIdentifier: "tenseconds",
NumEpochs: 10,
VestNowFactor: sdk.NewInt(90),
NumMaxVestings: 10,
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/commitment/keeper/msg_server_uncommit_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (k msgServer) UncommitTokens(goCtx context.Context, msg *types.MsgUncommitT
if msg.Denom == ptypes.Eden {
k.EdenUncommitted(ctx, msg.Creator, sdk.NewCoin(msg.Denom, msg.Amount))
}

// Emit blockchain event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
Expand Down
Loading

0 comments on commit f39bfed

Please sign in to comment.