github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2create.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"encoding/hex"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/iotexproject/iotex-core/action"
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  	"github.com/iotexproject/iotex-core/ioctl/util"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_stake2CreateCmdUses = map[config.Language]string{
    22  		config.English: "create AMOUNT_IOTX CANDIDATE_NAME STAKE_DURATION [DATA] [--auto-stake" +
    23  			"] [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GASP_RICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "create IOTX数量 候选人名字 投票持续时间 [数据] [--auto-stake" +
    25  			"] [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    26  	}
    27  
    28  	_stake2CreateCmdShorts = map[config.Language]string{
    29  		config.English: "Create bucket on IoTeX blockchain",
    30  		config.Chinese: "在IoTeX区块链上创建投票",
    31  	}
    32  )
    33  
    34  // _stake2CreateCmd represents the stake2 create command
    35  var _stake2CreateCmd = &cobra.Command{
    36  	Use:   config.TranslateInLang(_stake2CreateCmdUses, config.UILanguage),
    37  	Short: config.TranslateInLang(_stake2CreateCmdShorts, config.UILanguage),
    38  	Args:  cobra.RangeArgs(3, 4),
    39  	RunE: func(cmd *cobra.Command, args []string) error {
    40  		cmd.SilenceUsage = true
    41  		err := stake2Create(args)
    42  		return output.PrintError(err)
    43  	},
    44  }
    45  
    46  func init() {
    47  	RegisterWriteCommand(_stake2CreateCmd)
    48  	_stake2CreateCmd.Flags().BoolVar(&_stake2AutoStake, "auto-stake", false,
    49  		config.TranslateInLang(_stake2FlagAutoStakeUsages, config.UILanguage))
    50  }
    51  
    52  func stake2Create(args []string) error {
    53  	var amount = args[0]
    54  	amountInRau, err := util.StringToRau(amount, util.IotxDecimalNum)
    55  	if err != nil {
    56  		return output.NewError(output.ConvertError, "invalid amount", err)
    57  	}
    58  	amountStringInRau := amountInRau.String()
    59  
    60  	var candidateName = args[1]
    61  	if !action.IsValidCandidateName(candidateName) {
    62  		return output.NewError(output.ValidationError, "", action.ErrInvalidCanName)
    63  	}
    64  	stakeDuration, err := parseStakeDuration(args[2])
    65  	if err != nil {
    66  		return output.NewError(0, "", err)
    67  	}
    68  	duration := uint32(stakeDuration.Uint64())
    69  
    70  	var data []byte
    71  	if len(args) == 4 {
    72  		data, err = hex.DecodeString(args[3])
    73  		if err != nil {
    74  			return output.NewError(output.ConvertError, "failed to decode data", err)
    75  		}
    76  	}
    77  
    78  	sender, err := Signer()
    79  	if err != nil {
    80  		return output.NewError(output.AddressError, "failed to get signed address", err)
    81  	}
    82  
    83  	gasLimit := _gasLimitFlag.Value().(uint64)
    84  	if gasLimit == 0 {
    85  		gasLimit = action.CreateStakeBaseIntrinsicGas + action.CreateStakePayloadGas*uint64(len(data))
    86  	}
    87  
    88  	gasPriceRau, err := gasPriceInRau()
    89  	if err != nil {
    90  		return output.NewError(0, "failed to get gas price", err)
    91  	}
    92  	nonce, err := nonce(sender)
    93  	if err != nil {
    94  		return output.NewError(0, "failed to get nonce ", err)
    95  	}
    96  
    97  	s2c, err := action.NewCreateStake(nonce, candidateName, amountStringInRau, duration, _stake2AutoStake, data, gasLimit, gasPriceRau)
    98  	if err != nil {
    99  		return output.NewError(output.InstantiationError, "failed to make a createStake instance", err)
   100  	}
   101  	return SendAction(
   102  		(&action.EnvelopeBuilder{}).
   103  			SetNonce(nonce).
   104  			SetGasPrice(gasPriceRau).
   105  			SetGasLimit(gasLimit).
   106  			SetAction(s2c).Build(),
   107  		sender)
   108  }