Skip to content

Commit

Permalink
feat(vesting): update to align with cosmos-sdk v0.50.4
Browse files Browse the repository at this point in the history
This commit updates the vesting module to match the changes introduced in cosmos-sdk v0.50.4.
  • Loading branch information
bdeneux committed Jun 4, 2024
1 parent 2ba73c5 commit 7536aee
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 378 deletions.
62 changes: 37 additions & 25 deletions x/vesting/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package cli

import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"

"cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -16,13 +19,13 @@ import (
"github.com/axone-protocol/axoned/v8/x/vesting/types"
)

// FlagDelayed Transaction command flags.
// Transaction command flags.
const (
FlagDelayed = "delayed"
)

// GetTxCmd returns vesting module's transaction commands.
func GetTxCmd() *cobra.Command {
func GetTxCmd(ac address.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Vesting transaction subcommands",
Expand All @@ -32,18 +35,18 @@ func GetTxCmd() *cobra.Command {
}

txCmd.AddCommand(
NewMsgCreateVestingAccountCmd(),
NewMsgCreatePermanentLockedAccountCmd(),
NewMsgCreatePeriodicVestingAccountCmd(),
NewMsgCreateCliffVestingAccountCmd(),
NewMsgCreateVestingAccountCmd(ac),
NewMsgCreatePermanentLockedAccountCmd(ac),
NewMsgCreatePeriodicVestingAccountCmd(ac),
NewMsgCreateCliffVestingAccountCmd(ac),
)

return txCmd
}

// NewMsgCreateVestingAccountCmd returns a CLI command handler for creating a
// MsgCreateVestingAccount transaction.
func NewMsgCreateVestingAccountCmd() *cobra.Command {
func NewMsgCreateVestingAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-vesting-account [to_address] [amount] [end_time]",
Short: "Create a new vesting account funded with an allocation of tokens.",
Expand All @@ -58,11 +61,15 @@ timestamp.`,
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}

if args[1] == "" {
return errors.New("amount is empty")
}

amount, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
Expand All @@ -76,7 +83,6 @@ timestamp.`,
delayed, _ := cmd.Flags().GetBool(FlagDelayed)

msg := types.NewMsgCreateVestingAccount(clientCtx.GetFromAddress(), toAddr, amount, endTime, delayed)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand All @@ -89,7 +95,7 @@ timestamp.`,

// NewMsgCreatePermanentLockedAccountCmd returns a CLI command handler for creating a
// MsgCreatePermanentLockedAccount transaction.
func NewMsgCreatePermanentLockedAccountCmd() *cobra.Command {
func NewMsgCreatePermanentLockedAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-permanent-locked-account [to_address] [amount]",
Short: "Create a new permanently locked account funded with an allocation of tokens.",
Expand All @@ -102,18 +108,21 @@ tokens.`,
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}

if args[1] == "" {
return errors.New("amount is empty")
}

amount, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
}

msg := types.NewMsgCreatePermanentLockedAccount(clientCtx.GetFromAddress(), toAddr, amount)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand All @@ -129,15 +138,15 @@ type VestingData struct {
}

type InputPeriod struct {
Coins string `json:"coins"`
LengthSeconds int64 `json:"length_seconds"`
Coins string `json:"coins"`
Length int64 `json:"length_seconds"` //nolint:tagliatelle
}

