github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2change.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  	"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  )
    18  
    19  // Multi-language support
    20  var (
    21  	_stake2ChangeCmdUses = map[config.Language]string{
    22  		config.English: "change CANDIDATE_NAME BUCKET_INDEX [DATA]" +
    23  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "change 候选人名字 票索引 [数据]" +
    25  			" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    26  	}
    27  	_stake2ChangeCmdShorts = map[config.Language]string{
    28  		config.English: "Change stake candidate",
    29  		config.Chinese: "在IoTeX区块链上改变投票候选人",
    30  	}
    31  )
    32  
    33  // _stake2ChangeCmd represents the stake2 change command
    34  var _stake2ChangeCmd = &cobra.Command{
    35  	Use:   config.TranslateInLang(_stake2ChangeCmdUses, config.UILanguage),
    36  	Short: config.TranslateInLang(_stake2ChangeCmdShorts, config.UILanguage),
    37  	Args:  cobra.RangeArgs(2, 3),
    38  	RunE: func(cmd *cobra.Command, args []string) error {
    39  		cmd.SilenceUsage = true
    40  		err := stake2Change(args)
    41  		return output.PrintError(err)
    42  	},
    43  }
    44  
    45  func init() {
    46  	RegisterWriteCommand(_stake2ChangeCmd)
    47  }
    48  
    49  func stake2Change(args []string) error {
    50  	var candidateName = args[0]
    51  	if !action.IsValidCandidateName(candidateName) {
    52  		return output.NewError(output.ValidationError, "", action.ErrInvalidCanName)
    53  	}
    54  
    55  	bucketIndex, err := strconv.ParseUint(args[1], 10, 64)
    56  	if err != nil {
    57  		return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
    58  	}
    59  
    60  	var data []byte
    61  	if len(args) == 3 {
    62  		data, err = hex.DecodeString(args[2])
    63  		if err != nil {
    64  			return output.NewError(output.ConvertError, "failed to decode data", err)
    65  		}
    66  	}
    67  
    68  	sender, err := Signer()
    69  	if err != nil {
    70  		return output.NewError(output.AddressError, "failed to get signed address", err)
    71  	}
    72  
    73  	gasLimit := _gasLimitFlag.Value().(uint64)
    74  	if gasLimit == 0 {
    75  		gasLimit = action.MoveStakeBaseIntrinsicGas + action.MoveStakePayloadGas*uint64(len(data))
    76  	}
    77  
    78  	gasPriceRau, err := gasPriceInRau()
    79  	if err != nil {
    80  		return output.NewError(0, "failed to get gas price", err)
    81  	}
    82  	nonce, err := nonce(sender)
    83  	if err != nil {
    84  		return output.NewError(0, "failed to get nonce ", err)
    85  	}
    86  
    87  	s2c, err := action.NewChangeCandidate(nonce, candidateName, bucketIndex, data, gasLimit, gasPriceRau)
    88  	if err != nil {
    89  		return output.NewError(output.InstantiationError, "failed to make a changeCandidate instance", err)
    90  	}
    91  	return SendAction(
    92  		(&action.EnvelopeBuilder{}).
    93  			SetNonce(nonce).
    94  			SetGasPrice(gasPriceRau).
    95  			SetGasLimit(gasLimit).
    96  			SetAction(s2c).Build(),
    97  		sender)
    98  }