cosmos-sdk/x/auth/vesting/client/cli/tx.go
2023-11-07 11:42:58 +00:00

117 lines
3.1 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
"cosmossdk.io/x/auth/vesting/types"
"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"
)
// GetTxCmd returns vesting module's transaction commands.
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Vesting transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(
NewMsgCreatePeriodicVestingAccountCmd(),
)
return txCmd
}
type VestingData struct {
StartTime int64 `json:"start_time"`
Periods []InputPeriod `json:"periods"`
}
type InputPeriod struct {
Coins string `json:"coins"`
Length int64 `json:"length_seconds"`
}
// NewMsgCreatePeriodicVestingAccountCmd returns a CLI command handler for creating a MsgCreatePeriodicVestingAccountCmd transaction.
// This command can be migrated to AutoCLI but it would be CLI breaking to do so.
func NewMsgCreatePeriodicVestingAccountCmd() *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.",
Long: `A sequence of coins and period length in seconds. Periods are sequential, in that the duration of of a period only starts at the end of the previous period. The duration of the first period starts upon account creation. For instance, the following periods.json file shows 20 "test" coins vesting 30 days apart from each other.
Where periods.json contains an array of coin strings and unix epoch times for coins to vest:
{
"start_time": 1625204910,
"periods": [
{
"coins": "10test",
"length_seconds": 2592000 //30 days
},
{
"coins": "10test",
"length_seconds": 2592000 //30 days
}
]
}
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
toAddr, err := clientCtx.AddressCodec.StringToBytes(args[0])
if err != nil {
return err
}
contents, err := os.ReadFile(args[1])
if err != nil {
return err
}
var vestingData VestingData
err = json.Unmarshal(contents, &vestingData)
if err != nil {
return err
}
var periods []types.Period
for i, p := range vestingData.Periods {
amount, err := sdk.ParseCoinsNormalized(p.Coins)
if err != nil {
return err
}
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.Length, Amount: amount}
periods = append(periods, period)
}
msg := types.NewMsgCreatePeriodicVestingAccount(clientCtx.GetFromAddress(), toAddr, vestingData.StartTime, periods)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}