2020-04-01 18:49:21 +00:00
|
|
|
package main
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"encoding/json"
|
2019-11-04 20:45:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-04-22 19:26:01 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
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"
|
2019-11-04 20:45:02 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
2021-04-17 10:00:07 +00:00
|
|
|
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/crypto/hd"
|
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
flagVestingStart = "vesting-start-time"
|
|
|
|
flagVestingEnd = "vesting-end-time"
|
|
|
|
flagVestingAmt = "vesting-amount"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddGenesisAccountCmd returns add-genesis-account cobra Command.
|
2021-04-17 10:00:07 +00:00
|
|
|
func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command {
|
2019-11-04 20:45:02 +00:00
|
|
|
cmd := &cobra.Command{
|
2022-10-31 17:47:57 +00:00
|
|
|
Use: "add-genesis-account ADDRESS_OR_KEY_NAME COIN...",
|
2019-11-04 20:45:02 +00:00
|
|
|
Short: "Add a genesis account to genesis.json",
|
|
|
|
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,
|
2020-06-08 18:33:48 +00:00
|
|
|
the address will be looked up in the local Keybase. The list of initial tokens must
|
2019-11-04 20:45:02 +00:00
|
|
|
contain valid denominations. Accounts may optionally be supplied with vesting parameters.
|
|
|
|
`,
|
|
|
|
Args: cobra.ExactArgs(2),
|
2019-12-13 19:50:19 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-10 10:38:33 +00:00
|
|
|
clientCtx := client.GetClientContextFromCmd(cmd).WithKeyringOptions(hd.EthSecp256k1Option())
|
|
|
|
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
serverCtx := server.GetServerContextFromCmd(cmd)
|
|
|
|
config := serverCtx.Config
|
|
|
|
|
|
|
|
config.SetRoot(clientCtx.HomeDir)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
kr := clientCtx.Keyring
|
2019-11-04 20:45:02 +00:00
|
|
|
addr, err := sdk.AccAddressFromBech32(args[0])
|
|
|
|
if err != nil {
|
2021-09-30 12:02:58 +00:00
|
|
|
info, err := kr.Key(args[0])
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
2021-09-30 12:02:58 +00:00
|
|
|
return fmt.Errorf("failed to get address from Keyring: %w", err)
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 08:09:39 +00:00
|
|
|
addr, err = info.GetAddress()
|
|
|
|
if err != nil {
|
2022-10-10 10:38:33 +00:00
|
|
|
return err
|
2022-04-11 08:09:39 +00:00
|
|
|
}
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(args[1])
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse coins: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:23:26 +00:00
|
|
|
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
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse vesting amount: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create concrete account type based on input parameters
|
2021-04-17 10:00:07 +00:00
|
|
|
var genAccount authtypes.GenesisAccount
|
|
|
|
|
|
|
|
balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}
|
|
|
|
baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
if !vestingAmt.IsZero() {
|
2021-04-17 10:00:07 +00:00
|
|
|
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)
|
2020-04-22 19:26:01 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) ||
|
|
|
|
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) {
|
2020-04-22 19:26:01 +00:00
|
|
|
return errors.New("vesting amount cannot be greater than total amount")
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-06-29 17:02:21 +00:00
|
|
|
genAccount = ðermint.EthAccount{
|
2020-04-01 18:49:21 +00:00
|
|
|
BaseAccount: baseAccount,
|
2021-06-29 17:02:21 +00:00
|
|
|
CodeHash: common.BytesToHash(evmtypes.EmptyCodeHash).Hex(),
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-11-04 20:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := genAccount.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("failed to validate new genesis account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genFile := config.GenesisFile()
|
2021-04-17 10:00:07 +00:00
|
|
|
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 12:02:58 +00:00
|
|
|
authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get accounts from any: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if accs.Contains(addr) {
|
2019-11-04 20:45:02 +00:00
|
|
|
return fmt.Errorf("cannot add account at existing address %s", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new account to the set of genesis accounts and sanitize the
|
|
|
|
// accounts afterwards.
|
2021-04-17 10:00:07 +00:00
|
|
|
accs = append(accs, genAccount)
|
|
|
|
accs = authtypes.SanitizeGenesisAccounts(accs)
|
|
|
|
|
|
|
|
genAccs, err := authtypes.PackAccounts(accs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert accounts into any's: %w", err)
|
|
|
|
}
|
|
|
|
authGenState.Accounts = genAccs
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-09-30 12:02:58 +00:00
|
|
|
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
appState[authtypes.ModuleName] = authGenStateBz
|
|
|
|
|
2021-09-30 12:02:58 +00:00
|
|
|
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
|
2021-04-17 10:00:07 +00:00
|
|
|
bankGenState.Balances = append(bankGenState.Balances, balances)
|
|
|
|
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
|
|
|
|
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)
|
|
|
|
|
2021-09-30 12:02:58 +00:00
|
|
|
bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
|
2021-04-17 10:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
appState[banktypes.ModuleName] = bankGenStateBz
|
2019-11-04 20:45:02 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
appStateJSON, err := json.Marshal(appState)
|
2019-11-04 20:45:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal application genesis state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
genDoc.AppState = appStateJSON
|
|
|
|
return genutil.ExportGenesisFile(genDoc, genFile)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
2021-09-30 12:02:58 +00:00
|
|
|
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
|
2019-11-04 20:45:02 +00:00
|
|
|
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
|
2021-04-17 10:00:07 +00:00
|
|
|
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)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|