github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2add.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 didslaimed.
     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  	"strconv"
    11  
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/action"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/output"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  )
    19  
    20  // Multi-language support
    21  var (
    22  	_stake2AddCmdUses = map[config.Language]string{
    23  		config.English: "add BUCKET_INDEX AMOUNT_IOTX [DATA]" +
    24  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    25  		config.Chinese: "add 票索引 IOTX数量 [数据]" +
    26  			" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    27  	}
    28  
    29  	_stake2AddCmdShorts = map[config.Language]string{
    30  		config.English: "Add IOTX to bucket on IoTeX blockchain",
    31  		config.Chinese: "添加IOTX到IoTeX区块链上的投票",
    32  	}
    33  )
    34  
    35  // _stake2AddCmd represents the stake2 add command
    36  var _stake2AddCmd = &cobra.Command{
    37  	Use:   config.TranslateInLang(_stake2AddCmdUses, config.UILanguage),
    38  	Short: config.TranslateInLang(_stake2AddCmdShorts, config.UILanguage),
    39  	Args:  cobra.RangeArgs(2, 3),
    40  	RunE: func(cmd *cobra.Command, args []string) error {
    41  		cmd.SilenceUsage = true
    42  		err := stake2Add(args)
    43  		return output.PrintError(err)
    44  	},
    45  }
    46  
    47  func init() {
    48  	RegisterWriteCommand(_stake2AddCmd)
    49  }
    50  
    51  func stake2Add(args []string) error {
    52  
    53  	bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
    54  	if err != nil {
    55  		return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
    56  	}
    57  
    58  	amountInRau, err := util.StringToRau(args[1], util.IotxDecimalNum)
    59  	if err != nil {
    60  		return output.NewError(output.ConvertError, "invalid amount", err)
    61  	}
    62  
    63  	var data []byte
    64  	if len(args) == 3 {
    65  		data, err = hex.DecodeString(args[2])
    66  		if err != nil {
    67  			return output.NewError(output.ConvertError, "failed to decode data", err)
    68  		}
    69  	}
    70  
    71  	sender, err := Signer()
    72  	if err != nil {
    73  		return output.NewError(output.AddressError, "failed to get signed address", err)
    74  	}
    75  
    76  	gasLimit := _gasLimitFlag.Value().(uint64)
    77  	if gasLimit == 0 {
    78  		gasLimit = action.DepositToStakeBaseIntrinsicGas + action.DepositToStakePayloadGas*uint64(len(data))
    79  	}
    80  
    81  	gasPriceRau, err := gasPriceInRau()
    82  	if err != nil {
    83  		return output.NewError(0, "failed to get gas price", err)
    84  	}
    85  	nonce, err := nonce(sender)
    86  	if err != nil {
    87  		return output.NewError(0, "failed to get nonce ", err)
    88  	}
    89  
    90  	s2a, err := action.NewDepositToStake(nonce, bucketIndex, amountInRau.String(), data, gasLimit, gasPriceRau)
    91  	if err != nil {
    92  		return output.NewError(output.InstantiationError, "failed to make a depositToStake instance", err)
    93  	}
    94  	return SendAction(
    95  		(&action.EnvelopeBuilder{}).
    96  			SetNonce(nonce).
    97  			SetGasPrice(gasPriceRau).
    98  			SetGasLimit(gasLimit).
    99  			SetAction(s2a).Build(),
   100  		sender)
   101  }