From 3fdeaded8211717e7b5b1dbd7c5ea4901b709218 Mon Sep 17 00:00:00 2001 From: arnabghose997 Date: Fri, 14 Oct 2022 15:40:45 +0530 Subject: [PATCH] added vesting module --- app/app.go | 7 +++++ cmd/hid-noded/cmd/genaccounts.go | 54 ++++++++++++++++++++++++++++++-- cmd/hid-noded/cmd/root.go | 4 ++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/app/app.go b/app/app.go index c5eb25f..42cc8f4 100644 --- a/app/app.go +++ b/app/app.go @@ -21,10 +21,12 @@ import ( "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/ante" + "github.com/cosmos/cosmos-sdk/x/auth/vesting" authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" "github.com/cosmos/cosmos-sdk/x/authz" authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" @@ -145,6 +147,7 @@ var ( evidence.AppModuleBasic{}, transfer.AppModuleBasic{}, authzmodule.AppModuleBasic{}, + vesting.AppModuleBasic{}, ssimodule.AppModuleBasic{}, ) @@ -392,6 +395,7 @@ func New( encodingConfig.TxConfig, ), auth.NewAppModule(appCodec, app.AccountKeeper, nil), + vesting.NewAppModule(app.AccountKeeper, app.BankKeeper), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(appCodec, *app.CapabilityKeeper), @@ -416,6 +420,7 @@ func New( // NOTE: staking module is required if HistoricalEntries param > 0 app.mm.SetOrderBeginBlockers( authtypes.ModuleName, + vestingtypes.ModuleName, banktypes.ModuleName, govtypes.ModuleName, upgradetypes.ModuleName, @@ -437,6 +442,7 @@ func New( app.mm.SetOrderEndBlockers( authtypes.ModuleName, + vestingtypes.ModuleName, banktypes.ModuleName, crisistypes.ModuleName, govtypes.ModuleName, @@ -464,6 +470,7 @@ func New( app.mm.SetOrderInitGenesis( capabilitytypes.ModuleName, authtypes.ModuleName, + vestingtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName, diff --git a/cmd/hid-noded/cmd/genaccounts.go b/cmd/hid-noded/cmd/genaccounts.go index 0788727..e212813 100644 --- a/cmd/hid-noded/cmd/genaccounts.go +++ b/cmd/hid-noded/cmd/genaccounts.go @@ -3,6 +3,7 @@ package cmd import ( "bufio" "encoding/json" + "errors" "fmt" "github.com/spf13/cobra" @@ -13,11 +14,18 @@ import ( "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/genutil" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) +const ( + flagVestingStart = "vesting-start-time" + flagVestingEnd = "vesting-end-time" + flagVestingAmt = "vesting-amount" +) + // AddGenesisAccountCmd returns add-genesis-account cobra Command. func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ @@ -26,7 +34,7 @@ func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command { Long: `Add a genesis account to genesis.json. The provided account must specify the account address or key name and a list of initial coins. If a key name is given, the address will be looked up in the local Keybase. The list of initial tokens must -contain valid denominations. +contain valid denominations. Accounts may optionally be supplied with vesting parameters. `, Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { @@ -65,12 +73,51 @@ contain valid denominations. addr = info.GetAddress() } + vestingStart, err := cmd.Flags().GetInt64(flagVestingStart) + if err != nil { + return err + } + vestingEnd, err := cmd.Flags().GetInt64(flagVestingEnd) + if err != nil { + return err + } + vestingAmtStr, err := cmd.Flags().GetString(flagVestingAmt) + if err != nil { + return err + } + + vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr) + if err != nil { + return fmt.Errorf("failed to parse vesting amount: %w", err) + } + // create concrete account type based on input parameters var genAccount authtypes.GenesisAccount balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()} baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0) - genAccount = baseAccount + + if !vestingAmt.IsZero() { + baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd) + + if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) || + baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) { + return errors.New("vesting amount cannot be greater than total amount") + } + + switch { + case vestingStart != 0 && vestingEnd != 0: + genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart) + + case vestingEnd != 0: + genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount) + + default: + return errors.New("invalid vesting parameters; must supply start and end time or end time") + } + } else { + genAccount = baseAccount + } if err := genAccount.Validate(); err != nil { return fmt.Errorf("failed to validate new genesis account: %w", err) @@ -134,6 +181,9 @@ contain valid denominations. cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") + cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts") + cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts") + cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts") flags.AddQueryFlagsToCmd(cmd) return cmd diff --git a/cmd/hid-noded/cmd/root.go b/cmd/hid-noded/cmd/root.go index a1a4f76..975aab6 100644 --- a/cmd/hid-noded/cmd/root.go +++ b/cmd/hid-noded/cmd/root.go @@ -24,7 +24,8 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/crisis" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" - + vestingcli "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli" + "github.com/spf13/cast" "github.com/spf13/cobra" tmcli "github.com/tendermint/tendermint/libs/cli" @@ -152,6 +153,7 @@ func txCommand() *cobra.Command { authcmd.GetEncodeCommand(), authcmd.GetDecodeCommand(), flags.LineBreak, + vestingcli.GetTxCmd(), ) hidnode.ModuleBasics.AddTxCommands(cmd)