github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/stake2update.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  	"github.com/spf13/cobra"
    10  
    11  	"github.com/iotexproject/iotex-core/action"
    12  	"github.com/iotexproject/iotex-core/ioctl/config"
    13  	"github.com/iotexproject/iotex-core/ioctl/output"
    14  	"github.com/iotexproject/iotex-core/ioctl/util"
    15  )
    16  
    17  // Multi-language support
    18  var (
    19  	_stake2UpdateCmdUses = map[config.Language]string{
    20  		config.English: "update NAME (ALIAS|OPERATOR_ADDRESS) (ALIAS|REWARD_ADDRESS)" +
    21  			" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    22  		config.Chinese: "update 名字 (别名|操作者地址) (别名|奖励地址)" +
    23  			" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
    24  	}
    25  	_stake2UpdateCmdShorts = map[config.Language]string{
    26  		config.English: "Update candidate on IoTeX blockchain",
    27  		config.Chinese: "在IoTeX区块链上更新候选人",
    28  	}
    29  )
    30  
    31  var _stake2UpdateCmd = &cobra.Command{
    32  	Use:   config.TranslateInLang(_stake2UpdateCmdUses, config.UILanguage),
    33  	Short: config.TranslateInLang(_stake2UpdateCmdShorts, config.UILanguage),
    34  	Args:  cobra.ExactArgs(3),
    35  	RunE: func(cmd *cobra.Command, args []string) error {
    36  		cmd.SilenceUsage = true
    37  		err := stake2Update(args)
    38  		return output.PrintError(err)
    39  	},
    40  }
    41  
    42  func init() {
    43  	RegisterWriteCommand(_stake2UpdateCmd)
    44  }
    45  
    46  func stake2Update(args []string) error {
    47  	name := args[0]
    48  	if !action.IsValidCandidateName(name) {
    49  		return output.NewError(output.ValidationError, "", action.ErrInvalidCanName)
    50  	}
    51  
    52  	operatorAddrStr, err := util.Address(args[1])
    53  	if err != nil {
    54  		return output.NewError(output.AddressError, "failed to get operator address", err)
    55  	}
    56  	rewardAddrStr, err := util.Address(args[2])
    57  	if err != nil {
    58  		return output.NewError(output.AddressError, "failed to get reward address", err)
    59  	}
    60  
    61  	sender, err := Signer()
    62  	if err != nil {
    63  		return output.NewError(output.AddressError, "failed to get signed address", err)
    64  	}
    65  
    66  	gasLimit := _gasLimitFlag.Value().(uint64)
    67  	if gasLimit == 0 {
    68  		gasLimit = action.CandidateUpdateBaseIntrinsicGas
    69  	}
    70  
    71  	gasPriceRau, err := gasPriceInRau()
    72  	if err != nil {
    73  		return output.NewError(0, "failed to get gas price", err)
    74  	}
    75  	nonce, err := nonce(sender)
    76  	if err != nil {
    77  		return output.NewError(0, "failed to get nonce ", err)
    78  	}
    79  
    80  	s2u, err := action.NewCandidateUpdate(nonce, name, operatorAddrStr, rewardAddrStr, gasLimit, gasPriceRau)
    81  	if err != nil {
    82  		return output.NewError(output.InstantiationError, "failed to make a candidateUpdate instance", err)
    83  	}
    84  	return SendAction(
    85  		(&action.EnvelopeBuilder{}).
    86  			SetNonce(nonce).
    87  			SetGasPrice(gasPriceRau).
    88  			SetGasLimit(gasLimit).
    89  			SetAction(s2u).Build(),
    90  		sender)
    91  }