// NewMsgCreatePeriodicVestingAccountCmd returns a CLI command handler for creating a
// MsgCreatePeriodicVestingAccountCmd transaction.
//
//nolint:funlen,lll
func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
func NewMsgCreatePeriodicVestingAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-periodic-vesting-account [to_address] [periods_json_file]",
Short: "Create a new vesting account funded with an allocation of tokens.",
Expand All @@ -146,7 +155,7 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
An array of coin strings and unix epoch times for coins to vest
{ "start_time": 1625204910,
"period":[
"periods":[
{
"coins": "10test",
"length_seconds":2592000 //30 days
Expand All @@ -165,7 +174,7 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
return err
}

toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}
Expand All @@ -190,15 +199,15 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {
return err
}

if p.LengthSeconds < 0 {
return fmt.Errorf("invalid period length of %d in period %d, length must be greater than 0", p.LengthSeconds, i)
if p.Length < 0 {
return fmt.Errorf("invalid period length of %d in period %d, length must be greater than 0", p.Length, i)
}
period := types.Period{Length: p.LengthSeconds, Amount: amount}

period := types.Period{Length: p.Length, Amount: amount}
periods = append(periods, period)
}

msg := types.NewMsgCreatePeriodicVestingAccount(clientCtx.GetFromAddress(), toAddr, vestingData.StartTime, periods)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand All @@ -210,11 +219,11 @@ func NewMsgCreatePeriodicVestingAccountCmd() *cobra.Command {

// NewMsgCreateCliffVestingAccountCmd returns a CLI command handler for creating a
// MsgCreateCliffVestingAccount transaction.
func NewMsgCreateCliffVestingAccountCmd() *cobra.Command {
func NewMsgCreateCliffVestingAccountCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-cliff-vesting-account [to_address] [amount] [cliff_time] [end_time]",
Short: "Create a new vesting account funded with an allocation of tokens with cliff.",
Long: `Create a new vesting account funded with an allocation of tokens. The
Long: `Create a new vesting account funded with an allocation of tokens with cliff. The
tokens allowed will be start vested but the token will be released only after the cliff time.
All vesting accounts created will have their start time
set by the committed block's time. The end_time and cliff_time must be provided as a UNIX epoch
Expand All @@ -225,11 +234,15 @@ timestamp.`,
if err != nil {
return err
}
toAddr, err := sdk.AccAddressFromBech32(args[0])
toAddr, err := ac.StringToBytes(args[0])
if err != nil {
return err
}

if args[1] == "" {
return errors.New("amount is empty")
}

amount, err := sdk.ParseCoinsNormalized(args[1])
if err != nil {
return err
Expand All @@ -246,7 +259,6 @@ timestamp.`,
}

msg := types.NewMsgCreateCliffVestingAccount(clientCtx.GetFromAddress(), toAddr, amount, endTime, cliffTime)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down
67 changes: 48 additions & 19 deletions x/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package vesting
import (
"encoding/json"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"

abci "github.com/cometbft/cometbft/abci/types"

modulev1 "cosmossdk.io/api/cosmos/vesting/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -23,22 +26,25 @@ import (
)

var (
_ module.AppModuleBasic = AppModule{}

_ appmodule.AppModule = AppModule{}
_ appmodule.HasServices = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// AppModuleBasic defines the basic application module used by the sub-vesting
// module. The module itself contain no special logic or state other than message
// handling.
type AppModuleBasic struct{}
type AppModuleBasic struct {
ac address.Codec
}

// Name returns the module's name.
func (AppModuleBasic) Name() string {
return types.ModuleName
}

// RegisterLegacyAminoCodec registers the module's types with the given codec.
// RegisterCodec registers the module's types with the given codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}
Expand All @@ -61,16 +67,11 @@ func (AppModuleBasic) ValidateGenesis(_ codec.JSONCodec, _ client.TxEncodingConf

// RegisterGRPCGatewayRoutes registers the module's gRPC Gateway routes. Currently, this
// is a no-op.
func (a AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *runtime.ServeMux) {}
func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.ServeMux) {}

// GetTxCmd returns the root tx command for the auth module.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
}

// GetQueryCmd returns the module's root query command. Currently, this is a no-op.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
}

// AppModule extends the AppModuleBasic implementation by implementing the
Expand All @@ -82,20 +83,19 @@ type AppModule struct {
bankKeeper types.BankKeeper
}

func (am AppModule) IsOnePerModuleType() {}

func (am AppModule) IsAppModule() {}

func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleBasic: AppModuleBasic{ac: ak.AddressCodec()},
accountKeeper: ak,
bankKeeper: bk,
}
}

// RegisterInvariants performs a no-op; there are no invariants to enforce.
func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}

// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
Expand All @@ -115,3 +115,32 @@ func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMe

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }

//
// App Wiring Setup
//

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}

type ModuleInputs struct {
depinject.In

AccountKeeper keeper.AccountKeeper
BankKeeper types.BankKeeper
}

type ModuleOutputs struct {
depinject.Out

Module appmodule.AppModule
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
m := NewAppModule(in.AccountKeeper, in.BankKeeper)

return ModuleOutputs{Module: m}
}
Loading

0 comments on commit 7536aee

Please sign in to comment.